Android JSON Object

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I have a JSON format like this

{"response":{"status":true,"result":"user_exists"}}

Now i am trying to retrieve the Status value to do some logic

JSONObject jData = new JSONObject(data); JSONArray response = jData.getJSONArray("response"); 

But i am getting the following error

org.json.JSONException: Value {"result":"user_exists","status":true} at response of type org.json.JSONObject cannot be converted to JSONArray

how to retrieve an Object from inside and Object ?

回答1:

you are trying to retreive the status attribut from a JSONArray but , you don't have any JSONArray in your Code , ( JSONArray is surrounded by [] , and JSONObject is surrounded by {} ) , So to retreive the status value , try this :

JSONObject jData = new JSONObject(data); JSONObject response = jData.getJSONObject("response");  boolean status = response.getBoolean("status"); 


回答2:

response is a JSONObject, not a JSONArray. Array objects are surrounded by these [] brackets, objects are with the normal ones {}. (See json.org for more format information)

Change

JSONArray response = jData.getJSONArray("response"); 

to

JSONObject response = jData.getJSONObject("response"); 


回答3:

response isn't an array but an object. Use getJSONObject and JSONObject instead of getJSONArray and JSONArray.



回答4:

You have to first navigate to the response object by

JSONObject response = jData.getJSONObject("response") instead of JSONArray, as response is a object.



文章来源: Android JSON Object
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!