Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON get child value

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.

like image 302
Arià Avatar asked Dec 27 '22 03:12

Arià


1 Answers

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

like image 152
MasNotsram Avatar answered Jan 09 '23 02:01

MasNotsram