9

I have to parse the below nested Json array's data into my application. I am confused how to get the values out of it.

  {
            "prodCat_list": [
                {
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
{
                    "prods": [
                        {
                            "cat_id": "9",
                            "position": "1",
                            "sku": "wwww345"

                        },
                        {
                            "cat_id": "9",
                            "position": "2",
                            "sku": "coof23"

                        },
                        {
                            "cat_id": "9",
                            "position": "3",
                            "sku": "dde45"

                        },
                        {
                            "cat_id": "9",
                            "position": "4",
                            "sku": "5555"

                        }
             ]
                },
            ]
        }

Can anyone please guide me how to get the inside values from that.

I have tried this

JSONParser parser = new JSONParser();
        JSONObject items = parser.getJSONFromUrl(productInfoUrl);
        try {
            JSONArray itemsDetails = items.getJSONArray("prodCat_list");
            if(itemsDetails.length()>0){

                for (int i = 0; i < itemsDetails.length(); i++) {
                    JSONArray productWithCategories = itemsDetails.getJSONArray(i);
                    JSONObject object = productWithCategories.getJSONObject(i);

                    Product productInfo = new Product( object.getString("sku"), object.getInt("cat_id"), object.getInt("position"));
                    ProductDbHandler productDbHandler = new ProductDbHandler(context);
                    productDbHandler.addProducts(productInfo);
                }
            }
            else 
                System.out.println("No product to add");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
Helping_Hand
  • 213
  • 1
  • 5
  • 10

6 Answers6

24

Here is how I think your JSON Parser should look like (there can be some typo mistakes,I didn't test this code on editor : )) :

JSONObject mainObj = new JSONOBject(myString);
if(mainObj != null){
    JSONArray list = mainObj.getJSONArray("prodCat_list");
    if(list != null){
        for(int i = 0; i < list.length();i++){
            JSONObject elem = list.getJSONObject(i);
            if(elem != null){
                JSONArray prods = elem.getJSONArray("prods");
                if(prods != null){
                    for(int j = 0; j < prods.length();j++){
                        JSONObject innerElem = prods.getJSONObject(j);
                        if(innerElem != null){
                            int cat_id = innerELem.getInt("cat_id");
                            int pos = innerElem.getInt("position");
                            String sku = innerElem.getString("sku");
                        }
                    }
                }
            }
        }
    }
}
hardartcore
  • 16,886
  • 12
  • 75
  • 101
  • http://stackoverflow.com/questions/21525993/check-and-read-inner-json-array-which-is-in-main-json-array-in-android?noredirect=1#comment32502246_21525993 – Mahesh Feb 03 '14 at 11:52
  • Nice one.. it helped me too ..thanks for the answer .. :)..I had a question though..is this the best way to parse nested arrays/objects ? – mike20132013 Apr 09 '14 at 00:03
  • It depends a lot of your JSON string, but in my opinion, this is the best solution which I know. – hardartcore Apr 09 '14 at 07:35
  • You can automatically parse it with `Gson` library from Google. See my answer below http://stackoverflow.com/a/37585670/3387187 – stenlytw Jun 02 '16 at 08:04
  • @hardartcore I did the same. As per the question I have 10 prods array and each having five elements. I am checking a condition on the innerElem and setText to a inflated View according to that. The problem is that all the 10 arrays are running at the same time and 10 views inflated on the screen. I want them to run one by one. – pa1pal Jun 07 '16 at 07:58
1

try to use

org.json.simple.JSONValue

code:

import org.json.simple.JSONValue;
String content = "{...}";
JSONValue.parse(content);
srain
  • 8,944
  • 6
  • 30
  • 42
1

You can use the GSon library. It parses the json into various objects that you can access. A dummy Code is here:

` GSon gSon  = new GSon();
  ProdCatList prodCatList = gSon.fromJson(---inputStreamReader of your JSon data---,ProdCatList.class);`
Abhishek Shukla
  • 1,242
  • 8
  • 11
1

Try this!

//Write your own implementation of json parser
JSONParser jParser = new JSONParser();
JSONArray prod_cat = new JSONArray();
JSONArray products = new JSONArray();
JSONObject json = jParser.getJSONFromUrl("your source");
prod_cat = json.getJSONArray("prodCat_list");
for (int i = 0; i < prod_cat.length(); i++) {
        JSONObject object = prod_cat.getJSONObject(i);
        products = object.getJSONArray("products");
}
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75
0

JSONObject reader = new JSONObject(results);

            JSONArray VendorProductsList = reader.getJSONArray("VendorProductsList");
            for (int i = 0; i < VendorProductsList.length(); i++) {
                ebay=new Ebay();
                JSONObject elem = VendorProductsList.getJSONObject(i);
                JSONArray Products = elem.getJSONArray("Products");

                for (int j = 0; j < Products.length(); j++) {
                    JSONObject innerElem = Products.getJSONObject(j);
                   //JSONObject ProductName=Products.getJSONObject(j);
                  ebay.setProductName(innerElem.getString("ProductName"));
                    gallery_array.add(ebay);
0

Simply you can automatically parse it with Gson:

JSONParser parser = new JSONParser();
JSONObject items = parser.getJSONFromUrl(productInfoUrl);
Gson gson = new GsonBuilder().create();
ProdCatResponse prodCatResponse = gson.fromJson(items.toString(), ProdCatResponse.class);

But actually you can parse the JSON response in string form. You don't need to get the response in JSONObject and use toString().

JSONParser parser = new JSONParser();
String items = parser.getFromUrl(productInfoUrl);
Gson gson = new GsonBuilder().create();
ProdCatResponse prodCatResponse = gson.fromJson(items, ProdCatResponse.class);

More info about Gson: https://guides.codepath.com/android/Leveraging-the-Gson-Library


This is the model class sample, you can generate it with http://www.jsonschema2pojo.org.

ProdCatResponse.java

public class ProdCatResponse {

    private List <Prod> prods = new ArrayList <Prod> ();

    public List <Prod> getProds() {
        return prods;
    }

    public void setProds(List < Prod > prods) {
        this.prods = prods;
    }

}

Prod.java

public class Prod {

    private String catId;
    private String position;
    private String sku;

    public String getCatId() {
        return catId;
    }

    public void setCatId(String catId) {
        this.catId = catId;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getSku() {
        return sku;
    }

    public void setSku(String sku) {
        this.sku = sku;
    }

}
stenlytw
  • 938
  • 13
  • 18