如何在javascript中解析JSON数组

时间:2017-03-14 06:08:17

标签: 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 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);
}

这里我将jsonData.root_name.length作为undefined也无法解析数组。

任何人都可以指导如何使这项工作?

1 个答案:

答案 0 :(得分:0)

你可以直接循环浏览这些数据..

$.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]);
            }