JSON使用Jquery访问和搜索嵌套对象

时间:2015-04-02 17:08:12

标签: javascript jquery json

我想访问嵌套的JSON,例如

var SchoolData =  {
        "SchoolRecord": {
            "classX": {
                "name": "Student Name 1",
                "subject": {
                    "mathsDept": {
                        "subject": "Maths",
                        "marks": 87
                    },
                    "scienceDept": {
                        "subject": "Physics",
                        "marks": 55                
                    },
                    "socialDept": {
                        "subject": "Social",
                        "marks": 95
                    }
                }
            },
            "classIX": {
                "name": "Student Name 2",
                 "subject": {
                    "mathsDept": {
                        "subject": "Maths",
                        "marks": 99
                    },
                    "scienceDept": {
                        "subject": "Physics",
                        "marks": 95

                    },
                    "socialDept": {
                        "subject": "Social",
                        "marks": 91
                    }
                }
            }
        }
    }

    console.log(SchoolData.SchoolRecord);

console.log(SchoolData.SchoolRecord[0]); 

这是FIDDLE

我想在JSON数据SchoolData.marks

中添加标记

3 个答案:

答案 0 :(得分:0)

你的第二个console.log失败了,因为当它是一个对象时,你将SchoolData.SchoolRecord视为一个数组。

要获得所有标记,您需要深入了解每个类对象:

console.log(SchoolData.SchoolRecord.classX.subject.mathsDept);
console.log(SchoolData.SchoolRecord.classX.subject.scienceDept);
console.log(SchoolData.SchoolRecord.classX.subject.socialDept);
console.log(SchoolData.SchoolRecord.classIX.subject.mathsDept);
console.log(SchoolData.SchoolRecord.classIX.subject.scienceDept);
console.log(SchoolData.SchoolRecord.classIX.subject.socialDept);

这不太理想,但可能符合您的需求。

如果您可以控制该数据的格式,请考虑使用数组进行结构化。也许是这样的:

var schoolData = {
    schoolRecord: {
        classes: [
            {name: "classX", student: {name: "Student Name 1", subjects: [
                {dept: "Maths", subject: "Maths", marks: [99]}
                {dept: "Physics", subject: "Physics", marks: [99]}
            ]}}
        ]
    }
}

这是一个完整的猜测,因为我不知道您的数据结构。使用数组意味着提取数据“应该”更容易。

答案 1 :(得分:0)

使用以下代码行。

SchoolData.SchoolRecord.classX.subject.mathsDept.marks

用于获取索引数据

SchoolData.SchoolRecord[0]

您必须创建一个记录数组

var SchoolData =  {
        "SchoolRecord": [{
            "classX": {
                "name": "Student Name 1",
                "subject": {
                    "mathsDept": {
                        "subject": "Maths",
                        "marks": 87
                    },
                    "scienceDept": {
                        "subject": "Physics",
                        "marks": 55                
                    },
                    "socialDept": {
                        "subject": "Social",
                        "marks": 95
                    }
                }
            },
            "classIX": {
                "name": "Student Name 2",
                 "subject": {
                    "mathsDept": {
                        "subject": "Maths",
                        "marks": 99
                    },
                    "scienceDept": {
                        "subject": "Physics",
                        "marks": 95

                    },
                    "socialDept": {
                        "subject": "Social",
                        "marks": 91
                    }
                }
            }
        }]
    }

答案 2 :(得分:0)

您可以使用_.each() function in underscorejs轻松完成此操作。

以下是JSFiddle中的解决方案:http://jsfiddle.net/g87g28h5/1/

基本位在这里:

for(var Class in SchoolData.SchoolRecord) {
    _.each(SchoolData.SchoolRecord[Class].subject, function(item) {
        var entry = "<li>" + Class;
        entry += ": " + item.subject;
        entry += " = " + item.marks;
        entry += "</li>";
        listing.innerHTML = listing.innerHTML + entry;
    });
}