最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON get child value - Stack Overflow

programmeradmin3浏览0评论

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

I'm using Backbone/javascript.

json['resource_uri'] does not work.

The JSON is:

{
    "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.

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

I'm using Backbone/javascript.

json['resource_uri'] does not work.

The JSON is:

{
    "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.

Share Improve this question edited May 16, 2013 at 11:39 T.J. Crowder 1.1m199 gold badges2k silver badges1.9k bronze badges asked May 16, 2013 at 11:36 AriàArià 9142 gold badges10 silver badges16 bronze badges 2
  • 4 When you were typing your question, there was this handy How to Format box to the right. Well worth a read. I've formatted things for you this time (both marking up the JSON as "code" and also running it through jsonlint. to make it readable first). – T.J. Crowder Commented May 16, 2013 at 11:39
  • Thanks, didn't know it. – Arià Commented May 17, 2013 at 8:26
Add a ment  | 

1 Answer 1

Reset to default 10

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:

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/SpAm/tnWmL/

发布评论

评论列表(0)

  1. 暂无评论