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"
          }

        ]
      }

    ]
  }
}
12
  • To clarify this: You parse the JSON object with Google gson, or do you need this solution for some other language? Commented Jun 15, 2015 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. Commented Jun 15, 2015 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? Commented Jun 15, 2015 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. Commented Jun 15, 2015 at 21:40
  • @mike brant, no i don't have control over it. I am getting as a response from calling external web service. Commented Jun 15, 2015 at 23:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.