2016-07-21 9 views
1

配列内のjson配列を解析する際に問題に直面していますが、私は新しいアンドロイドです。GSONを使って配列内のjson配列を解析する

{ 
"menu": [ 
    { 
     "Soups": [ 
      { 
       "name": "Safed Tamatar aur Tulsi ", 
       "price": 150, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Dum Murg", 
       "price": 168, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Murg Zaffrani", 
       "price": 168, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Tomato Dhania", 
       "price": 150, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      } 
     ] 
    }, 
    { 
     "Starters": [ 
      { 
       "name": "Achari Paneer tikka", 
       "price": 347, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Paneer Tikka", 
       "price": 347, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Murg Tikka", 
       "price": 393, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Tandoori Murg (Half)", 
       "price": 410, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Tandoori Murg (Full)", 
       "price": 851, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Macchi Tikka", 
       "price": 626, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      } 
     ] 
    }, 
    { 
     "Main Course": [ 
      { 
       "name": "Jeera Aloo", 
       "price": 275, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Paneer Tikka Masala", 
       "price": 392, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Palak Paneer", 
       "price": 370, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Murg Kadhai", 
       "price": 428, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Murg Tikka Masala", 
       "price": 428, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Palak Gosht", 
       "price": 455, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      }, 
      { 
       "name": "Macchi Kadhai", 
       "price": 545, 
       "description": "", 
       "veg": false, 
       "spicy": null 
      } 
     ] 
    }, 
    { 
     "Rice": [] 
    }, 
    { 
     "Breads": [] 
    }, 
    { 
     "Accompaniments": [] 
    }, 
    { 
     "Beverages": [] 
    }, 
    { 
     "Dessert": [] 
    }, 
    { 
     "Signature Mocktails": [] 
    }, 
    { 
     "Wraps": [] 
    }, 
    { 
     "Lentils": [ 
      { 
       "name": "Dal Tadka", 
       "price": 226, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Dal Kolhapuri", 
       "price": 226, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      }, 
      { 
       "name": "Dal Makhani", 
       "price": 275, 
       "description": "", 
       "veg": true, 
       "spicy": null 
      } 
     ] 
    } 
    ], 
    "name": "Zaffran" 
} 

このファイルを解析したいのですが、インターネット上にこのファイルをGSONで解析するコードはありません。 私は次のコードを使用:

JSONObject jsonObject = (JSONObject) new JSONTokener(response).nextValue(); 
     JSONArray jsonArray = jsonObject.getJSONArray("menu"); 
     String jsonString = jsonArray.toString(); 
     Type type = new TypeToken<Map<String, String>>() { 
     }.getType(); 
     Map<String, String> myMap = new Gson().fromJson(jsonString, type); 
     for (Map.Entry<String, String> entry : myMap.entrySet()) { 
      key = entry.getKey(); 
      value = entry.getValue(); 
      // do stuff 
     } 
     responseView.setText(value); 

をしかし、それは文字列が見つかった配列を期待エラーが発生します。助けてください

+0

これはyou.httpsを助ける://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html&http://blog.nkdroidsolutions.com/how-to -parsing-json-array-gson-in-android-tutorial/ –

+0

助けてくれてありがとうございますが、あなたのリンクの1つを試しましたが、どちらもうまくいかないようですが、次のエラーが表示されます:BEGIN_ARRAY行1の列10- @SuhasB –

答えて

0

以下のコードを使用して、完全なjsonデータを解析します。

try { 
     JSONObject jsonObject = new JSONObject(jsonArray); 

     JSONArray jsonArray1 = jsonObject.getJSONArray("menu"); 
     Menu menu = new Menu(); 
     if (jsonObject.has("name")) { 
      menu.setName(jsonObject.getString("name")); 
     } else { 
      menu.setName(""); 
     } 
     List<MenuHeaders> menuHeadersList = new ArrayList<>(); 
     for (int i = 0; i < jsonArray1.length(); i++) { 
      JSONObject jsonObject1 = jsonArray1.getJSONObject(i); 
      MenuHeaders menuHeaders = new MenuHeaders(); 
      //Creating list to save all prices and item names 
      List<ItemDetails> list = new ArrayList<>(); 
      JSONArray subHeaderJsonArray = null; 

      if (jsonObject1.has("Soups")) { 
       menuHeaders.setTitle("Soups"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Soups"); 
      } 
      if (jsonObject1.has("Starters")) { 
       menuHeaders.setTitle("Starters"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Starters"); 

      } 
      if (jsonObject1.has("Main Course")) { 
       menuHeaders.setTitle("Main Course"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Main Course"); 
      } 
      if (jsonObject1.has("Rice")) { 
       menuHeaders.setTitle("Rice"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Rice"); 
      } 
      if (jsonObject1.has("Breads")) { 
       menuHeaders.setTitle("Breads"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Breads"); 
      } 
      if (jsonObject1.has("Accompaniments")) { 
       menuHeaders.setTitle("Accompaniments"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Accompaniments"); 
      } 
      //Add all other sub menu items to menu object 

      if (jsonObject1.has("Lentils")) { 
       menuHeaders.setTitle("Lentils"); 
       subHeaderJsonArray = jsonObject1.getJSONArray("Lentils"); 
      } 
      if (subHeaderJsonArray != null) { 
       for (int index1 = 0; index1 < subHeaderJsonArray.length(); index1++) { 
        JSONObject subHeaderJsonObject = subHeaderJsonArray.getJSONObject(i); 
        ItemDetails itemDetails = new ItemDetails(); 
        itemDetails.setName(subHeaderJsonObject.getString("name")); 
        itemDetails.setPrice(subHeaderJsonObject.getInt("price")); 
        itemDetails.setDescription(subHeaderJsonObject.getString("description")); 
        itemDetails.setVeg(subHeaderJsonObject.getBoolean("veg")); 
        itemDetails.setSpicy(subHeaderJsonObject.getString("spicy")); 
        list.add(itemDetails); 
       } 
       menuHeaders.setList(list); 
      } 
      menuHeadersList.add(menuHeaders); 
      menu.setMenuHeaders(menuHeadersList); 
     } 

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

えええええええええええええええええええええええええええええええええええええええええええええ問題は、内部配列の名前を知らずにこのデータを配列形式で解析する必要があるということです。例えば、スープ、スターターなど –

+0

No.:文字列を知っていなくても、解析することはできません。とにかくあなたはタイトルとデータを得ています。そうですか?次に、このコードを解析して解析しないでください。 –

+0

私を助けてくれてありがとう.. –

0

これは完璧な答えではありませんが、私はあなたにネストされたjsonを解析する形式を表示しようとしました。これによりjson配列 "Menu"とその内部配列も得られます。

必要に応じて変更してください。

JSONObject jsonMenu = mJsonObject.optJSONObject("menu"); 

       JSONArray jsnSoupsarr = jsonMenu.optJSONArray("Soups"); 
       for (int k = 0; k < jsnSoupsarr.length(); k++) { 
        JSONObject jsnSoup = jsnMSouparr.optJSONObject(k); 
        //here get the inner fields of "Soup" like "price","name" etc. 
       } 

       JSONArray startersJson = jsnMenu.optJSONArray("Starters"); 
       for (int k = 0; k < starterJson.length(); k++) { 
        JSONObject objstarter = starterJson.optJSONObject(k); 
       } 

       JSONArray mainCourseJson = jsnMenu.optJSONArray("Main Course"); 
       for (int k = 0; k < mainCourseJson.length(); k++) { 
        JSONObject objmainCourse = mainCourseJson.optJSONObject(k);} 

希望すると助かります!

+0

で動作しますが、問題はこのメニューが静的なエンティティではないということです。基本的に、メニューにはレストランがあります。これらのすべてを持っている必要はありません、これらの料理の別の種類があるかもしれないので、私は内部配列の名前を知らずに配列形式でこのデータを解析する必要があります... - @ Suhas B –

+0

あなたは位置によってそれを得ることができる名前と解析したいと思う。 ** Menu **配列のtake ** forループ**と同じように、** soups **と** starters **の内部配列のようにforループで 'jsonMenu.optJSONArray(i)'を使用します。 – Bee

+0

ありがとうございました。本当にありがとうございました。 –

関連する問題