2016-08-29 7 views
0

私は以下のような対応をしています。応答のキー値ペアの処理

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

を与えたが、それは私に私のコンソールで値[インド]を与えます。

+0

あなたは全体の応答から "インド"だけが必要ですか? – Razzka

+0

はい。 $ scope.state = Indiaに割り当てる必要があります –

+0

この値はJSONオブジェクトを介して取得できます: 'JSON.parse().json.response.data [0] .data [1] [0] .x' – Razzka

答えて

0

root responseオブジェクト。

+0

お返事をありがとうございます。本当に役に立ちました。 –

0

あなたは非常に近くでした。

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

すると、次のオブジェクトを返します。

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

あなたがしてxキーでフィルタリングすることができます:あなたの文から起動しない$scope.state = response.json.response.data[0].data[1][0].x ;

を与えてみ

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