JSON 获取子值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16586387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:47:05  来源:igfitidea点击:

JSON get child value

javascriptjsonbackbone.js

提问by Arià

I got this JSON. I want to get the value of "resource_uri" that is "/api/v1/client/2/".

我得到了这个 JSON。我想获得“resource_uri”的值,即“/api/v1/client/2/”。

I'm using Backbone/javascript.

我正在使用 Backbone/javascript。

json['resource_uri'] does not work.

json['resource_uri'] 不起作用。

The JSON is:

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"
            }
        }
    ]
}

Thanks in advance.

提前致谢。

回答by MasNotsram

The value you're looking for, is in the objects array in the JSON, but it's in an object which is the first in an array:

您要查找的值位于 JSON 中的对象数组中,但它位于数组中的第一个对象中:

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);

See here:

看这里:

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

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