从json数组字符串中获取变量数据

时间:2014-04-18 16:44:58

标签: javascript json

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

但要获得parentIdname的价值呢?

1 个答案:

答案 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']