简体   繁体   中英

Get Json values from string

I have a json string like the below format. I would like to get the Table1.ActualDate value. How can i get this using jquery.

{
    "Table": [
        {
            "DisplayVoucherNumber": "A101239Z",
            "ActualDate": "08/07/2013",
            "AccountName": "shyamal",
            "Pcs": "50",
            "Weight": "500.000"
        }
    ],
    "Table1": [
        {
            "DisplayVoucherNumber": "R101249B",
            "ActualDate": "11/07/2013",
            "AccountName": "vipul",
            "NetWeight": "90.000",
            "Weight": "80.000",
            "Difference": "10.000"
        },
        {
            "DisplayVoucherNumber": "R101249B",
            "ActualDate": "11/07/2013",
            "AccountName": "vipul",
            "NetWeight": "500.000",
            "Weight": "100.000",
            "Difference": "400.000"
        }
    ]
}

You have to parse that string to get a valid JSON object.

Try,

var xObj = JSON.parse(xString);
console.log(xObj.Table1[0].ActualDate);

where xString is the string variable contains your JSON string.

If it is a string, you should make it as a json object using

var data= jQuery.parseJSON(data); 

After that can use it like.

 alert(data.Table1[0].ActualDate);

If it is an abject already, you dont have to use the jQuery.parseJSON() method

Since your Table1 array may grow, I would suggest you to go for iteration like

var tab = JSON.parse("you object");
for (i = 0; i < tab["Table1"].length; i++) {
    console.log(tab["Table1"][i].AccountName);
}
var jObj = JSON.parse(jsonString); // Converts the string to JSON object

if(jObj !== undefined && jObj.Table1 !== undefined) {
    for (i = 0; i < jObj.Table1.length; i++) {
        console.log(jObj.Table1[i].ActualDate); 
    }
}

You can use:

$.each(json.Table1, function(x, contents) {
    alert(contents.ActualDate);
});

Fiddle Demo

        var json = {
            "Table": [
                {
                    "DisplayVoucherNumber": "A101239Z",
                    "ActualDate": "08/07/2013",
                    "AccountName": "shyamal",
                    "Pcs": "50",
                    "Weight": "500.000"
                }
            ],
            "Table1": [
                {
                    "DisplayVoucherNumber": "R101249B",
                    "ActualDate": "11/07/2013",
                    "AccountName": "vipul",
                    "NetWeight": "90.000",
                    "Weight": "80.000",
                    "Difference": "10.000"
                },
                {
                    "DisplayVoucherNumber": "R101249B",
                    "ActualDate": "13/08/2012",
                    "AccountName": "vipul",
                    "NetWeight": "500.000",
                    "Weight": "100.000",
                    "Difference": "400.000"
                }
            ]
        };


        for(var i=0; i<json.Table1.length; ++i) {
            alert(json.Table1[i].ActualDate)
        }

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