0

I'm making an API call that returns JSON which has a particular field which either returns false or a map depending on content. It's a field that I don't care about. I expected GSON to ignore this particular field, though it doesn't seem to be. The object generation fails with the following message:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 403560

I've seen this particular question (Gson deserialize json with varying value types). I want to make sure I need to make a custom deserializer before doing so. I'm wondering if I may have another issue.

edit:

Example:

"anonymous_flag": {  }

vs

"anonymous_flag": "yes"

Another Edit:

I actually had the field in my model object... I was referencing the wrong class. Judge away :)

Community
  • 1
  • 1
loeschg
  • 29,961
  • 26
  • 97
  • 150
  • Please can you share your JSON string to make it more clear? I have already answered it in the same context. Read it [HERE](http://stackoverflow.com/questions/24125695/how-to-convert-json-string-to-a-java-object-when-the-structure-is-different?answertab=votes#tab-top). It might help you. If you need more clarity then please let me know. – Braj Jun 10 '14 at 20:35
  • As I mentioned above, I actually did include that field in my model object, which was the problem. I'll keep the question around as a reminder to others to double check their code :P – loeschg Jun 10 '14 at 21:23

1 Answers1

1

If you are deserializing the JSON into a domain object of your own design, you can simply omit the field from it if you're not interested in the value. GSON will ignore the field in the JSON.

Dave Morrissey
  • 4,371
  • 1
  • 23
  • 31