Tôi đang sử dụng Sencha Touch (ExtJS) để nhận thông báo JSON từ máy chủ. Tin nhắn tôi nhận được là thông báo này:Javascript cách phân tích cú pháp mảng JSON
{
"success": true,
"counters": [
{
"counter_name": "dsd",
"counter_type": "sds",
"counter_unit": "sds"
},
{
"counter_name": "gdg",
"counter_type": "dfd",
"counter_unit": "ds"
},
{
"counter_name": "sdsData",
"counter_type": "sds",
"counter_unit": " dd "
},
{
"counter_name": "Stoc final",
"counter_type": "number ",
"counter_unit": "litri "
},
{
"counter_name": "Consum GPL",
"counter_type": "number ",
"counter_unit": "litri "
},
{
"counter_name": "sdg",
"counter_type": "dfg",
"counter_unit": "gfgd"
},
{
"counter_name": "dfgd",
"counter_type": "fgf",
"counter_unit": "liggtggggri "
},
{
"counter_name": "fgd",
"counter_type": "dfg",
"counter_unit": "kwfgf "
},
{
"counter_name": "dfg",
"counter_type": "dfg",
"counter_unit": "dg"
},
{
"counter_name": "gd",
"counter_type": "dfg",
"counter_unit": "dfg"
}
]
}
Vấn đề của tôi là tôi không thể phân tích đối tượng JSON này để tôi có thể sử dụng từng đối tượng truy cập.
Tôi đang cố gắng để acomplish đó như thế này:
var jsonData = Ext.util.JSON.decode(myMessage);
for (var counter in jsonData.counters) {
console.log(counter.counter_name);
}
am i làm gì sai? Cảm ơn bạn!
Bản sao có thể có của [Biến an toàn chuỗi JSON thành đối tượng] (https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) –