简体   繁体   English

字符串无法在 Android 中转换为 JSONObject

[英]String cannot be converted to JSONObject in Android

I'm implementing the JSONParsing but it getting the JSONException of org.json.JSONException: Value true at Status of type java.lang.String cannot be converted to JSONObject.Thanks in advanced.我正在实施 JSONParsing,但它得到 org.json.JSONException 的 JSONException: Value true at Status of type java.lang.String cannot be convert to JSONObject.Thanks in advanced。

Here is my Json String这是我的 Json 字符串

{"Upd_Post_ActivityResult":{"ResponseItem":"103_20150900021028","Status":"true"}} {"Upd_Post_ActivityResult":{"ResponseItem":"103_20150900021028","Status":"true"}}

Here is the JSON Parsing这是JSON解析

 if (result != null) {
                            try
                            {
                                JSONObject jsonObj = new JSONObject(result);
                                String jsonResult = jsonObj.toString().trim();
                                Log.e("jsonResult ", " = " + jsonResult);

                                JSONObject json_Upd_Post_ActivityResult = jsonObj.getJSONObject("Upd_Post_ActivityResult");
                                Log.e("getAllActivity_List ", " = " + json_Upd_Post_ActivityResult.toString());

                                JSONObject jobj1 = json_Upd_Post_ActivityResult.getJSONObject("Status");
                                String strStatusObj = jobj1.toString();
                                Log.e("strStatusObj "," = " + strStatusObj);

                                if(strStatusObj.equals("true"))
                                {
                                    Log.e("StartMark Url Uploaded ", "Succesfully !!!!!");
                                    Log.e("Status ", "True");
                                    int imagesArray = imgItemArray.size();
                                    Log.e("imagesArray "," = "+imagesArray);


                                }
                                else
                                {
                                    Log.e("Status ","False");
                                }
                            }
                            catch (JSONException je)
                            {
                                je.printStackTrace();
                            }
                        }

Here is log error这是日志错误

09-18 06:41:02.543  20427-20490/? W/System.err﹕ org.json.JSONException: Value true at Status of type java.lang.String cannot be converted to JSONObject
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:100)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at org.json.JSONObject.getJSONObject(JSONObject.java:613)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at com.example.tazeen.classnkk.AddPost$7$1.doInBackground(AddPost.java:320)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at com.example.tazeen.classnkk.AddPost$7$1.doInBackground(AddPost.java:286)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-18 06:41:02.543  20427-20490/? W/System.err﹕ at java.lang.Thread.run(Thread.java:818)

Try the below approach:尝试以下方法:

JSONObject jobj1 = json_Upd_Post_ActivityResult.getJSONObject("Status");
String strStatusObj = jobj1.toString();

Status is not a JSONObject.状态不是 JSONObject。 So instead to getJSONObject("Status") you have to use String strStatusObj = json_Upd_Post_ActivityResult.getString("Status") ;因此,要使用 getJSONObject("Status") ,您必须使用String strStatusObj = json_Upd_Post_ActivityResult.getString("Status") ;

i have done few changes in your code , please check...我对您的代码做了一些更改,请检查...

try { JSONObject jsonObj = new JSONObject(result);尝试 { JSONObject jsonObj = 新 JSONObject(result); String jsonResult = jsonObj.toString().trim();字符串 jsonResult = jsonObj.toString().trim(); Log.e("jsonResult ", " = " + jsonResult); Log.e("jsonResult ", " = " + jsonResult);

                            JSONObject json_Upd_Post_ActivityResult = jsonObj.getJSONObject("Upd_Post_ActivityResult");
                            Log.e("getAllActivity_List ", " = " + json_Upd_Post_ActivityResult.toString());

                            String strStatusObj = json_Upd_Post_ActivityResult.getString("Status");
                            Log.e("strStatusObj "," = " + strStatusObj);
                            if(strStatusObj.equals("true"))
                            {
                                Log.e("StartMark Url Uploaded ", "Succesfully !!!!!");
                                Log.e("Status ", "True");
                                int imagesArray = imgItemArray.size();
                                Log.e("imagesArray "," = "+imagesArray);


                            }
                            else
                            {
                                Log.e("Status ","False");
                            }
                        }
                        catch (JSONException je)
                        {
                            je.printStackTrace();
                        }
                    }

Change this改变这个

JSONObject jobj1 = json_Upd_Post_ActivityResult.getJSONObject("Status");

to

String strStatusObj = json_Upd_Post_ActivityResult.getString("Status");

请使用下面的,让我知道它是否有效

String str=json_Upd_Post_ActivityResult.getString("Status");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM