简体   繁体   中英

How can i get Json format data through jquery

i am trying to get the value of count and year but unable to get. I have tried this so far.

[
    [{
        "count": 130,
        "year": "Week 1"
    }],[{
        "count": 160,
        "year": "Week 2"
    }],[{
        "count": 190,
        "year": "Week 3"
    }]
]
var parsedData = JSON.parse(data);
alert(parsedData.count);
alert(parsedData[0].count);
alert(parsedData[0].year);

parseddata is a 2 dimensional array. Try accessing the data like this.

 var data = '[[{"count":130,"year":"Week 1"}],[{"count":160,"year":"Week 2"}],[{"count":190,"year":"Week 3"}]]'; var parseddata = JSON.parse(data); console.log(parseddata[0][0].count); console.log(parseddata[0][0].year); 

As @Rory McCrossan mentioned, your JSON is too messy. It can be simple as below.

 var data = [{ "count": 130, "year": "Week 1" }, { "count": 160, "year": "Week 2" }, { "count": 190, "year": "Week 3" }]; console.log(data[0].count); console.log(data[0].year); 

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