How to fetch Data From JSON

前端 未结 6 1109
予麋鹿
予麋鹿 2021-01-27 15:01

I have following data from JSON

{
        \"MenuName\": \"starter dish\",
        \"SubMenu\": [
            \"pizza dish1\",
            \"pizza dish2\"
                


        
相关标签:
6条回答
  • 2021-01-27 15:10

    Just to throw in a quickie - read up on, and use GSON.

    For simple small jobs I find it is the best. Not the fastest running for complex, or long structures, but really quick on the dev side.

    Here's the link: google-gson

    0 讨论(0)
  • 2021-01-27 15:16

    Hi There in that senarerio you have to use

        JSONArray _submenu = object.getJSONArray("SubMenu");
            for (int i = 0; i < _submenu.length(); i++) {
                String text = _submenu.getString(i);
            }
            JSONArray _price = object.getJSONArray("Price");
            for (int i = 0; i < _price.length(); i++) {
                String text = _price.getString(i);
            }
    
    0 讨论(0)
  • 2021-01-27 15:19

    you can use this link for creating POGO class for your response. It will automatically generate class for your response.

    Use google GSON library to parse your response.

    or you can simply create a JSON Array or Objects to parse it. in your case it is JSON Object or JSON arrays.

    0 讨论(0)
  • 2021-01-27 15:20

    Submenu and Price is not String it is JSONArray so you will need to use for-loop to get all values from Submenu JSONArray as:

    JSONArray jsonsubmenu=yourjsonobject.getJSONArray("Submenu");
    
      for(int i=0;i < jsonsubmenu.length();i++){     
         // get all values from jsonsubmenu JSONArray..
          String str_value=jsonsubmenu.optString(i);
          ....
       }
    
    0 讨论(0)
  • 2021-01-27 15:21

    try this

              for (int i = 0; i < jsonArray.length(); i++) {
                 jsonArray.getJSONObject(i).getInt("SubMenu");
                 }
    
    0 讨论(0)
  • 2021-01-27 15:28

    You can retrieve array values and store it in string[] and use them as per your need. i.e., as follows..

    try {
                JSONObject jObject = new JSONObject(jsonString);
                JSONArray jSubmenu = jObject.getJSONArray("SubMenu");
                subMenu = new String[jSubmenu.length()];
                for (int i = 0; i < subMenu.length; i++) {
                    subMenu[i] = jSubmenu.getString(i);
                }
                JSONArray jPrice = jObject.getJSONArray("Price");
                price = new String[jPrice.length()];
                for (int i = 0; i < price.length; i++) {
                    price[i] = jPrice.getString(i);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
            }
    
    0 讨论(0)
提交回复
热议问题