org.json.jsonarray cannot be converted to jsonobject error

江枫思渺然 提交于 2019-11-30 22:08:45
Nezam

Relevant code to change - in doInBackground() of MainActivity.java:

JSONObject eventDetails = parent.getJSONObject("event");

to:

JSONArray eventDetails = parent.getJSONArray("event");

you get from the server a json array and you are try to convert it in a JsonObject.

instead of

JSONObject obj = new JSONObject(string);

you should do

JSONArray obj = new JSONArray(string);

as we know..

[ = JSONArray

and

{ = JSONObject

So

try 

    {
            JSONArray jObj = new JSONArray(json);
//other code
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

Your code just parsing Json Objects not the values of Json Arrays.

To parse JsonObjects:

jsonobject = JSONfunctions.getJSONfromURL("URL Here");

To parse JsonArray from the JsonObjects:

jsonarray = jsonobject.getJSONArray("Object name here");

In your Code change this

JSONObject parent = new JSONObject(json);
JSONObject eventDetails = parent.getJSONObject("event");

to

JSONObject parent = new JSONObject(json);
JSONArray eventDetails = parent.getJSONArray("event");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!