colon (:) within JSON data using Gson

£可爱£侵袭症+ 提交于 2019-12-13 03:56:55

问题


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?


回答1:


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"
}


来源:https://stackoverflow.com/questions/14083365/colon-within-json-data-using-gson

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!