-2

我想使用 jquery 解析 json 值,我无法使用 $.each 获取所有值,但我可以获取密钥。我不知道如何解析这些值。我的项目中使用了以下代码。所以以下是我需要解析它的json。

{
    "0": {
        "Date": "2013-09-05 03:56:30"
    },
    "1": {
        "City": {
            "id": "1",
            "country_id": "1",
            "cityname": "Mumbai",
            "city_urlname": "mumbai",
            "lat": "19.0759837",
            "long": "72.87765590000004",
            "status": "enable",
            "flag": "0",
            "created_on": "2013-09-04 22:02:42"
        }
    },
    "2": {
        "City": {
            "id": "2",
            "country_id": "1",
            "cityname": "Chennai",
            "city_urlname": "chennai",
            "lat": "13.0524139",
            "long": "80.25082459999999",
            "status": "enable",
            "flag": "0",
            "created_on": "2013-09-04 00:53:54"
        }
    },
    "33": {
        "Category": {
            "id": "1",
            "parent_id": null,
            "name": "dream",
            "category_urlname": "culture",
            "lft": "1",
            "rght": "2",
            "created_on": "2013-08-13 05:51:04",
            "status": "enable",
            "flag": "0"
        }
    },
    "34": {
        "Category": {
            "id": "2",
            "parent_id": null,
            "name": "Education",
            "category_urlname": "education",
            "lft": "3",
            "rght": "4",
            "created_on": "2013-08-13 05:51:23",
            "status": "enable",
            "flag": "0"
        }
    },
    "35": {
        "Category": {
            "id": "3",
            "parent_id": null,
            "name": "Marketing",
            "category_urlname": "marketing",
            "lft": "5",
            "rght": "6",
            "created_on": "2013-08-13 05:51:51",
            "status": "enable",
            "flag": "0"
        }
    }
}


$.getJSON("http://demourl/something?date=0000-00-00",function(data){

                                     $.each(data, function(i,value){
                                          alert(i);
                                          alert("city"+value.City.id);-->i cannot get the city or category values
                                            });




                                     });
4

1 回答 1

5

你说得不够多,但如果你试试这个,你会明白的

$.each(data, function(k, v){
    if('Date' in v) console.log('Date : ' + v.Date);
    if('Category' in v) console.log('Category : ' + v.Category.name);
    if('City' in v) console.log('City : ' + v.City.cityname);
});

检查这个例子。

于 2013-09-06T04:48:09.190 回答