-1

I have a json as mentioned below.

{
  "product": [
    {
      "classification": "abc",
      "ABC": [
        {
          "classification": "abc",
          "name": "abc new product one",
          "price": "10775.0000",
        },
        {
          "classification": "abc",
          "name": "abc new product two",
          "price": "12725.0000",
        }
      ]
    },
    {
      "classification": "def",
      "DEF": [
        {
          "classification": "def",
          "name": "def product one",
          "price": "728.0000",
        },
        {
          "classification": "def",
          "name": "def product two",
          "price": "1263.0000",
        },
      ]
    }
  ],
  "status": "OK",
  "message": "success"
}

In the above json, the key in capital letter is dynamic

(Ex: ABC, DEF)

. I've created pojo class as below:

    public class ProductResponse{

       private String status;
       private String message;
       private List<Products>;

       Getters And Setters
    }

    public class Products{

       private String classification;

    }

I'm struggling to write the next part in Products pojo class, As the keys which are in capitals

(Ex: ABC, DEF)

are dynamic. I am using volley library for getting the data and for parsing I'm using gson library. Please help me out.

Community
  • 1
  • 1
  • 1
    there is poor standard of api. plz edit your web api according your requirments.....!!! – Muhammad Waleed May 09 '16 at 11:46
  • Using JSONObject for retrieve the list content of ABC may be a workaround. At least, you have a key comes from backend even if dynamic. Then you can parse the json string to ArrayList<> – asozcan May 09 '16 at 11:51
  • for that case do not directly map json to object of course there can be other solution. till now you can get ABC,DEF which are dynamic by index using loop to iterate and read json. – Pramod mishra May 09 '16 at 12:00
  • this might help http://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result – deepThought May 09 '16 at 12:10
  • i can't understand your problem please explain it briefly – Basil Battikhi May 09 '16 at 13:23
  • I just want to get the dynamic key"ABC" and "DEF" so that i can form a pojo class. Both "ABC" and "DEF" are in capital letters just for highlighting purpose. – user2511004 May 09 '16 at 14:44

2 Answers2

0

Your problem is the dynamic key, which I found is not dynamic. As I can see, For each element in the product array, the classification key's value is the next key. Only difference is that they are in capital letters.

{
  "classification": "def",
  "DEF": [
    {
      "classification": "def",
      "name": "def product one",
      "price": "728.0000",
    },
    {
      "classification": "def",
      "name": "def product two",
      "price": "1263.0000",
    },
  ]
}

As in, here "classification": "def" and the next key is DEF. What I would do here is get the value of classification key, capitalize all the letters in the string and then use it as the key.

salmanwahed
  • 9,450
  • 7
  • 32
  • 55
  • I have capitalized it just for highlighting purpose. Actually it's in small letter. Can you please tell me how can I get the "classification" value in pojo class as you said. – user2511004 May 09 '16 at 12:42
  • You can set the value in the constructor. Parse the values and pass them while creating an object of your pojo class. – salmanwahed May 09 '16 at 12:59
0

You can't have a dynamic name. the reason being is the name in your json needs to be linked to an attribute in your object. I recommend you to add a "classification" attribute in your json and your model as following :

{
  "type": "def",
  " results": [
    {
      "name": "def product one",
      "price": "728.0000",
    },
    {
      "name": "def product two",
      "price": "1263.0000",
    },
  ]
}



public final class ProductResponse{

       private String status;
       private String message;
       private List<Products> product = new Arraylist<>();

       // Getters And Setters
    }

    public final class Products{

       private String type; // type of product // ABC or DEF
       private List<Result> results = new Arraylist<>(); 

      // Getters And Setters
    }


     public final class Result{

       private String name;
       private String price;


     // Getters And Setters
    }

Your products class has now a list of results associated to a classification !

Hope it helps !

Guillaume agis
  • 3,756
  • 1
  • 20
  • 24
  • If you observe in my json, there are two types of products. One is "ABC" and other one is "DEF". I've capitalized it just for highlighting purpose. – user2511004 May 09 '16 at 14:43
  • Hi, I updated the snippet. The type needs to be defined. Please see above. – Guillaume agis May 09 '16 at 14:49
  • @user2511004 are you happy with my answer ? If yes, please upvote my answer ! thanks :) – Guillaume agis May 10 '16 at 11:02
  • I'm happy with your answer. That's the reason I've accepted your answer but i don't have 15reputation to upvote your answer sorry. Thank you very much for the answer – user2511004 May 19 '16 at 18:21