我在我的一个应用程序中处理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_text":"link","link_src":"http://www.diynetwork.com/"},{"link_text":"link","link_src":"http://www.cookingchanel.com/"},{"link_text":"link","link_src":"http://www.travelchannel.com/"},{"link_text":"link","link_src":"http://www.food.com/"}]},{"name":"topic2","content":[{"link_text":"link","link_src":"http://www.google.com/"},{"link_text":"link","link_src":"http://www.yahoo.com/"},{"link_text":"link","link_src":"http://www.aol.com/"},{"link_text":"link","link_src":"http://www.msn.com/"},{"link_text":"link","link_src":"http://www.facebook.com/"},{"link_text":"link","link_src":"http://www.twitter.com/"}]},{"name":"topic3","content":[{"link_text":"link","link_src":"http://www.a.com/"},{"link_text":"link","link_src":"http://www.b.com/"},{"link_text":"link","link_src":"http://www.c.com/"},{"link_text":"link","link_src":"http://www.d.com/"},{"link_text":"link","link_src":"http://www.e.com/"},{"link_text":"link","link_src":"http://www.f.com/"}]}]});
我希望以下列形式:
主题:链接
答案 0 :(得分:21)
尝试一下:
success: 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>");
});
},
答案 1 :(得分:5)
我在谷歌上闲逛,然后我发现了你的问题,将JSON响应解析为普通HTML非常简单。只需使用这个小JavaScript代码:
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p id="demo"></p>
<script>
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;
</script>
</body>
</html>
答案 2 :(得分:1)
jQuery.ajax({
type: 'GET',
url: "../struktur2/load.php",
async: false,
contentType: "application/json",
dataType: 'json',
success: function(json) {
items = json;
},
error: function(e) {
console.log("jQuery error message = "+e.message);
}
});
答案 3 :(得分:0)
JSON返回的数据采用json格式:这只是一个值数组。这就是你看到 [object Object],[object Object],[object Object] 的原因。
您必须遍历该值才能获得实际值。如下所示
jQuery为迭代提供$ .each(),所以你也可以这样做:
$.getJSON("url_with_json_here", function(data){
$.each(data, function (linktext, link) {
console.log(linktext);
console.log(link);
});
});
现在只需使用该信息创建一个超链接。
答案 4 :(得分:0)
原始问题是解析主题列表,但是从原始示例开始,让函数返回单个值也可能有用。为此,这是(单向)这样做的一个例子:
<script type='text/javascript'>
function getSingleValueUsingJQuery() {
var value = "";
var url = "rest/endpointName/" + document.getElementById('someJSPFieldName').value;
jQuery.ajax({
type: 'GET',
url: url,
async: false,
contentType: "application/json",
dataType: 'json',
success: function(json) {
console.log(json.value); // needs to match the payload (i.e. json must have {value: "foo"}
value = json.value;
},
error: function(e) {
console.log("jQuery error message = "+e.message);
}
});
return value;
}
</script>
答案 5 :(得分:0)
尝试下面的代码。这有助于您的代码。
$("#btnUpdate").on("click", function () {
//alert("Alert Test");
var url = 'http://cooktv.sndimg.com/webcook/sandbox/perf/topics.json';
$.ajax({
type: "GET",
url: url,
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
debugger;
$.each(result.callback, function (index, value) {
alert(index + ': ' + value.Name);
});
},
failure: function (result) { alert('Fail'); }
});
});
我无法访问您的网址。波纹错误显示
XMLHttpRequest无法加载http://cooktv.sndimg.com/webcook/sandbox/perf/topics.json。对预检请求的响应没有通过访问控制检查:否&#39;访问控制 - 允许 - 来源&#39;标头出现在请求的资源上。起源&#39; http://localhost:19829&#39;因此不允许访问。响应的HTTP状态代码为501。