我在我的一个应用程序中处理
JSON响应.我已成功使用jsonp建立连接.但是我无法解析我的回复.
码:
<script type='text/javascript'> (function($) { var url = 'http://cooktv.sndimg.com/webcook/sandBox/perf/topics.json'; $.ajax({ type: 'GET',url: url,async: false,jsonpCallback: 'callback',contentType: "application/json",dataType: 'jsonp',success: function(json) { console.log(json.topics); $("#nav").html('<a href="">'+json.topics+"</a>"); },error: function(e) { console.log(e.message); } }); })(jQuery); </script>
输出我得到:
[object Object],[object Object],[object Object]
响应示例:
callback({"topics":[{"name":"topic","content":[{"link_text":"link","link_src":"http://www.foodnetwork.com/"},{"link_text":"link","link_src":"http://www.hgtv.com/"},"link_src":"http://www.diynetwork.com/"},"link_src":"http://www.cookingchanel.com/"},"link_src":"http://www.travelchannel.com/"},"link_src":"http://www.food.com/"}]},{"name":"topic2","link_src":"http://www.google.com/"},"link_src":"http://www.yahoo.com/"},"link_src":"http://www.aol.com/"},"link_src":"http://www.msn.com/"},"link_src":"http://www.facebook.com/"},"link_src":"http://www.twitter.com/"}]},{"name":"topic3","link_src":"http://www.a.com/"},"link_src":"http://www.b.com/"},"link_src":"http://www.c.com/"},"link_src":"http://www.d.com/"},"link_src":"http://www.e.com/"},"link_src":"http://www.f.com/"}]}]});
我希望以下列形式:
尝试一下:
原文链接:https://www.f2er.com/ajax/159901.htmlsuccess: function(json) { console.log(JSON.stringify(json.topics)); $.each(json.topics,function(idx,topic){ $("#nav").html('<a href="' + topic.link_src + '">' + topic.link_text + "</a>"); }); },