10

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",
        "email": "[email protected]",
        "street": "mystreet",
        "city": "mycity",
        "state": "mystate",
        "zipcode": "123456",
        "myimage": "image.gif"}
    ]
}​

6 Answers 6

14

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": "[email protected]",
        "street": "mystreet",
        "city": "mycity",
        "state": "mystate",
        "zipcode": "123456",
        "myimage": "image.gif"}
    ]
}​

accessing the items:

myJson.row[0].id
myJson.row[0].name
myJson.row[0].street
//and so on...
Sign up to request clarification or add additional context in comments.

Comments

9

you can take the json result to a var like follows

var json = {
    "row": [
        {
        "id": "5",
        "name": "test",
        "email": "[email protected]",
        "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++) {
    var object = result[i];
    for (property in object) {
        var value = object[property];
        alert(property + "=" + value); // This alerts "id=5",  etc..
    }
}

hope this will help you

Comments

2

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": "[email protected]",
        "street": "mystreet",
        "city": "mycity",
        "state": "mystate",
        "zipcode": "123456",
        "myimage": "image.gif"
        },
        {
        "id": "10",
        "name": "test2",
        "email": "[email protected]",
        "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.

Comments

2

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 in data.employees) {   
    alert(data.employees[key].firstName) //alert Anushka, Shreya, Kajal
}

Comments

1

You can use JQuery each function:

$.each(myData.row, function(index,item) {
  // here you can extract the data
  alert (item.zipcode);
});

Comments

0

Use JSON.parse(jsonstring). Then iterate over the objects/arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.