How To Parse Multi Dimensional Json Data Through Javascript
How could i parse this type of json data, getting in 'results' to fetch single values like zipcode, state etc { 'row': [ { 'id': '5', 'name': 'test',
Solution 1:
first, you need to parse that string with JSON.parse
var myJson = JSON.parse(the_raw_data_string);
it ends up into an object like this:
var myJson = {
"row": [
{
"id": "5",
"name": "test",
"email": "test@test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}
accessing the items:
myJson.row[0].idmyJson.row[0].namemyJson.row[0].street//and so on...
Solution 2:
you can take the json result to a var like follows
var json = {
"row": [
{
"id": "5",
"name": "test",
"email": "test@test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"}
]
}
then get the result to another
var result = json.row;
then you can iterate through the result
for (var i = 0; i < result.length; i++) {
varobject = result[i];
for (property inobject) {
varvalue = object[property];
alert(property + "=" + value); // This alerts "id=5", etc..
}
}
hope this will help you
Solution 3:
Again here jQuery is your good friend
I have posted a sample using jsfiddle with multiple records in your data row
$(document).ready(function () {
var result = {
"row": [
{
"id": "5",
"name": "test",
"email": "test@test.com",
"street": "mystreet",
"city": "mycity",
"state": "mystate",
"zipcode": "123456",
"myimage": "image.gif"
},
{
"id": "10",
"name": "test2",
"email": "test2@test.com",
"street": "mystreet2",
"city": "mycity2",
"state": "mystate2",
"zipcode": "7891011",
"myimage": "image.gif"
}
]
};
var oE = $("#output");
$.each(result.row, function(index, value) {
//- extract target value like zipCode
oE.append($("<li></li>").text(value.zipcode));
});
});
Hope this helps.
Solution 4:
If the json data is raw then use json.parse. After that to loop the multi dimensional json data.
data = {"employees":[
{ "firstName":"Anushka", "lastName":"shetty" },
{ "firstName":"Shreya", "lastName":"Saran" },
{ "firstName":"Kajal", "lastName":"Agarwal" }
]};
for (var key indata.employees) {
alert(data.employees[key].firstName) //alert Anushka, Shreya, Kajal
}
Solution 5:
You can use JQuery each function:
$.each(myData.row, function(index,item) {
// here you can extract the data
alert (item.zipcode);
});
Post a Comment for "How To Parse Multi Dimensional Json Data Through Javascript"