nithin
nithin

Reputation: 2467

Dynamic json parsing using GSON -Android

I have two jsons

{
    success: 1,
    camera: [
    {
    productid: 18486,
    productkey: 509,
    categoryid: 85,
    categorykey: 2,
    productname: "Samsung",
    productimage: "samsung.jpg",
    price: "10900"
    },
    {
    productid: 18458,
    productkey: 508,
    categoryid: 85,
    categorykey: 2,
    productname: "Nikon Coolpix L29",
    productimage: "nikoncoolpix.jpg",
    price: "4446"
    }]
    }

Second Json

{   
        success: 1,
        mobile: [
        {
        productid: 9999,
        productkey: 519,
        categoryid: 852,
        categorykey: 21,
        productname: "Samsung grand",
        productimage: "samsung.jpg",
        price: "10900"
        },
        {
        productid: 1858,
        productkey: 58,
        categoryid: 5,
        categorykey: 12,
        productname: "nokia",
        productimage: "nokia.jpg",
        price: "44462"
        }]
        }

I need to parse the json using GSON,I tried these code

 Gson mJson = new Gson();
 PDProduct mObj = (PDProduct) mJson.fromJson(jsonStr,PDProduct.class);
 List<ProductObj> mobiles = null;



     mobiles = mObj.mobiles;

                     for (int i = 0; i < mobiles.size(); i++) {

                     HashMap<String, String> ProductDetails = new HashMap<String,
                     String>();
                     ProductDetails.put(Tags.PRODUCTTAG_CATGORY_ID,
                     mobiles.get(i).categoryid);


                     ProductDetails.put(Tags.PRODUCTTAG_CATGORY_KEY,
                     mobiles.get(i).categorykey);

                     ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_ID,
                     mobiles.get(i).productid);
                     ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_KEY,
                     mobiles.get(i).productkey);
                     ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_NAME,
                     mobiles.get(i).productname);
                     ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_PIC,
                     mobiles.get(i).productimage);
                     ProductDetails.put(Tags.PRODUCTTAG_PRODUCT_PRICE,
                     mobiles.get(i).price);

                     ProductDetail.add(ProductDetails);

    }


public class PDProduct {
    public String success = null;
    public List<PDProductObj> mobiles = null;

}

For this I'm getting only mobile data,How could dynamically create key?

Is there any other method to parse this data?

Upvotes: 0

Views: 671

Answers (2)

Brian Roach
Brian Roach

Reputation: 76898

Your two JSON objects are two different objects; one has a field camera, one has a field mobile.

If you're the one producing that JSON the easiest answer is: fix the JSON. Use one field name and include something in the objects to denote a category (or whatever that is).

If you can't fix the JSON, you can write a custom deserializer that deals with it by renaming the field:

class MyDeserializer implements JsonDeserializer<PDProduct> {

    @Override
    public PDProduct deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException
    {
       JsonObject jo = je.getAsJsonObject();
       if (jo.has("camera"))
       {
           JsonElement e = jo.remove("camera");
           jo.add("mobile", e);
       }

       return new Gson().fromJson(jo, type);

    }

} 

Then use it with Gson:

Gson gson = 
    new GsonBuilder()
        .registerTypeAdapter(PDProduct.class, new MyDeserializer())
        .create();

Upvotes: 1

Ran Wakshlak
Ran Wakshlak

Reputation: 1774

The simplest solution is to change the json. Add a new level "product" to the json and let it have name and data.

For example:

{
  success: 1,
  product:
  {
     name:camera,
     data: [
     {
     productid: 18486,
     productkey: 509,
     categoryid: 85,
     categorykey: 2,
...

Change PDIProduct class to reflect this change.

If you can't change the json (because you are getting it from an external source), change the JSON object to this structure before using GSON.

Upvotes: 1

Related Questions