json string
var jsonString ='[
{`"id":1`,"parentId":"0","name":"Mc Donald","value":{"name":"Mc Donald","device_id":null,"loc_det_id":"1"}},
{"id":"2","parentId":"1","name":"All Regions","value":{"name":"All Regions","isleaf":false,"device_id":null,"loc_det_id":"2"}},
{"id":"5","parentId":"2","name":"West","value":{"name":"West","isleaf":false,"device_id":null,"loc_det_id":"5"}},
{"id":"7","parentId":"5","name":"Mumbai","value":{"name":"Mumbai","isleaf":false,"device_id":null,"loc_det_id":"7"}}
]'
jsonString[0].id
,这会给我id
1
但要获得parentId
或name
的价值呢?
答案 0 :(得分:2)
您应该使用JSON.parse()将您的字符串转换为有效的JSON对象。
var jsonData = JSON.parse(jsonString);
jsonData[0].id
jsonData[0].name
jsonData[0].parentId
或
var jsonData = JSON.parse(jsonString);
jsonData[0]['id']
jsonData[0]['name']
jsonData[0]['parentId']