2017-06-13 20 views
-2

json apiからデータを取得しようとしています。何度も試してみますが、結果は失敗します。ここでは、JSONパターンは次のとおりです。3レベルのjson APIからデータを取得する方法

{ 
"All": [{ 
     "MainCategory": { 
      "AllData": [{ 
        "SubCategory": { 
         "AllData": [{ 

           "MainBigThumb": "rsz_0_800_CHO1.jpg", 
           "MainImage": "rsz_0_400_CHO1.jpg", 
           "MainSmallImages": "rsz_0_100_CHO1.jpg" 
          }, { 

           "MainBigThumb": "rsz_0_800_CHO100.jpg", 
           "MainImage": "rsz_0_400_CHO100.jpg", 
           "MainSmallImages": "rsz_0_100_CHO100.jpg" 

          }, 
          { 
           "MainBigThumb": "rsz_0_800_CHO101.jpg", 
           "MainImage": "rsz_0_400_CHO101.jpg", 
           "MainSmallImages": "rsz_0_100_CHO101.jpg" 
          } 
         ], 
         "Attribute_Name": "Chocolate", 
         "Attribute_Value": "123", 
         "StockKeepingunit": null 
        } 
       }, 
       { 
        "SubCategory": { 
         "AllData": [{ 

           "MainBigThumb": "rsz_0_800_CRI100.jpg", 
           "MainImage": "rsz_0_400_CRI100.jpg", 
           "MainSmallImages": "rsz_0_100_CRI100.jpg" 
          }, 
          { 

           "MainBigThumb": "rsz_0_800_CRI101.jpg", 
           "MainImage": "rsz_0_400_CRI101.jpg", 
           "MainSmallImages": "rsz_0_100_CRI101.jpg" 
          } 
         ], 
         "Attribute_Name": "Crisps", 
         "Attribute_Value": "124", 
         "StockKeepingunit": null 
        } 
       } 
      ], 
      "Attribute_Name": "Confectionery", 
      "Attribute_Value": "122", 
      "StockKeepingunit": null 
     } 
    }, 

    { 
     "MainCategory": { 
      "AllData": [{ 
       "SubCategory": { 
        "AllData": [{ 

         "MainBigThumb": "rsz_0_800_YP100.jpg", 
         "MainImage": "rsz_0_400_YP100.jpg", 
         "MainSmallImages": "rsz_0_100_YP100.jpg" 

        }], 
        "Attribute_Name": "Sub-Category", 
        "Attribute_Value": "163", 
        "StockKeepingunit": null 
       } 
      }], 
      "Attribute_Name": "Your Category", 
      "Attribute_Value": "162", 
      "StockKeepingunit": null 
     } 
    } 

], 
"status": { 
    "message": "success", 
    "result": 1 
}} 

データ形式は、あなたがそれのために改造を使用して、それがより多くのより簡単になる、Gsonにあなたの全体のJSONデータを変換することができます

 
1. MainCategory1 
     1.1 Subcategory1 
      1.1.1 Product1 
      1.1.2 Product2 

     1.2 Subcategory2 
      1.2.1 Product1 
      1.2.2 Product2 

2. MainCategory2 
     2.1 Subcategory1 
      2.1.1 Product1 
      2.1.2 Product2 

+0

試したことを示してください。 – lelloman

+0

は、apiからデータベースにデータを保存したいだけです。 –

+0

[もっと速い回答を得るために、どのような状況で私の質問に「緊急」やその他の類似のフレーズを追加することができますか?](// meta.stackoverflow.com/q/326569) - 要約はこれがボランティアに対処する理想的な方法であり、おそらく回答を得ることは非生産的です。これをあなたの質問に追加しないでください。 – halfer

答えて

0

私はここで私の問題を解決しました。

JSONObject jsonObj = new JSONObject(json); 
      JSONArray AllJsonArray = jsonObj.getJSONArray("All"); 
      for (int i = 0; i < AllJsonArray.length(); i++) { 

       JSONObject AllJsonObject = AllJsonArray.getJSONObject(i); 

       JSONObject mainCategoryJsonObject = AllJsonObject.getJSONObject("MainCategory"); 
       String mainCategoryName = mainCategoryJsonObject.getString("Attribute_Name"); 
       String mainCategoryValue= mainCategoryJsonObject.getString("Attribute_Value"); 
       JSONArray AllData1JsonArray = mainCategoryJsonObject.getJSONArray("AllData"); 

       for (int j = 0; j < AllData1JsonArray.length(); j++) { 
        JSONObject AllData1JsonObject = AllData1JsonArray.getJSONObject(j); 
        JSONObject subCategoryJsonObject = AllData1JsonObject.getJSONObject("SubCategory"); 
        String subCategoryName = subCategoryJsonObject.getString("Attribute_Name"); 
        String subCategoryValue = subCategoryJsonObject.getString("Attribute_Value"); 
        JSONArray AllData2JsonArray = subCategoryJsonObject.getJSONArray("AllData"); 

        for (int k = 0; k < AllData2JsonArray.length(); k++) { 
         JSONObject AllData2JsonObject = AllData2JsonArray.getJSONObject(k); 
         String MainBigThumb = AllData2JsonObject.getString("MainBigThumb"); 
         String MainImage = AllData2JsonObject.getString("MainImage"); 
         String MainSmallImages = AllData2JsonObject.getString("MainSmallImages"); 

         } 

       } 

      } 
0

のようなものです。

あなたのAPIの応答をコピーして、GSON * Unticking useDoubleNumbers * Unticking AllowAditionプロパティにJSON *注釈スタイルに

をソースタイプをクリックすることで

thisサイト*を通して、あなたのBeanクラスを作成し、すべての最初は、コピーあなたの活動の

その後、プロジェクトにこれらのファイル

Retrofit retrofit = new Retrofit.Builder() 
    .baseUrl(BASE_URL) 
    .addConverterFactory(GsonConverterFactory.create()) 
    .build(); 
APIServiceInterface apiController = retrofit.create(APIServiceInterface.class); 
Call<Bean> result = apiController.studentLogin(username,password); 

result.enqueue(new Callback<Bean>() { 
@Override 
public void onResponse(Call<Bean> call, Response<Bean> response) { 
    // you will get the reponse in the response parameter 

} 

@Override 
public void onFailure(Call<Bean> call, Throwable t) { 

} 
}); 

ここで、APIServiceInterfaceはインターフェイスファイルです。

0

それは簡単ですが、あなたはJsonを解析中{}[],については慎重でなければなりません。私はあなたのためにコードを解析するのではなく、アイデアを与えます。

Intitial Json Response Object 

Exract JsonArray named as "All" from above object 

Get first Position Of above array, you will get an JsonObject 

Extract a JsonObject from above object with name "MainCategory" 

Exract JsonArray named as "AllData" from above object 

Get first Position Of above array, you will get an JsonObject 

Extract a JsonObject from above object with name "SubCategory" 

Exract JsonArray named as "AllData" from above object 

Get first Position Of above array, you will get an JsonObject 

Containing following data, 

           "MainBigThumb": "rsz_0_800_CHO1.jpg", 
           "MainImage": "rsz_0_400_CHO1.jpg", 
           "MainSmallImages": "rsz_0_100_CHO1.jpg" 
関連する問題