JSON获取子值

时间:2013-05-16 11:36:37

标签: javascript json backbone.js

我有这个JSON。我想得到“resource_uri”的值是“/ api / v1 / client / 2 /”。

我正在使用Backbone / javascript。

json ['resource_uri']不起作用。

JSON是:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 1
    },
    "objects": [
        {
            "id": 2,
            "nom": "",
            "resource_uri": "/api/v1/client/2/",
            "telefon": "",
            "user": {
                "date_joined": "2013-05-15T12:28:40",
                "first_name": "",
                "id": 51,
                "is_active": true,
                "is_staff": false,
                "last_login": "2013-05-16T06:20:43",
                "last_name": "",
                "resource_uri": "/api/v1/user/51/",
                "username": "gli"
            }
        }
    ]
}

提前致谢。

1 个答案:

答案 0 :(得分:8)

您正在寻找的值是在JSON中的objects数组中,但它位于数组中的第一个对象中:

var jsonVar = {"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 1}, "objects": [{"id": 2, "nom": "", "resource_uri": "/api/v1/client/2/", "telefon": "", "user": {"date_joined": "2013-05-15T12:28:40", "first_name": "", "id": 51, "is_active": true, "is_staff": false, "last_login": "2013-05-16T06:20:43", "last_name": "", "resource_uri": "/api/v1/user/51/", "username": "gli"}}]}

alert(jsonVar.objects[0].resource_uri);

见这里:

http://jsfiddle.net/SpAm/tnWmL/