简体   繁体   English

在jquery中解析json对象

[英]Parse json object in jquery

I see a lot of examples for parsing a json object in jquery using $.parseJSON and understood them. 我看到很多使用$.parseJSON在jquery中解析json对象的例子并理解它们。 However I am missing some basics and I am not able to parse the below VALID json. 但是我缺少一些基础知识,我无法解析下面的VALID json。

{
   "studentList":
   [
       {
           "id": 2,
           "name": "Alex",
           "opened": true
       },
       {
           "id": 3,
           "name": "Paul",
           "opened": true
       }
 ]
}

All the examples I found over the internet has a structure like below 我在互联网上找到的所有例子都有如下结构

[
       {
           "id": 2,
           "name": "Alex",
           "opened": true
       },
       {
           "id": 3,
           "name": "Paul",
           "opened": true
       }
 ]

Notice, its not enclosed in { "sudentList": } . 注意,它没有包含在{ "sudentList": } Can someone explain the logic to parse such json? 有人可以解释解析这样的json的逻辑吗?

The studentList has two arrays inside. studentList里面有两个数组。 So you can parse the values directly or using $.each . 因此,您可以直接或使用$.each解析值。

var a = { "studentList": [ { "id": 2, "name": "Alex", "opened": true }, { "id": 3, "name": "Paul", "opened": true } ] };

//values shoud be Alex and Paul
console.log(a.studentList[0].name)
console.log(a.studentList[1].name)

http://jsfiddle.net/EYrxJ/ http://jsfiddle.net/EYrxJ/

var a = { "studentList": [ { "id": 2, "name": "Alex", "opened": true }, { "id": 3, "name": "Paul", "opened": true } ] };

//values shoud be Alex and Paul
$.each(a.studentList, function() {
    console.log(this.name);
});

http://jsfiddle.net/EYrxJ/1/ http://jsfiddle.net/EYrxJ/1/

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

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