如何在javascript中解析JSON数组
我需要帮助用javascript解析json数组如何在javascript中解析JSON数组,javascript,php,json,Javascript,Php,Json,我需要帮助用javascript解析json数组 var data={ "root_name": { "0": "400", "1": "test coupon by ashutosh", "2": "90", "3": "1", "4": "2", "5": "", "6": "test coupon by ashutosh test coupon by ashutosh test coupon by as
var data={
"root_name": {
"0": "400",
"1": "test coupon by ashutosh",
"2": "90",
"3": "1",
"4": "2",
"5": "",
"6": "test coupon by ashutosh test coupon by ashutosh test coupon by ashutoshundefined",
"7": "",
"8": "2",
"9": "1",
"10": "50",
"11": "0",
"12": "0",
"13": "0",
"14": "",
"15": "0",
"16": "0",
"17": "1",
"18": "0",
"19": "Test Store",
"20": "0",
"21": "test coupon by ashutosh test coupon by ashutosh",
"22": "250002",
"23": "0",
"24": "1",
"25": "2017-03-14 00:06:07",
"26": "",
"27": "",
"id": "400",
"title": "test coupon by ashutosh",
"user_id": "90",
"category_id": "1",
"subcategory_id": "2",
"image": "",
"description": "test coupon by ashutosh test coupon by ashutosh test coupon by ashutoshundefined",
"tags": "",
"duration": "2",
"charges": "1",
"fixed_rate": "50",
"hour_rate": "0",
"buy_one": "0",
"straight_price": "0",
"straight_off": "",
"was_price": "0",
"now_price": "0",
"coupan_type": "1",
"no_of_view": "0",
"business_name": "Test Store",
"no_like": "0",
"advice": "test coupon by ashutosh test coupon by ashutosh",
"zipcode": "250002",
"chk_add": "0",
"status": "1",
"post_date": "2017-03-14 00:06:07",
"lat": "",
"lon": ""
}
}
这就是我试图用javascript解析上述json数组的方式
var jsonData = JSON.parse(data);
alert(jsonData.root_name.length)
for (var i = 0; i < jsonData.root_name.length; i++) {
var counter = jsonData.root_name[i];
alert(counter.id);
}
var jsonData=JSON.parse(数据);
警报(jsonData.root\u name.length)
for(var i=0;i
这里我得到的jsonData.root_name.length是未定义的,也无法解析数组
任何人都可以指导这项工作吗?您可以直接循环浏览这些数据
$.each(data.root_name, function(i){
console.log(data.root_name[i]);
})
循环到JS中
for(i in data.root_name){
console.log(data.root_name[i]);
}
试试JSON.parse(JSON.stringify(data))
root\u name
是一个对象,没有length
数据
已经是一个对象。不能对其调用JSON.parse
。也没有数组。它不是数组它的JSON对象,你可以直接循环通过这个..@FelixKling谢谢你的帮助,你能告诉我如何检索值我想检索id和标题。谢谢你的帮助,请用javascript编写。请参见我的编辑…data.root\u name undefinedsee此工作示例的问题似乎在于我从ajax php页面获取这些值,然后将这些值传递给javascript函数。