2017-10-31 9 views
0

Volleyライブラリを使用して、ネストされたJSON配列内の情報にアクセスしようとしています。具体的には、 'lineStatuses'内から 'statusSeverityDescription'のプロパティを取得しようとしています。私は検索し、私が見た多くの解決策を試しました。ちょうど私は必要なデータを得ることができません。以下のコードとJSONをご覧ください。Volleyライブラリを使用してJSON配列のネストされたアイテムにアクセス

コード

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    bob = (TextView) findViewById(R.id.bob); 
    tempTextView = (TextView) findViewById(R.id.tempTextView); 

    String url = "https://api.tfl.gov.uk/Line/victoria/Status"; 

    JsonArrayRequest jsArrayRequest = new JsonArrayRequest 
      (Request.Method.GET, url, null, new Response.Listener<JSONArray>() { 

       @Override 
       public void onResponse(JSONArray response) { 

        try{ 
         // Loop through the array elements 
         for(int i=0;i<response.length();i++){ 
          // Get current json object 
          JSONObject line = response.getJSONObject(i); 

          // Get the current line (json object) data 
          String lineName = line.getString("name"); 

          String lineStatus = line.getString("lineStatuses"); 

          // Display the formatted json data in text view 

          tempTextView.setText(lineName); 

          bob.setText(lineStatus); 

          Log.v("status", "Response: " + lineStatus); 

         } 
        }catch (JSONException e){ 
         e.printStackTrace(); 
        } 
       } 
      }, 






      new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("TAG", "Error response:", error); 
       } 
      }); 

JSON

[ 
{ 
    "$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities", 
    "id": "northern", 
    "name": "Northern", 
    "modeName": "tube", 
    "disruptions": [], 
    "created": "2017-10-31T10:48:22.99Z", 
    "modified": "2017-10-31T10:48:22.99Z", 
    "lineStatuses": [ 
     { 
      "$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities", 
      "id": 0, 
      "statusSeverity": 10, 
      "statusSeverityDescription": "Good Service", 
      "created": "0001-01-01T00:00:00", 
      "validityPeriods": [] 
     } 
    ], 
    "routeSections": [], 
    "serviceTypes": [ 
     { 
      "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities", 
      "name": "Regular", 
      "uri": "/Line/Route?ids=Northern&serviceTypes=Regular" 
     }, 
     { 
      "$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities", 
      "name": "Night", 
      "uri": "/Line/Route?ids=Northern&serviceTypes=Night" 
     } 
    ], 
    "crowding": { 
     "$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities" 
    } 
} 
    ] 

見ていただきありがとうございます。

答えて

1

JSONによれば、あなたの "lineStatuses"は配列です。だからあなたはあなたの "行"オブジェクトから配列を取得する必要があります。その後、個々の "statusSeverityDescription"値を取得するためにその配列をステップ実行します。

だから、あなたのコードでは他の問題を持っていないと仮定すると、あなたのforループ

JSONArray arrayStatus = line.getJSONArray("lineStatuses"); 
int len = arrayStatus.length(); 
    for (int j = 0; j < len; j++) { 
     JSONObject o = arrayStatus.getJSONObject(j); 
     String statusSeverityDescription = o.optString("statusSeverityDescription", ""); 
    } 

にあなたのコードにこれを追加する上記のコードは動作するはずです(あなたはすべてのエラーを言及しませんでした)。

+0

完璧!どうもありがとう.. –

関連する問題