2013-05-16 100 views
1

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

我正在使用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" 
      } 
     } 
    ] 
} 

感谢。

+4

当你输入你的问题时,右边有这个方便的**如何格式**框。值得一读。我这次为你格式化了一些东西(既把JSON标记为“代码”,也通过http://jsonlint.com运行它,使其首先可读)。 –

+0

谢谢,不知道。 –

回答

8

您正在寻找的价值,是在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); 

在这里看到:

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

+0

它的工作原理!这非常有帮助。 –