我正在實現JSONParsing,但它獲取的是org.json.JSONException的JSONException:值true時,狀態類型java.lang.String無法在高級中轉換爲JSONObject.Thanks。在Android中無法將字符串轉換爲JSONObject
這裏是我的JSON字符串
{ 「Upd_Post_ActivityResult」:{ 「的ResponseItem」: 「103_20150900021028」, 「狀態」: 「真正的」}}
這裏是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();
}
}
這裏是日誌錯誤
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)
'status'是一個字符串,所以使用'json_Upd_Post_ActivityResult.getString(「Status」);' – Emil
它運作良好。但是JSONObject和getString有什麼不同。它也是JSONObject(「狀態」)。 – androidTag
考慮使用Google Gson庫來簡化json解析,這很好 – lubilis