Reputation: 287
Below is the JSON response I am getting for API requests.
user = { 'name':'Siva', 'address':'my address', 'pincode':12345, 'url':'http://myweb.com/index.php?title=firstname:lastname+middlename&action=edit' }
As this JSON response started with user = its neither JSONObject nor JSONArray. So I considered this as String and i split the response
String[] response = responseBody.split("=");
Gson gson = new GsonBuilder().setLenient().create();
User user = gson.fromJson(response[1], User.class);
This is causing MalformedJsonException like below
Caused by: com.google.gson.stream.MalformedJsonException: Unterminated string at line 5 column 47 path $.url
I observed that value for url key is causing issues. Because it has = and : characters in url value. But i did not find the proper solution.
Can anybody help me on how to handle this.
Upvotes: 1
Views: 125
Reputation: 6704
After you get this in response[1]
,
{ 'name':'Siva', 'address':'my address', 'pincode':12345, 'url':'http://myweb.com/index.php?title=firstname:lastname+middlename&action=edit' }
replace the '
with "
first. Like this,
response[1] = response[1].replaceAll("\'", "\"");
Check the response after that to be sure that all the '
are actually replaced by "
.
Then see if it parses afterwards.
Upvotes: 0
Reputation: 526
Since you have been using gson could do something like this
Gson g = new Gson();
Person person = g.fromJson(responsejsonstring, Person.class);
System.out.println(person.name); //[email protected]
System.out.println(g.toJson(person)); // {"email":"[email protected]"}
Upvotes: 1