Arun Yadav
Arun Yadav

Reputation: 23

Parse String array out of JSON Array

I am having this in JSONArray ary2;

JSON string is :

[
      {
        "item_id": "1",
        "Head_item_id": "1",
        "major_item_id": "1",
        "Quantity": "10",
        "selling_prize": "20",
        "MRP": "90",
        "title": "JK Lakshmi PPC Cement",
        "SKU": "B2WBUICEM2"
      },
      {
        "item_id": "2",
        "Head_item_id": "1",
        "major_item_id": "1",
        "Quantity": "10",
        "selling_prize": "30",
        "MRP": "80",
        "title": "JK Lakshmi PPC Cement",
        "SKU": "B2WBUICEM2"
      },
      {
        "item_id": "3",
        "Head_item_id": "1",
        "major_item_id": "1",
        "Quantity": "10",
        "selling_prize": "10",
        "MRP": "70",
        "title": "Shree Ultra OPC 43 Grade Cement",
        "SKU": "B2WBUICEM5"
      }
      ]

I want to get String array for each attribute , like :

String[] item_id;
String[] Head_item_id; 

etc. unable to get it. Please help.

Upvotes: 1

Views: 75

Answers (2)

Pallav
Pallav

Reputation: 165

You need to follow these steps: -> Create POJO for your JSON data and provite getters and setters for each attribute. -> Parse your JSON data using GSON -> get the desired attribute(using getters) and store it in the respective string array

Upvotes: 1

Amy
Amy

Reputation: 4032

Try This:

JSONArray arr = new JSONArray(yourJSONresponse);
String[] item_id=new String[array.length()];
String[] Head_item_id=new String[arr .length()];
for(int i = 0; i < arr.length(); i++){
    item_id[i]=arr.getJSONObject(i).getString("item_id");
    Head_item_id[i]=arr.getJSONObject(i).getString("Head_item_id");
}

Upvotes: 1

Related Questions