将json数据显示到列表视图

时间:2014-08-21 13:01:20

标签: android arrays json listview jsonobject

我正在尝试将数据从JSON数组显示到 ListView 。但它给了我一个例外:

org.json.JSONException: Value [{"fine_id":"51771","street":"Tom Mboya","area":"Uptown","car_no":"MP08MF3299"}] of type org.json.JSONArray cannot be converted to JSONObject

我在做错误的地方请帮助我解决问题

08-21 08:33:54.640: E/JSON(943): 
[
{
  "car_no":"MP08MF3299"
 ,"fine_id":"51771"
 ,"area":"Uptown"
 ,"street":"Tom Mboya"
}
]
08-21 08:33:54.640: D/Response:(943): [{"car_no":"MP08MF3299","fine_id":"51771","area":"Uptown","street":"Tom Mboya"}]
08-21 08:33:54.650: W/System.err(943):
 org.json.JSONException: 
Value 
[
 {
  "fine_id":"51771"
 ,"street":"Tom Mboya"
 ,"area":"Uptown"
 ,"car_no":"MP08MF3299"
 }
] of type org.json.JSONArray cannot be converted to JSONObject
08-21 08:33:54.680: W/System.err(943):  at org.json.JSON.typeMismatch(JSON.java:111)
08-21 08:33:54.680: W/System.err(943):  at org.json.JSONObject.<init>(JSONObject.java:159)
08-21 08:33:54.740: W/System.err(943):  at org.json.JSONObject.<init>(JSONObject.java:172)
08-21 08:33:54.740: W/System.err(943):  at com.example.umoja_parking.ClamperActivity$FetchVehiclesTask.doInBackground(ClamperActivity.java:182)
08-21 08:33:54.780: W/System.err(943):  at com.example.umoja_parking.ClamperActivity$FetchVehiclesTask.doInBackground(ClamperActivity.java:1)
08-21 08:33:54.780: W/System.err(943):  at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-21 08:33:54.790: W/System.err(943):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-21 08:33:54.830: W/System.err(943):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-21 08:33:54.830: W/System.err(943):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-21 08:33:54.840: W/System.err(943):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-21 08:33:54.840: W/System.err(943):  at java.lang.Thread.run(Thread.java:841)
08-21 08:33:54.880: I/System.out(943): Fetching Response[{"car_no":"MP08MF3299","fine_id":"51771","area":"Uptown","street":"Tom Mboya"}]

这是我的活动代码

public class ClamperActivity extends ListActivity {


JSONParser jsonParser = new JSONParser();

private static final String CAR_NO = "car_no";
private static final String FINE_ID = "fine_id";
private static final String AREA = "area";
private static final String STREET = "street";

JSONArray jsonArray = null;

 ArrayList<HashMap<String, String>> jsonlist;

 public static String user_id="";

 static String url= "";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clamper);
jsonlist = new ArrayList<HashMap<String,String>>();
ListView lv = getListView();

new FetchVehiclesTask().execute();

}

class FetchVehiclesTask extends AsyncTask<Void, Void, Void>
{
private ListActivity activity;

protected void onPreExecute() {
    super.onPreExecute();
    pDialog = new ProgressDialog(ClamperActivity.this);
    pDialog.setMessage("Fetching Vehicles List...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
}

protected Void doInBackground(Void... args0)
{
     System.out.println("Login Street clamper ="+LoginActivity.street);
     System.out.println("Login Area clamper ="+LoginActivity.area);
     System.out.println("Login Uid clamper = "+LoginActivity.user_id);
     JSONParser jsonParser = new JSONParser();
        UserFunctions userFunction = new UserFunctions();
        String json = userFunction.clamperFetch(LoginActivity.street,   LoginActivity.area, LoginActivity.user_id);
        Log.d("Response:",json);
        if(json!=null)
        {
            try{
                JSONObject jsonObj = new JSONObject(json);
                jsonArray = jsonObj.getJSONArray("");
                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject c = jsonArray.getJSONObject(i);
                    String fineId = c.getString(FINE_ID);
                    String street2 = c.getString(STREET);
                    String area2 = c.getString(AREA);
                    String carNo = c.getString(CAR_NO);
                    HashMap<String,String>jsonArray = new HashMap<String,String>();
                    jsonArray.put(FINE_ID,fineId);
                    jsonArray.put(STREET, street2);
                    jsonArray.put(AREA, area2);
                    jsonArray.put(CAR_NO, carNo);
                    jsonlist.add(jsonArray);

                }
            }catch(JSONException e){
                e.printStackTrace();
            }
        }else {
            Log.e("JASON Parser", "Couldn't get any data from the url");
        }



       // Log.e("Fetching Response:>", json);
        System.out.println("Fetching Response" +json);

           return null;
}
protected void onPostExecute(final Boolean success) {
    // dismiss the dialog once done
    if(pDialog.isShowing())
    {
        pDialog.dismiss();
    }
    ListAdapter adapter = new SimpleAdapter(
            ClamperActivity.this,jsonlist,
            R.layout.list_item,
            new String[]{FINE_ID,STREET,AREA,CAR_NO},
            new int[]{R.id.carNo,R.id.fineId,R.id.area1,R.id.street1});
    setListAdapter(adapter);

}
}

}

这是我的用户功能类

public String clamperFetch(String street,String area, String user_id)
{
     List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("street",street));
        params.add(new BasicNameValuePair("area",area));
        params.add(new BasicNameValuePair("user_id",user_id));
        String json = jsonParser.getJSONStringFromUrl(url_fetch,params);

    return json;
}

activity_clamper.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.umoja_parking.ClamperActivity" >

<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentBottom="true" />

</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" >

<TextView
    android:id="@+id/carNo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/fineId"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/area1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />
<TextView
    android:id="@+id/street1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="2dip"
    android:paddingTop="6dip"
    android:textColor="#43bd00"
    android:textSize="16sp"
    android:textStyle="bold" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

执行以下操作:

更改

JSONObject data = jsonObj.getJSONObject(json); 

JSONArray data = jsonObj.getJSONArray(json);

json 是一个数组,而不是一个对象。因此,你得到一个例外。

<强>编辑:

for(int i=0; i<data.length(); i++)
{
     JSONObject obj=data.getJSONObject(i);
     String value1 = obj.getString(FINE_ID); 
        String value2 = obj.getString(WHATEVER YOU WANT); 
        System.out.println(value1);
        System.out.println(value2);
}

<强> P.S

[ represents JSON array node

{ represents JSON object node