如何在jQuery中解析JSON多维数组? [英] How do I parse a JSON multidimensional array in jQuery?

查看:78
本文介绍了如何在jQuery中解析JSON多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要解析的JSON:

Here is my JSON which I need to parse:

{"opcode":"groupdetails",
 "status":"success",
 "data":[{"Group ID":5,"Group Name":"data structure","Group Subject":"computer science","Role Type":"Teacher"},{"Group ID":4,"Group Name":"information technology","Group Subject":"computer science","Role Type":"Student"},{"Group ID":6,"Group Name":"data mining","Group Subject":"computer science","Role Type":"Parent"},{"Group ID":7,"Group Name":"dccn","Group Subject":"computer science","Role Type":"Teacher"}]}

我有尝试并实现了此处提供的解决方案,这是JS中定义的实现解决方案,仅解析JSON数组

I have tried and implemented the solution provided here and this is the implementation of JS that was defined in there solution, which parses only the JSON array

for (var i = 0; i < data.data.length; i++) 
 {
    var object = data.data[i];
     for (property in object) 
     {
        var value = object[property];
        alert(property + "=" + value);
     }
 }

外部JSON 数据是从服务器返回的,是的,我尝试使用以下代码进行解析,但没有结果:

the outer JSON data is returned from server and yes I have tried parsing using the following code and there is no result:

for (var i = 0; i < data.length; i++) 
{
 var object = data[i];
 for (property in object) 
 {
    var value = object[property];
    alert(property + "=" + value);
 }
}

如何使用单一方法解析整个JSON而不是单独解析JSON数组?

How can I parse the entire JSON using a single method instead of parsing the JSON array separately?

推荐答案

试试这个:

for(var key in data) {
    if(typeof data[key] === "object") {
        for(var i = 0; i < data[key].length; i++) {
            for(var property in data[key][i]) {
                 alert(property + " = " + data[key][i][property]);
            }
        }
    } else if(typeof data[key] === "string") {
        alert(key + " = " + data[key]);
    }
}

这篇关于如何在jQuery中解析JSON多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆