4

我已经尝试修复此错误 2 天了,从 stackoverflow.com 搜索并尝试了多种编码类型。我检查了我的 JSON http://jsonformatter.curiousconcept.com/#jsonformatter。但我仍然无法找出我的代码为什么会这样做。我有 3 个文件。MainACtivity.java 应该从我的服务器上的 test.json 文件中获取信息,然后将其处理到 Events.java。Events.java 只是显示信息,但应用程序并没有做到那么远。

更新的代码以防其他人需要修复此问题。

我的错误:

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

}

}


}
4

3 回答 3

4

您从服务器获得一个 json 数组,并尝试将其转换为 JsonObject。

代替

JSONObject obj = new JSONObject(string);

你应该做

JSONArray obj = new JSONArray(string);

据我们所知..

[ = JSON数组

{ = JSON对象

所以

try 

    {
            JSONArray jObj = new JSONArray(json);
//other code
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
于 2014-01-15T04:54:00.573 回答
3

doInBackground()更改的相关代码MainActivity.java

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

至:

JSONArray eventDetails = parent.getJSONArray("event");
于 2014-01-15T04:57:57.670 回答
0

您的代码只是解析 Json 对象而不是 Json 数组的值。

解析 JsonObjects:

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

从 JsonObjects 解析 JsonArray:

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

在您的代码中更改此

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

JSONObject parent = new JSONObject(json);
JSONArray eventDetails = parent.getJSONArray("event");
于 2014-01-15T05:07:51.700 回答