Iterating through json array with appended json string in android after json as response from url using volley Iterating through json array with appended json string in android after json as response from url using volley json json

Iterating through json array with appended json string in android after json as response from url using volley


Here is the working solution: Using GOOGLE GSON (Open source jar)

import java.io.IOException;import com.google.gson.Gson;import com.google.gson.GsonBuilder;     public class JsonToJava {            public static void main(String[] args) throws IOException {                try{                    String json = "<YOUR_JSON>";                    Gson gson = new GsonBuilder().create();                    VendorInfo vInfo = gson.fromJson(json, VendorInfo.class);                           System.out.println(vInfo.getVendorName());                              } catch(Exception ex) {                    ex.printStackTrace();                }            }        }

Create classes for Vendor and Product

public class Vendor {    public String vendor_name;    public String vendor_description;    public String vendor_slug;    public String vendor_logo;    public String contact_number;    public String getName() {        return vendor_name;    }}public class Product {    public String name;    public long price;    public String image;    public String getName() {        return name;    }}

VendorInfo is the JSON object form:

import java.util.Map;public class VendorInfo {    public Vendor[] vendor;    public Map<Integer, Product> products;    public String getVendorName() {        return vendor[0].getName();    }    public Product getProduct() {        System.out.println(products.size());        return products.get(25);    }}

You can add your getters for Vendor, Product and VendorInfo. You are done! You will get all the data.

Output of JsonToJava:

Tapan Moharana


To get your products data , you need to use Iterator

   JSONObject jProducts = jsonObject            .optJSONObject("products");    try {        if (jProducts                .length() > 0) {            Iterator<String> p_keys = jProducts                    .keys();            while (p_keys                    .hasNext()) {                String keyProduct = p_keys                        .next();                JSONObject jP = jProducts                        .optJSONObject(keyProduct);                if (jP != null) {                    Log.e("Products",                            jP.toString());                }            }        }    } catch (Exception e) { // TODO:        // handle        // exception    }


you can try with this

JSONObject jsono = null;    try {        jsono = new JSONObject(response);        JSONObject productObject = jsono.getJSONObject("products");        Iterator<String> keys = productObject.keys();        while (keys.hasNext())        {            // get the key            String key = keys.next();            // get the value            JSONObject value = productObject.getJSONObject(key);            //get seprate objects            String name = value.getString("name");            String image = value.getString("image");            Log.i(TAG,name+"-"+image);         }        }        catch (JSONException e) {        e.printStackTrace();    }