0

Hi I am trying to parse json object below. But the problem is, inside the profile attribute, there is an attribute called fields which is sometimes a json object and sometime an json-array, so it is creating a problem when i am trying to use Gson to parse it. I followed this link but it didn't help How to dynamically handle json response array/object using Gson , so help is needed from someone who encountered this before, thanks!.

{
  "users": {
    "profile": [
      {
        "fields": {
          "key": "fname",
          "value": "Michael"
        }        
      },
      {
        "fields": [
          {
            "key": "lname",
            "value": "Bob"
          },
          {
            "key": "age",
            "value": "25"
          }

        ]
      }

    ]
  }
}
Community
  • 1
  • 1
  • To clarify this: You parse the JSON object with Google gson, or do you need this solution for some other language? – Philipp Jun 15 '15 at 19:18
  • Hi Philipp, what i what is to convert this json to pojo java class so that i can retrieve any field. I am working in java by the way. – user3780757 Jun 15 '15 at 19:47
  • Sounds like poor JSON creatoin logic if you have a case where multiple "fields" gives you an array on JSON encoding and s single field value gives you an object. Typically one would expect to use an array in all cases (whether the array has multiple values, a single value, or no values). Do you have control over the initial JSON serialization? – Mike Brant Jun 15 '15 at 20:41
  • _"I followed this link but it didn't help How to dynamically handle json response array/object using Gson"_ I think it should help. Write a custom deserializer. – Alexis C. Jun 15 '15 at 21:40
  • @mike brant, no i don't have control over it. I am getting as a response from calling external web service. – user3780757 Jun 15 '15 at 23:19
  • What happened when you tried the solution in the link? The solution there looks right to me. – Taylor Jun 15 '15 at 23:20
  • @AlexisC. Can you please try to give the skeleton of the pojo class with respect to this json. – user3780757 Jun 15 '15 at 23:22
  • @user3780757 Your pojo could contains a list of objects. The thing that matters is that you need to implement the deserialization process yourself with a custom deserializer. – Alexis C. Jun 15 '15 at 23:30
  • @MikeBrant, Here are the two pojo classes i am using public class Field { private String key; private String Value; public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getValue() { return Value; } public void setContent(String Value) { this.Value = Value; } } public class profile { private Field fields; public fields getFields() { return attribute; } public void setFields(Field field) { this.field = field; } } – user3780757 Jun 15 '15 at 23:30
  • @AlexisC. so it seems that is what i missing. Can you give me a hint on that? I am only trying to replicate what i got from the link. – user3780757 Jun 15 '15 at 23:39
  • Show your deserializer – Alexis C. Jun 15 '15 at 23:45
  • @AlexisC. i am using as follows: Gson gson = new GsonBuilder().registerTypeAdapterFactory( new ArrayAdapterFactory()).create(); Users response; response = gson .fromJson(jsonInString, Users.class); – user3780757 Jun 16 '15 at 00:12

0 Answers0