org.json.jsonarray不能转换为JSONObject的错误转换为、错误、json、org

2023-09-07 02:29:52 作者:阳光未必能暖人心

我一直在努力,现在来修复这个错误2天,搜查,从stackoverflow.com尝试多种编码类型。我检查我的JSON http://jsonformatter.curiousconcept.com/#jsonformatter 。但我仍然无法找出为什么我的code是这样做的。我有3个文件。 MainACtivity.java应该让我的服务器上从我test.json文件中的信息,然后将它处理到Events.java。 Events.java只是显示的资料,但应用程序不让它那么远。

**修订code如果任何人需要定为这。 **

我的错误:

  01-14 22:18:08.165:E / JSON响应:(419):> {事件:    01-14 22:18:08.165:E / JSON响应:(419):{    01-14 22:18:08.165:E / JSON响应:(419):EVENT_NAME:测试赛,    01-14 22:18:08.165:E / JSON响应:(419):EVENT_TIME:下午7:00    01-14 22:18:08.165:E / JSON响应:(419):event_price:「$ 120.00元    01-14 22:18:08.165:E / JSON响应:(419):}    01-14 22:18:08.165:E / JSON响应:(419):]    01-14 22:18:08.165:E / JSON响应:(419):}    01-14 22:18:08.175:E / JSON的错误(419):错误:org.json.JSONException:值[{event_price:「$ 120.00元,EVENT_TIME:下午7时00,EVENT_NAME: 测试事件}]在类型org.json.JSONArray的事件不能被转换成的JSONObject 

MainActivity.java

 包com.example.dba;进口org.json.JSONException;进口org.json.JSONObject;进口android.app.Activity;进口android.content.Intent;进口android.os.AsyncTask;进口android.os.Bundle;进口android.util.Log;公共类MainActivity扩展活动{串EVENT_NAME,EVENT_TIME,event_price;静态的JSONObject对象= NULL;@覆盖保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);    新的prefetchData()执行();}/ ** *异步任务进行HTTP调用 * /私有类prefetchData延伸的AsyncTask<太虚,太虚,太虚>{    @覆盖    在preExecute保护无效()    {        super.on preExecute();    }    @覆盖    保护无效doInBackground(虚空......为arg0)    {        JsonParser jsonParser =新JsonParser();        串JSON = jsonParser.getJSONFromUrl(http://www.website/test.json);        Log.e(JSON回应:,>中+ JSON);        如果(JSON!= NULL)        {           尝试            {                JSONObject的父=新的JSONObject(JSON);                JSONArray eventDetails = parent.getJSONArray(事件);                的for(int i = 0; I< eventDetails.length();我++)                {                    对象= eventDetails.getJSONObject(ⅰ);                    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);                }            }赶上(JSONException E)                {                Log.e(Json的错误,错误:+ e.toString());                    e.printStackTrace();                }        }        返回null;    }    @覆盖    保护无效onPostExecute(虚空结果)    {        super.onPostExecute(结果);        意图I =新意图(MainActivity.this,Events.class);        i.putExtra(EVENT_NAMEEVENT_NAME);        i.putExtra(EVENT_TIME,EVENT_TIME);        i.putExtra(event_price,event_price);        startActivity(ⅰ);        //关闭此活动        完();    }}}} 

解决方案

相关code改变:在 doInBackground() MainActivity的.java

的JSONObject eventDetails = parent.getJSONObject(事件);

JSONArray eventDetails = parent.getJSONArray(事件);

JSONArray和JSONObject的转换

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

}

}


}

解决方案

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

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

to:

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