-1

I need to convert

{"officeId":1,"clientId":97,"resourceId":97}

Please note that for values I don't have quotes. I have seen similar questions asked and answered, i haven't seen one that looks exactly like this one, i.e values have no quotes in the string to be converted.

Here's the original string from REST server

"{\"officeId\":1,\"clientId\":98,\"resourceId\":98}"
makunomark
  • 789
  • 6
  • 10
  • 2
    Try using `JSONParser`.. `JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse("{\"officeId\":1,\"clientId\":98,\"resourceId\":98}");` – RP- Jan 18 '16 at 17:59
  • I'm trying to import `JSONParser` in Android Studio but it seems like its not available for import. :-( – makunomark Jan 18 '16 at 18:07
  • I was assuming that you were using "org.json" library. If you are using something else like Gson, it should be `JsonParser`. The idea behind is to let the parser take care of parsing it. It will guess the correct types. – RP- Jan 18 '16 at 18:18
  • I assumed android would contain `JSONParser`, but I couldn't import it, I'll try to check why, but in the meantime i am using `Gson`. `JsonParser` works well. – makunomark Jan 18 '16 at 18:25

1 Answers1

0

Are you getting an error? You shouldn't be because it's valid JSON. If you quote the numbers, they're now strings instead.

See Can JSON numbers be quoted.

A regular parse will suffice:

JSONObject jsonObj = new JSONObject("{\"officeId\":1,\"clientId\":98,\"resourceId\":98}");
Community
  • 1
  • 1
Eugene
  • 10,957
  • 20
  • 69
  • 97
  • Yes I'm getting an error : `org.json.JSONException: Value {"officeId":1,"clientId":97,"resourceId":97} at body of type java.lang.String cannot be converted to JSONObject` – makunomark Jan 18 '16 at 18:09