JSONObject obj=new JSONObject(arr);
remove Map and try because the JSONObject expects a JSONArray and the problem was that it was being typcasted to Map Object
質問
My Json Array of object is looks as
[{"Temp_Password":"arun111","Password":""}]
In my Android code I have been used AsyncTask task to execute HTTP request,
In preexecute method() by using toast I can get this as string[{"Temp_Password":"arun111","Password":""}]
I have tried to to get individual values inside this Json ,but i cannot get expected result
My Java code
protected void onPostExecute(String result) {
//View your result here.
JSONArray arr = null;
String a = null;
String b = null;
try {
arr=new JSONArray(result);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONObject obj=new JSONObject((Map) arr);
try {
a = obj.getString("Temp_Password");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
b = obj.getString("Password");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(Certify_Login.this,a, Toast.LENGTH_LONG).show();
Toast.makeText(Certify_Login.this,b, Toast.LENGTH_LONG).show();
}
Error
03-08 00:50:33.426: E/AndroidRuntime(1788): java.lang.ClassCastException: org.json.JSONArray cannot be cast to java.util.Map
解決
JSONObject obj=new JSONObject(arr);
remove Map and try because the JSONObject expects a JSONArray and the problem was that it was being typcasted to Map Object