Convert from string to json object android

I am working on an Android application. In my application, I need to convert a string to a Json Object, and then parse the values. I checked the solution in stackoverflow and found a similar problem here link

Solution looks like this

`{"phonetype":"N95","cat":"WP"}` JSONObject jsonObj = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}"); 

I use the same in my code. My line

 {"ApiInfo":{"description":"userDetails","status":"success"},"userDetails":{"Name":"somename","userName":"value"},"pendingPushDetails":[]} string mystring= mystring.replace("\"", "\\\""); 

And after the replacement, I got a result like this

 {\"ApiInfo\":{\"description\":\"userDetails\",\"status\":\"success\"},\"userDetails\":{\"Name\":\"Sarath Babu\",\"userName\":\"sarath.babu.sarath babu\",\"Token\":\"ZIhvXsZlKCNL6Xj9OPIOOz3FlGta9g\",\"userId\":\"118\"},\"pendingPushDetails\":[]} 

when I execute JSONObject jsonObj = new JSONObject(mybizData);

I get json exception below

 org.json.JSONException: Expected literal value at character 1 of 

Please help me solve my problem.

+83
json android jsonexception string-conversion
Aug 12 '13 at 17:17
source share
8 answers

Remove the slashes:

 String json = {"phonetype":"N95","cat":"WP"}; try { JSONObject obj = new JSONObject(json); Log.d("My App", obj.toString()); } catch (Throwable t) { Log.e("My App", "Could not parse malformed JSON: \"" + json + "\""); } 
+179
Aug 12 '13 at 17:24
source share

his job

  String json = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; try { JSONObject obj = new JSONObject(json); Log.d("My App", obj.toString()); Log.d("phonetype value ", obj.getString("phonetype")); } catch (Throwable tx) { Log.e("My App", "Could not parse malformed JSON: \"" + json + "\""); } 
+17
May 14 '16 at 20:39
source share

try the following:

 String json = "{'phonetype':'N95','cat':'WP'}"; 
+6
Mar 13 '14 at 10:22
source share

To get a JSONObject or JSONArray from a String, I created this class:

 public static class JSON { public Object obj = null; public boolean isJsonArray = false; JSON(Object obj, boolean isJsonArray){ this.obj = obj; this.isJsonArray = isJsonArray; } } 

Here to get JSON:

 public static JSON fromStringToJSON(String jsonString){ boolean isJsonArray = false; Object obj = null; try { JSONArray jsonArray = new JSONArray(jsonString); Log.d("JSON", jsonArray.toString()); obj = jsonArray; isJsonArray = true; } catch (Throwable t) { Log.e("JSON", "Malformed JSON: \"" + jsonString + "\""); } if (object == null) { try { JSONObject jsonObject = new JSONObject(jsonString); Log.d("JSON", jsonObject.toString()); obj = jsonObject; isJsonArray = false; } catch (Throwable t) { Log.e("JSON", "Malformed JSON: \"" + jsonString + "\""); } } return new JSON(obj, isJsonArray); } 

Example:

 JSON json = fromStringToJSON("{\"message\":\"ciao\"}"); if (json.obj != null) { // If the String is a JSON array if (json.isJsonArray) { JSONArray jsonArray = (JSONArray) json.obj; } // If it a JSON object else { JSONObject jsonObject = (JSONObject) json.obj; } } 
+4
Sep 28 '16 at 8:10
source share

You just need lines of code, as shown below:

  try { String myjsonString = "{\"phonetype\":\"N95\",\"cat\":\"WP\"}"; JSONObject jsonObject = new JSONObject(myjsonString ); //getting specific key values Log.d("phonetype = ", jsonObject.getString("phonetype")); Log.d("cat = ", jsonObject.getString("cat"); }catch (Exception ex) { ex.printStackTrace(); } 
+3
Jul 06 '18 at 11:01
source share

Here is the code, and you can decide whether a (synchronized) StringBuffer or faster to use StringBuilder.

The test shows that StringBuilder is faster.

 public class Main { int times = 777; long t; { StringBuffer sb = new StringBuffer(); t = System.currentTimeMillis(); for (int i = times; i --> 0 ;) { sb.append(""); getJSONFromStringBuffer(String stringJSON); } System.out.println(System.currentTimeMillis() - t); } { StringBuilder sb = new StringBuilder(); t = System.currentTimeMillis(); for (int i = times; i --> 0 ;) { getJSONFromStringBUilder(String stringJSON); sb.append(""); } System.out.println(System.currentTimeMillis() - t); } private String getJSONFromStringBUilder(String stringJSONArray) throws JSONException { return new StringBuffer( new JSONArray(stringJSONArray).getJSONObject(0).getString("phonetype")) .append(" ") .append( new JSONArray(employeeID).getJSONObject(0).getString("cat")) .toString(); } private String getJSONFromStringBuffer(String stringJSONArray) throws JSONException { return new StringBuffer( new JSONArray(stringJSONArray).getJSONObject(0).getString("phonetype")) .append(" ") .append( new JSONArray(employeeID).getJSONObject(0).getString("cat")) .toString(); } } 
0
Feb 09 '15 at 11:28
source share

Better to be lower.

 JSONObject jsonObject=null; try { jsonObject=new JSONObject(); jsonObject.put("phonetype","N95"); jsonObject.put("cat","wp"); String jsonStr=jsonObject.toString(); } catch (JSONException e) { e.printStackTrace(); } 
0
Dec 14 '17 at 6:37
source share

just try this finally it works for me:

 //delete backslashes ( \ ) : data = data.replaceAll("[\\\\]{1}[\"]{1}","\""); //delete first and last double quotation ( " ) : data = data.substring(data.indexOf("{"),data.lastIndexOf("}")+1); JSONObject json = new JSONObject(data); 
0
Jan 21 '19 at 14:08
source share



All Articles