从string转换为json对象android

我正在开发一个Android应用程序。 在我的应用程序,我必须将string转换为Json对象,然后parsing值。 我检查了一个解决scheme,并在这里find类似的问题链接

解决scheme就是这样

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

我在我的代码中使用相同的方式。 我的string是

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

而replace后,我得到了这样的结果

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

当我执行JSONObject jsonObj = new JSONObject(mybizData);

我得到了下面的jsonexception

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

请帮我解决我的问题。

删除斜杠:

 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 + "\""); } 

是工作

  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 + "\""); } 

尝试这个:

 String json = "{'phonetype':'N95','cat':'WP'}"; 

从string中获取JSONObject或JSONArray我创build了这个类:

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

这里获取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); } 

例:

 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's a JSON object else { JSONObject jsonObject = (JSONObject) json.obj; } } 

这是代码,你可以决定哪个
(同步)StringBuffer或更快的StringBuilder使用。

基准显示StringBuilder更快。

 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(); } }