Trying to parse with Jquery.parseJson... part II
I have recently post here about the Jquery.parseJson. Now I'm in part II of my achievement. I got this script working with a stackoverflow user help.
<input type="text" id="query" /><button>search</button><br />
<div id="results">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$("#query").val(); // Do nothing with this value
var json = eval('({"name":"John"})');
$("#results").append('<p>' + json.name + '</p>');
// $.getJSON('{"name":"John"}',function(json){
// $.each(json.results,function(i,obj){
// $("#results").append('<p>' + obj.name + '</p>');
// });
// });
});
});
Now that I have this code working I have build a real JSON to parse, here it is:
["http:\/\/guimaraes-braga.olx.pt\/seat-leon-1-9tdi-sport-111cv-iid-148286318",
{"1":"http:\/\/static04.olx-st.com\/images\/v4\/logos\/logo-default.png"},
{"2":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"},
{"3":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"},
{"4":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"},
{"5":"http:\/\/images02.olx.pt\/ui\/10\/开发者_Python百科71\/18\/t_1292886514_148286318_4.jpg"},
{"6":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"},
{"7":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"},
{"8":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"},
{"9":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"},
{"10":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"},
{"11":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"},
{"12":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"},
{"13":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"},
{"14":"http:\/\/images01.olx.com\/images\/spinner.gif"},
{"15":"http:\/\/images01.olx.pt\/ui\/10\/71\/18\/1292886514_148286318_1-Fotos-de--SEAT-LEON-19TDI-SPORT-111CV.jpg"},
{"16":""}]
This JSON above contain the URLs for images, and I have tried to pass this JSON just like in the first script, but it is not working... some more details in the code below.
<?php $json = new img_json_output(); ?>
<?php $data = $json->get_img_tags_as_json($json->get_page('http://guimaraes-braga.olx.pt/seat-leon-1-9tdi-sport-111cv-iid-148286318')); ?>
<input type="text" id="query" /><button>search</button><br />
<div id="results">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
$("#query").val(); // Do nothing with this value
var json = eval(<?php echo $data; ?>);
$("#results").append('<p>' + json.1 + '</p>');
});
});
</script>
The PHP variable $data returns the JSON.
How to get this to work? Is the problem on my JSON format?
This is not really an answer to your question but just a suggestion. Have you tried out the json2 library? It is really good and handles parsing and serializing json. Here is a link json.org. When minified it is 2.5k so not much additional overhead.
var json = eval( "(" + '<?php echo $data; ?>' + ")" );
Try to check:
1. the JSON string doesn't contain carriage returns.
2. the method chain. The outermost structure is an Array. It should be json[0] or json[1]["1"].
The following script works for me.
<body>
<input type="text" id="query" /><button>search</button><br />
<div id="results">
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('button').click(function(){
var json = eval('(' + '["http:\/\/guimaraes-braga.olx.pt\/seat-leon-1-9tdi-sport-111cv-iid-148286318", {"1":"http:\/\/static04.olx-st.com\/images\/v4\/logos\/logo-default.png"}, {"2":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"}, {"3":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"}, {"4":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"}, {"5":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"}, {"6":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"}, {"7":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"}, {"8":"http:\/\/images03.olx.pt\/ui\/10\/71\/18\/t_1292857323_148286318_1.jpg"}, {"9":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_2.jpg"}, {"10":"http:\/\/images04.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_3.jpg"}, {"11":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_4.jpg"}, {"12":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_5.jpg"}, {"13":"http:\/\/images02.olx.pt\/ui\/10\/71\/18\/t_1292886514_148286318_6.jpg"}, {"14":"http:\/\/images01.olx.com\/images\/spinner.gif"}, {"15":"http:\/\/images01.olx.pt\/ui\/10\/71\/18\/1292886514_148286318_1-Fotos-de--SEAT-LEON-19TDI-SPORT-111CV.jpg"}, {"16":""}]' + ')');
for (var i=1; i<json.length; i++){
$("#results").append('<p>' + json[i][i] + '</p>');
}
});
});
</script>
<hr />
</body>
var json = eval(<?php echo $data; ?>); // Will not work
Get rid of the eval function. JSON is valid JavaScript in this context of assigning it to a variable from server-generated JavaScript code. Then it should work fine:
var json = <?php echo $data; ?>; // Works fine
You do need to fix this line though:
$("#results").append('<p>' + json.1 + '</p>');
Try something like json[1]["1"]
instead of json.1
.
精彩评论