简体   繁体   中英

How to get data from JsonArray within Array using Javascript/JQuery?

I'm having the service response like,

{
    "Name": [
        [{
                "Key": "A",
                "Value": "Sample1"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample2"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample3"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample4"
            }
        ]
    ],
    "Title": "Office"
}

I need the output as Value field.

I tried lot of ways.But not getting any solutions. Please help me..

Try this:

var obj = {"Name":[[{"Key":"A","Value":"Sample1"}],[{"Key":"A","Value":"Sample2"}],[{"Key":"A","Value":"Sample3"}],[{"Key":"A","Value":"Sample4"}]],"Title":"Office"}

$.each( obj.Name, function( key, d ) {
  console.log( key + ": " + d[0].Value );
});
var data= {"Name":[[{"Key":"A","Value":"Sample1"}],[{"Key":"A","Value":"Sample2"}],[{"Key":"A","Value":"Sample3"}],[{"Key":"A","Value":"Sample4"}]],"Title":"Office"};    
var output = [];

for(var i = 0;i<data.Name.length;i++) // data is your JSON response
    output.push(data.Name[i][0].Value);
alert(output);

Fiddle for your Reference

Please have a look on this jsfiddle http://jsfiddle.net/2dJAN/16/

var fields= {
    "Name": [
        [{
                "Key": "A",
                "Value": "Sample1"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample2"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample3"
            }
        ],
        [{
                "Key": "A",
                "Value": "Sample4"
            }
        ]
    ],
    "Title": "Office"
}

$.each(fields['Name'], function(index, value) {
$.each(value, function(index, innervalue) {
alert(innervalue['Value'])
});
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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