处理响应中的键值对

时间:2016-08-29 10:05:45

标签: javascript angularjs

我有以下回复。

response: {
      "json": {
         "response":{ 
           "servicetype":"", 
           "functiontype":"", 
           "statuscode":"0", 
           "statusmessage":"Success", 
           "data":             
               [
                 {
                   "graphtype":"headcountgraph", 
                   "xlabel":"state", 
                   "ylabel":"count",
                   "s1":"contact", 
                   "s2":"User",
                   "data": 
                        [
                             "Total Contacts: 1 Users: 36",
                             [
                               { 
                                 "x":"India", 
                                 "s1":"3", 
                                 "s2":"3", 
                                 "lat":"22",       
                                 "long":"77" 
                                }
                             ]
                      ]
                 }

               ]
          }
      }
  };

我需要分配$scope.state = India

如何获取?我试过给$scope.state = json.response.data[0].data[1][0].x,它返回未定义的数据。

事情是它有钥匙和入口。我真的不能从代码中获取条目值。

我这样给了代码

   $.each(data, function(key, entry) { x.push(parseInt(entry.x)});

但它在我的控制台中给了我[india]的值。

2 个答案:

答案 0 :(得分:0)

你非常接近。

json.response.data[0].data[1]

返回以下对象:

{"x":"India","s1":"3","s2":"3","lat":"22","long":"77"}

您可以使用x键过滤:

json.response.data[0].data.filter(o => o[0].x == "India")[0];

答案 1 :(得分:0)

尝试提供$scope.state = response.json.response.data[0].data[1][0].x ;

您的语句不是从根response对象开始的。

相关问题