最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I parse a JSON multidimensional array in jQuery? - Stack Overflow

programmeradmin0浏览0评论

Here is my JSON which I need to parse:

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

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);
     }
 }

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);
 }
}

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

Here is my JSON which I need to parse:

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

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);
     }
 }

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);
 }
}

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

Share Improve this question edited May 23, 2017 at 11:50 CommunityBot 11 silver badge asked Jun 4, 2012 at 12:38 Akhil JainAkhil Jain 14.2k15 gold badges61 silver badges94 bronze badges 2
  • parse it into what? html, array or ?? – charlietfl Commented Jun 4, 2012 at 12:42
  • parse into html, further processing is yet to be done first i need to be sure that parsing is done properly. – Akhil Jain Commented Jun 4, 2012 at 12:44
Add a ment  | 

3 Answers 3

Reset to default 3

Try this:

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]);
    }
}

If your data is a JSON string, you need to decode it to object first. Use JSON.parse.

I was able to access each value on my multilevel array:

{"dirArray":[{"Dir":{"name":"hr12325","dir_description":"B2B NFIB Field","id":"249"}},{"Dir":{"name":"klk","dir_description":"B2B NFIB klk","id":"251"}}]}

using

data.dirArray[0].Dir.name
data.dirArray[0].Dir.dir_description

as explained here.

发布评论

评论列表(0)

  1. 暂无评论