2017-10-07 10 views
1

これはJSONですが、colorArrayから結果(Values)を取得しましたが、shapeArrayから結果を取得できません。 shapeArrayから結果を得るには? shapeArrayの値を取得するには?これらのタイプのネストされたJSONレスポンスを管理するにはどうすればよいですか?アンドロイドの入れ子JSONレスポンスにアクセスする方法は?

[ 
    { 

      "colorArray":[ 
        { 
        "colorName":"red", 
        "hexValue":"#f00" 
       }, 
        { 
        "colorName":"green", 
        "hexValue":"#0f0" 
       }, 
        { 
        "colorName":"blue", 
        "hexValue":"#00f" 
       }, 
        { 
        "colorName":"cyan", 
        "hexValue":"#0ff" 
       }, 
        { 
        "colorName":"magenta", 
        "hexValue":"#f0f" 
       }, 
        { 
        "colorName":"yellow", 
        "hexValue":"#ff0" 
       } 
      ] 
     }, 
     { 
      "shapeArray":[ 
       { 
        "shapeName":"circle" 
       }, 
       { 
        "shapeName":"square" 
       }, 
       { 
        "shapeName":"triangle" 
       }, 
       { 
        "shapeName":"hexagon" 
       } 
      ] 
     } 
    ] 

コード

for (int i = 0; i < jsonArray.length(); i++) 
{ jsonObject = jsonArray.getJSONObject(i); 
JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
for (int j=0;j<jsonColorArray.length();j++) 
    { JSONObject colorObj = jsonColorArray.getJSONObject(j); 
    String colorName = colorObj.getString("colorName"); 
    String hexValue = colorObj.getString("hexValue"); 
    } 
} 
+0

マイコード:のために(INT i = 0; iは++; iはjsonArray.length()<){ jsonObject = jsonArray.getJSONObject(i)を、 JSONArray jsonColorArray = jsonObject.getJSONArray( "colorArray"); for(int j = 0; j suryac

+0

あなたはどのような問題に直面していますか? –

+0

シェイプ配列の値を取得できません。どのようにJson配列からshapeArray値を取得するには? – suryac

答えて

1

まずは非常に悪いjsonデザイン。第2の

JSONArray jsonShapeArray = jsonObject.getJSONArray( "shapeArray");

はこのようなオブジェクトが存在しないため、エラーが発生します。これを使用してみてください:

JSONArray jsonArray = parent.getJSONObject(1).getJSONArray( "shapeArray");

ここで、parentはルートjsonArrayです。

+0

ありがとうございます。それは私の問題を解決しました。ありがとうございましたadnaan.zohran – suryac

0
JSONArray jsonColorArray = jsonArray.getJSONObject(0).getJSONArray("colorArray"); 
    JSONArray jsonShapeArray = jsonArray.getJSONObject(1).getJSONArray("shapeArray"); 
for (int j=0;j<jsonColorArray.length();j++) 
{ 
    JSONObject colorObj = jsonColorArray.getJSONObject(j); 
    String colorName = colorObj.getString("colorName"); 
    String hexValue = colorObj.getString("hexValue"); 
} 
for (int k=0;k<jsonShapeArray.length();k++) 
{ 
    JSONObject shapeObj = jsonShapeArray.getJSONObject(k); 
    String shapeName = shapeObj.getString("shapeName"); 

} 

希望何か助けが必要な場合は...あなたは

+0

ありがとう、しかしJSONArray jsonShapeArray = jsonObject.getJSONArray( "shapeArray");例外を与えます – suryac

0

jsonObject.getJSONArray()を求めることができ、このヘルプは常に2つのJSONArrayのでNullPointerExceptionを与えます名前は です。

あなたはJSONArrayあなたが例外を処理することができます

このような
 try { 
      JSONArray jsonArray = new JSONArray(jsonText); 
      for (int i = 0; i < jsonArray.length(); i++){ 
       JSONObject jsonObject = (JSONObject) jsonArray.get(i); 
       try { 
        JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
        for (int j = 0; j < jsonColorArray.length(); j++) { 
         JSONObject jsonColorObject = (JSONObject) jsonColorArray.get(j); 
         String colorName = jsonColorObject.getString("colorName"); 
         String hexValue = jsonColorObject.getString("hexValue"); 
        } 
       } catch (JSONException ex) { 

       } 

       try { 
        JSONArray jsonShapeArray = jsonObject.getJSONArray("shapeArray"); 
        for (int k = 0; k < jsonShapeArray.length(); k++) { 
         JSONObject jsonShapeObject = (JSONObject) jsonShapeArray.get(k); 
         String shapeName = jsonShapeObject.getString("shapeName"); 
        } 
       } catch (JSONException ex) { 

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

を反復しているので、それはあなたを助けることを願っています。

+0

少し変更してコードを修正しました。ありがとうございます – suryac

0
try { 
       if (jsonResult != null) { 
        JSONArray jsonArray = null; 
        JSONObject jsonObject = null; 
        try { 
         jsonArray = new JSONArray(jsonResult); 

         for (int i = 0; i < jsonArray.length(); i++) { 
          jsonObject = jsonArray.getJSONObject(i); 
          JSONArray jsonColorArray = jsonObject.getJSONArray("colorArray"); 
          for (int j=0;j<jsonColorArray.length();j++){ 

           JSONObject colorObj = jsonColorArray.getJSONObject(j); 
           String colorName = colorObj.getString("colorName"); 
           String hexValue = colorObj.getString("hexValue"); 
          } 

          JSONArray shapeArray = jsonArray.getJSONObject(1).getJSONArray("shapeArray"); 
          JSONObject shapeObject = jsonObject.getJSONArray("shapes").getJSONObject(i); 

          for (int k = 0; k < shapeArray.length(); k++) { 
           JSONObject jsonShapeObject = (JSONObject) shapeArray.get(k); 
           String shapeName = jsonShapeObject.getString("shapeName"); 
          } 
         } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      } catch (Exception e) { 
       e.printStackTrace(); } 
関連する問題