capdragon
capdragon

Reputation: 14899

colon (:) within JSON data using Gson

I'm calling a web service that returns JSON. Within that JSON I have a property that holds a URL. But the colon (:) within that URL is making Gson throw a gson.stream.MalformedJsonException error. I know these keys and values should be wrapped

JSON returned by web service:

{
   ID=15; 
   Code=ZPFgNr; 
   UserName=https://www.google.com/accounts/o8/id?id=xxxxxx; //<--problem
   FirstName=Joe
}

My Java:

resultData=((SoapObject) result).getProperty(0).toString();
User response = gson.fromJson(resultData, User.class);

I know these keys and values should be wrapped in double quotes. But they are not, and that seems to be the problem.

So my question is:

Should I be encoding this JSON before deserializing it somehow? If so, how?

or

Should I do a find and replace on https: and escape the colon, If so, how would I escape the colon?

Upvotes: 3

Views: 11917

Answers (1)

JB Nizet
JB Nizet

Reputation: 691973

JSON uses commas to separate attributes, colon to separate the attribute name from the attribute value, and double quotes around the names and the values. This is not valid JSON.

Here's valid JSON:

{
   "ID" : "15", 
   "Code" : "ZPFgNr",
   "UserName" : "https://www.google.com/accounts/o8/id?id=xxxxxx",
   "FirstName" : "Joe"
}

Upvotes: 5

Related Questions