从字符串转换为JSON对象的android转换为、字符串、对象、JSON

2023-09-04 06:13:07 作者:倣卟芐·。·

我工作的Andr​​oid应用程序。在我的应用程序我有字符串转换为JSON对象,然后解析值。我检查了计算器中的一个解决方案,并发现了类似的问题在这里link

解决的办法是这样的

 `{phonetype:N95,猫:WP}`
        JSONObject的jsonObj =新的JSONObject({\phonetype \:\N95 \,\猫\:\WP \});
 

我用同样的方法在我的code。我的字符串是

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

和更换后,我得到的结果,因为这

{\"ApiInfo\":{\"description\":\"userDetails\",\"status\":\"success\"},\"userDetails\":{\"Name\":\"Sarath巴布\,\用户名\:\sarath.babu.sarath巴布\,\令牌\:\ZIhvXsZlKC​​NL6Xj9OPIOOz3FlGta9g \,\用户id \:\118 \} \pendingPushDetails \ :[]} 字符串,对象转json格式

当我执行的JSONObject jsonObj =新的JSONObject(mybizData);

我收到下面的JSON异常

  org.json.JSONException:在性格1期望的文字值
 

请帮我解决我的问题。

解决方案

删除斜杠:

  JSON字符串= {phonetype:N95,猫:WP};

尝试 {

    JSONObject的OBJ =新的JSONObject(JSON);

    Log.d(我的应用,obj.toString());

}赶上(的Throwable T){
    Log.e(我的应用,无法解析畸形的JSON:\+ JSON +\);
}
 

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

The solution is like this

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

I use the same way in my code . My string is

{"ApiInfo":{"description":"userDetails","status":"success"},"userDetails":{"Name":"somename","userName":"value"},"pendingPushDetails":[]}

string mystring= mystring.replace("\"", "\\\"");

And after replace I got the result as 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 am getting the below json exception

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

Please help me to solve my issue.

解决方案

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