-2
{
 item:[
     {
      item_id: 1
      add_on:[
             {
             name: Thin Crust
             },

             {
             name: Extra Cheese
             },
             
             {
             name: Extra Sauce
             }

     }]
}

I want to get these names and place them into one TextView

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • 1
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – a_local_nobody Oct 06 '21 at 08:28

2 Answers2

0

First of all include the JSON Library in your project.

Then access your JSON Object like this:

JSONObject jsonObject = new JSONObject(jsonString); // here is your JSON as String

// Get your Json Array of item

JSONArray itemArray = jsonObject.getJSONArray("item");

// Get your first Element from your array 

JSONObject firstItem = itemArray.getJSONObject(0);

// Then get your add_on array

JSONArray itemArray = firstItem.getJSONArray("add_on");

// After you get your array Get your second object which is { name: Extra Cheese}

JSONObject secondObject = itemArray.getJSONObject(1);

// Then you get your itemName this way:

String itemName = secondObject.getString("name");
Renis1235
  • 4,116
  • 3
  • 15
  • 27
  • I am using volley request and I have gotten into add_on and I am showing the result into a ListView but if there are multiple names in add_on, it makes a new List for that actually I need to parse like follow example: item name: Burger Add On: Extra Cheese, Extra sauce, Wheat bun – Salman Afzal Oct 06 '21 at 07:45
0

First of all you need to correct to JSON input. It's not valid JSON. Correct JSON should be :

{
    "item": [{
        "item_id": 1,
        "add_on": [{
                "name": "Thin Crust"
            },

            {
                "name": "Extra Cheese"
            },

            {
                "name": "Extra Sauce"
            }
        ]
    }]
}

After that with Jackon library you can get the data from Json in POJO as below:

POJO(s):

class Items {

  @JsonProperty("item")
  public List<Item> item;
}

class Item {

  @JsonProperty("item_id")
  public int itemId;

  @JsonProperty("add_on")
  public List<Name> addOn;
}

class Name {

  @JsonProperty("name")
  public String name;
}

Conversion with Jackson:

public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
    String json = "{\r\n" + 
        "  \"item\": [{\r\n" + 
        "    \"item_id\": 1,\r\n" + 
        "    \"add_on\": [{\r\n" + 
        "        \"name\": \"Thin Crust\"\r\n" + 
        "      },\r\n" + 
        "\r\n" + 
        "      {\r\n" + 
        "        \"name\": \"Extra Cheese\"\r\n" + 
        "      },\r\n" + 
        "\r\n" + 
        "      {\r\n" + 
        "        \"name\": \"Extra Sauce\"\r\n" + 
        "      }\r\n" + 
        "    ]\r\n" + 
        "  }]\r\n" + 
        "}";
    
    Items item = (new ObjectMapper()).readValue(json, Items.class);
    System.out.println(item);

  }

Now from this object structure you can get names.

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47
  • Sorry I used StackOverflow for the first time so couldn't ask the question correctly, secondly, I am using volley to parse all these all I want to display data like name: Burger Add on : sauce, cheese, bun etc – Salman Afzal Oct 06 '21 at 08:32
  • 1
    I will give this method a try, Thankyou for your help – Salman Afzal Oct 06 '21 at 08:32