org.json.jsonarray cannot be converted to jsonobject error

隐身守侯 提交于 2020-01-11 03:25:18

问题


I have been trying to fix this error for 2 days now, searched and tried multiple coding types from stackoverflow.com. I have checked my JSON http://jsonformatter.curiousconcept.com/#jsonformatter. But I am still unable to find out why my code is doing this. I have 3 files. MainACtivity.java that should get the information from my test.json files on my server and then process it to the Events.java. Events.java just displays the information, but the app doesn't make it that far.

UPDATED CODE IN CASE ANYONE ELSE NEEDS THE FIX FOR THIS.

My Error:

 01-14 22:18:08.165: E/JSON Response:(419): > { "event":[ 
    01-14 22:18:08.165: E/JSON Response:(419):      {
    01-14 22:18:08.165: E/JSON Response:(419):       "event_name":"Test Event",
    01-14 22:18:08.165: E/JSON Response:(419):       "event_time":"7:00pm",
    01-14 22:18:08.165: E/JSON Response:(419):       "event_price":"$15.00"
    01-14 22:18:08.165: E/JSON Response:(419):      }
    01-14 22:18:08.165: E/JSON Response:(419):   ] 
    01-14 22:18:08.165: E/JSON Response:(419): }
    01-14 22:18:08.175: E/Json Error(419): Error: org.json.JSONException: Value         [{"event_price":"$15.00","event_time":"7:00pm","event_name":"Test Event"}] at event of type     org.json.JSONArray cannot be converted to JSONObject 

MainActivity.java

package com.example.dba;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity 
{

String event_name, event_time, event_price;
static JSONObject object =null;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new PrefetchData().execute();
}

/**
 * Async Task to make http call
 */
private class PrefetchData extends AsyncTask<Void, Void, Void> 
{

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();      
    }

    @Override
    protected Void doInBackground(Void... arg0) 
    {

        JsonParser jsonParser = new JsonParser();
        String json = jsonParser.getJSONFromUrl("http://www.website/test.json");

        Log.e("JSON Response: ", "> " + json);

        if (json != null) 
        {
           try 
            {
                JSONObject parent = new JSONObject(json);
                JSONArray eventDetails = parent.getJSONArray("event");

                for(int i=0; i < eventDetails.length(); i++)
                {
                    object = eventDetails.getJSONObject(i);
                    event_name = object.getString("event_name");
                    event_time = object.getString("event_time");
                    event_price = object.getString("event_price");

                    Log.e("JSON", "> " + event_name + event_time + event_price );
                }
            }  catch (JSONException e) 
                {
                Log.e("Json Error", "Error: " + e.toString());
                    e.printStackTrace();
                }

        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) 
    {           
        super.onPostExecute(result);

        Intent i = new Intent(MainActivity.this, Events.class);
        i.putExtra("event_name", event_name);
        i.putExtra("event_time", event_time);
        i.putExtra("event_price", event_price);
        startActivity(i);

        // close this activity
        finish();
    }

}

}


}

回答1:


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

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

to:

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



回答2:


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());
        }



回答3:


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");


来源:https://stackoverflow.com/questions/21129318/org-json-jsonarray-cannot-be-converted-to-jsonobject-error

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