需要帮助从Javascript对象中提取数据?

时间:2015-09-14 22:29:17

标签: javascript arrays json

你好我有这个数据:

var data={
"city_id": "2",
"dist": "2",
"City Name 1": [
{
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},
{
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}
],{
"city_id": "2",
"dist": "2",    
"City Name 2": [
{
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},
{
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}
]
}
};

我想循环遍历此JSON数据以提取City_id,dist_id,城市名称,然后另一个循环以获取姓名,地址和电话。数据格式不正确,我不知道如何修复它。

感谢您的帮助。谢谢

1 个答案:

答案 0 :(得分:1)

你的括号不合适,这不是一个有效的json。

var data={ //------------------------------------open
"city_id": "2",
"dist": "2",
 "City Name 1": [
{//----------------------open
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},//----------------------close
{//-----------------------open
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}//----------------------close
],{//----------------------------------open (problem is here. There is nothing index this object}
"city_id": "2",
"dist": "2",    
"City Name 2": [
{//-----------------------open
"id": 1,
"name": "name 1",
"address": "address 1",
"tel": "tel 1",
"Radio": "0",
"surgery": "0"
},//----------------------close
{//-----------------------open
"id": 2,
"name": "name 2",
"address": "address 2",
"tel": "tel 2",
"Radio": "0",
"surgery": "0"
}//----------------------close
]
}//----------------------------------close
}; //------------------------------------------------close

该位需要在数组中或从json中删除。