I have a web service that returns data in the form of json. but the returned data actually not in json format because for some key values like ids it gives the values of numbers without any quotes because of that when I am creating an object it throws an exception so my requirement is simple how can i surround quotes to the numbers that don’t have quotes.
{"name":"pragnani","job":"android developer","experience":"fresher","age":22,"id":10}
Here Id and age are not there with quotes, I want to replace them with quotes.
Don’t simple post substring kind of a thing because it won’t work because web services return huge amount of data . And I am using one separate class to retrive data from different web services .so Please help me….
Edit:
Here is the data
"[{\"PracticeID\":36,\"PracticeName\":\"Dr. John Doe\",\"DBServerName\":\"DBSERVER8\"}]"
this is the method to retrive data
@Override
protected String doInBackground(String... aurl) {
String results="";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(aurl[0]);
post.setHeader("content-type", "application/json");
StringEntity entity = new StringEntity(dto.toString());
post.setEntity(entity);
HttpResponse resp = httpClient.execute(post);
String respStr = EntityUtils.toString(resp.getEntity());
results=respStr.trim();
results=results.replace("]\"", "]").replace("}", "\"}").replace("\":", "\":\"").replace("}\",","},");
results=results.replace("\"[", "[");
results=results.replace("\\\"", "\"");
results=results.replace("\"\"", "\"");
String[] arr=results.split("\\{");
if(arr.length<=2)
{
results=results.substring(results.indexOf("[")+1,results.indexOf("]"));
}
else
{
results=results.replace("[", "{\"result\":[").replace("]", "]}");
}
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
How to I convert it into json String….
JSON allows numeric values in its structure (I.E. unquoted numbers). They can be intergers floats or any other numeric type that JavaScript would recognize.
Allowed types: Objects, Arrays, Numerics, Strings, Booleans, null.
As opposed to normal JavaScript all Object keys have to be quoted, that is true! Asides from that you can directly use numerics at will.