2

A service I'm using (SendBird), which I have no control over, is returning data (a string) in the form of:

"{u'some_key': u'some_value', u'some_other_key': u'some_other_value'}"

Some searching revealed that certain versions of Python will prefix strings with a 'u' character so I'm assuming that's what's happening here. However, when I do a standard:

Gson gson = new Gson();
SomeClass object = gson.fromJson(dataString, SomeClass.class);

where dataString = "{u'some_key': u'some_value', u'some_other_key': u'some_other_value'}"

it doesn't properly map the values to my class (yes, I've double checked that the serialized names matches the java object properties). Does Gson not handle the 'u' prefix properly or is something else possibly going on here?

JJC
  • 1,346
  • 10
  • 10
  • the JSON format is not valid . – Zahidul Islam Apr 30 '16 at 07:10
  • Not sure what you mean by that? It's a valid string likely returned by a server running Python 2.x according to this http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string – JJC Apr 30 '16 at 07:12
  • check your JSON String here .https://jsonformatter.curiousconcept.com/ – Zahidul Islam Apr 30 '16 at 07:15
  • To clarify, the service returns a string literal in it's data field, not a JSON object. I'm using Gson to deserialize the string to a plain old java object. – JJC Apr 30 '16 at 07:17
  • GSON only parse JSON formatted data. – Zahidul Islam Apr 30 '16 at 07:20
  • I suppose that it might be that servers returning JSON in this format will never be compatible with GSON, which is unfortunate. – JJC Apr 30 '16 at 07:22

1 Answers1

1

Like you said is not a valid json because of the "u" prefix in the labels. You can check it here: http://jsonlint.com/

So Gson can't parse and map to your class.

Try removing the "u" before the labels and it will work.

Pablo
  • 2,581
  • 3
  • 16
  • 31
  • Just clarified above that I don't have control over the service so this isn't possible. – JJC Apr 30 '16 at 07:05