【发布时间】:2010-12-28 17:27:17
【问题描述】:
我最近在这里发布了有关 Jquery.parseJson 的信息。现在我进入了我成就的第二部分。我让这个脚本与 stackoverflow 用户帮助一起工作。
<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>');
// });
// });
});
});
现在我的代码已经工作了,我已经构建了一个真正的 JSON 来解析,这里是:
["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":""}]
上面的这个 JSON 包含图像的 URL,我尝试像在第一个脚本中一样传递这个 JSON,但是它不起作用……下面的代码中有更多细节。
<?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>
PHP 变量 $data 返回 JSON。
如何让它工作?是我的 JSON 格式有问题吗?
【问题讨论】: