简体   繁体   English

如何使用jQuery解析JSON

[英]How can I parse JSON using jQuery

This is my JSON output from server side: 这是服务器端的JSON输出:

[{"id":1,"name":"Information Technology"},
 {"id":2,"name":"mechanical engineering"},
 {"id":3,"name":"computer science"},
 {"id":4,"name":"electronics and communication"}] 

How can I parse this in my client side using jQuery? 如何使用jQuery在客户端解析此内容?

IF you're getting it through jquery's getJson method, your object will be automatically be parsed. 如果您通过jquery的getJson方法获取它,则将自动解析您的对象。
A second option will be to parse it yourself through the parseJSON jquery method ( http://api.jquery.com/jQuery.parseJSON/ ) or through the browser's native implementation of JSON parsing : JSON.parse . 第二种选择是自己通过parseJSON jquery方法( http://api.jquery.com/jQuery.parseJSON/ )或通过浏览器的JSON解析的本机实现JSON.parse解析。
The last alternative would be to evaluate the object using eval , but that isn't recommended. 最后一种选择是使用eval评估对象,但是不建议这样做。

正如您专门要求使用JQuery的解决方案一样,您应该查看JQuery文档,这是他们的JSON解析文档Jquery.parseJSON

使用jQuery的

var theObject = jQuery.parseJSON(jsonString);

try this 尝试这个

for (var idx in json_object){
 console.log(json_object[idx].id, json_object[idx].name);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM