Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 448
capdragon Avatar asked Sep 03 '26 00:09

capdragon


1 Answers

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"
}
like image 155
JB Nizet Avatar answered Sep 04 '26 13:09

JB Nizet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!