我最近在这里发布了关于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格式上吗?
答案 0 :(得分:0)
这不是你问题的真正答案,只是一个建议。你试过json2库吗?它非常好并且处理解析和序列化json。这是一个链接json.org。当缩小时,它是2.5k,因此没有多少额外的开销。
答案 1 :(得分:0)
var json = eval( "(" + '<?php echo $data; ?>' + ")" );
尝试检查:
1. JSON字符串不包含回车符
2.方法链。最外面的结构是一个数组。它应该是json [0]或json [1] [“1”]。
以下脚本适合我。
<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>
答案 2 :(得分:0)
var json = eval(<?php echo $data; ?>); // Will not work
摆脱eval功能。在此上下文中,JSON是有效的JavaScript,它将服务器生成的JavaScript代码分配给变量。然后它应该工作正常:
var json = <?php echo $data; ?>; // Works fine
您确实需要修复此行:
$("#results").append('<p>' + json.1 + '</p>');
尝试json[1]["1"]
而不是json.1
。