显示JSON数据列表视图视图、数据、列表、JSON

2023-09-03 22:35:50 作者:百度在牛╰也搜不到回忆

我想显示从JSON数组数据为的ListView 。但它给了我一个例外:

org.json.JSONException:值[{fine_id:51771,街:汤姆姆博亚,区:上城,car_no:MP08MF3299 }]类型org.json.JSONArray不能转换为JSONObject的

我在哪里做的错误,请帮我解决它的问题。

  8月8号至21号:33:54.640:E / JSON(943):
[
{
  car_no:MP08MF3299
 fine_id:51771
 ,区:小区
 街道:汤姆姆博亚
}
]
八月八日至21号:33:54.640:D /响应:(943):[{car_no:MP08MF3299,fine_id:51771,面积:小区,街道:汤姆姆博亚}]
8月8号至21日:33:54.650:W / System.err的(943):
 org.json.JSONException:
值
[
 {
  fine_id:51771
 街道:汤姆姆博亚
 ,区:小区
 car_no:MP08MF3299
 }
类型] org.json.JSONArray无法转换到的JSONObject
8月8号至21日:33:54.680:W / System.err的(943):在org.json.JSON.typeMismatch(JSON.java:111)
8月8号至21日:33:54.680:W / System.err的(943):在org.json.JSONObject< INIT>(JSONObject.java:159)
8月8号至21日:33:54.740:W / System.err的(943):在org.json.JSONObject< INIT>(JSONObject.java:172)
8月8号至21日:33:54.740:W / System.err的(943):在com.example.umoja_parking.ClamperActivity $ FetchVehiclesTask.doInBackground(ClamperActivity.java:182)
8月8号至21日:33:54.780:W / System.err的(943):在com.example.umoja_parking.ClamperActivity $ FetchVehiclesTask.doInBackground(ClamperActivity.java:1)
8月8号至21日:33:54.780:W / System.err的(943):在android.os.AsyncTask $ 2.call(AsyncTask.java:288)
8月8号至21日:33:54.790:W / System.err的(943):在java.util.concurrent.FutureTask.run(FutureTask.java:237)
8月8号至21日:33:54.830:W / System.err的(943):在android.os.AsyncTask $ SerialExecutor $ 1.运行(AsyncTask.java:231)
8月8号至21日:33:54.830:W / System.err的(943):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
8月8号至21日:33:54.840:W / System.err的(943):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587)
8月8号至21日:33:54.840:W / System.err的(943):在java.lang.Thread.run(Thread.java:841)
8月8号至21日:33:54.880:我/的System.out(943):正在提取响应[{car_no:MP08MF3299,fine_id:51771,区:上城,街: 汤姆姆博亚}]
 

这是我的活动code

 公共类ClamperActivity扩展ListActivity {


JSONParser jsonParser =新JSONParser();

私有静态最后弦乐CAR_NO =car_no;
私有静态最后弦乐FINE_ID =fine_id;
私有静态最后弦乐面积=区域;
私有静态最后弦乐STREET =一条街;

JSONArray jsonArray = NULL;

 ArrayList的< HashMap的<字符串,字符串>> jsonlist;

 公共静态字符串USER_ID =;

 静态字符串URL =;

// JSON节点名称
私有静态最后弦乐TAG_SUCCESS =成功;

@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_clamper);
jsonlist =新的ArrayList< HashMap的<字符串,字符串>>();
ListView的LV = getListView();

新FetchVehiclesTask()执行()。

}

类FetchVehiclesTask扩展的AsyncTask<虚空,虚空,虚空>
{
私人ListActivity的活动;

在preExecute保护无效(){
    super.on preExecute();
    pDialog =新ProgressDialog(ClamperActivity.this);
    pDialog.setMessage(撷取车辆列表...);
    pDialog.setIndeterminate(假);
    pDialog.setCancelable(真正的);
    pDialog.show();
}

保护无效doInBackground(空... args0)
{
     的System.out.println(登录街夹具=+ LoginActivity.street);
     的System.out.println(登陆区域夹具=+ LoginActivity.area);
     的System.out.println(登录的Uid夹具=+ LoginActivity.user_id);
     JSONParser jsonParser =新JSONParser();
        UserFunctions userFunction =新UserFunctions();
        JSON字符串= userFunction.clamperFetch(LoginActivity.street,LoginActivity.area,LoginActivity.user_id);
        Log.d(回应:JSON);
        如果(JSON!= NULL)
        {
            尝试{
                JSONObject的jsonObj =新的JSONObject(JSON);
                jsonArray = jsonObj.getJSONArray();
                的for(int i = 0; I< jsonArray.length();我++)
                {
                    JSONObject的C = jsonArray.getJSONObject(我);
                    字符串fineId = c.getString(FINE_ID);
                    字符串street2 = c.getString(街道);
                    字符串区域2 = c.getString(区);
                    字符串carNo = c.getString(CAR_NO);
                    HashMap的<字符串,字符串> jsonArray =新的HashMap<字符串,字符串>();
                    jsonArray.put(FINE_ID,fineId);
                    jsonArray.put(街道,street2);
                    jsonArray.put(区,区2);
                    jsonArray.put(CAR_NO,carNo);
                    jsonlist.add(JSONArray),其中,

                }
            }赶上(JSONException E){
                e.printStackTrace();
            }
        }其他 {
            Log.e(JASON分析器,无法从该网址的任何数据);
        }



       // Log.e(撷取回应:>中,JSON);
        的System.out.println(抓取,响应+ JSON);

           返回null;
}
保护无效onPostExecute(最终布尔成功){
    //驳回一旦完成对话
    如果(pDialog.isShowing())
    {
        pDialog.dismiss();
    }
    ListAdapter适配器=新SimpleAdapter(
            ClamperActivity.this,jsonlist,
            R.layout.list_item,
            新的String [] {FINE_ID,街道,区,CAR_NO},
            新的INT [] {R.id.carNo,R.id.fineId,R.id.area1,R.id.street1});
    setListAdapter(适配器);

}
}

}
 

这是我的用户函数类

 公共字符串clamperFetch(街道字符串,字符串面积,字符串USER_ID)
{
     名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
        params.add(新BasicNameValuePair(街,街道));
        params.add(新BasicNameValuePair(区,区));
        params.add(新BasicNameValuePair(user_ID的,USER_ID));
        JSON字符串= jsonParser.getJSONStringFromUrl(url_fetch,则params);

    返回JSON;
}
 
把json的值赋给列表里,不是一条数据,怎么样能显示出来

activity_clamper.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=com.example.umoja_parking.ClamperActivity>

<的ListView
    机器人:ID =@机器人:ID /列表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentLeft =真
    机器人:layout_alignParentTop =真
    机器人:layout_alignParentRight =真
    机器人:layout_alignParentBottom =真/>

< / RelativeLayout的>
 

list_item.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直
机器人:填充=10dp
机器人:以下属性来=10dp
机器人:paddingRight =10dp>

<的TextView
    机器人:ID =@ + ID / carNo
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:paddingBottom会=2DIP
    机器人:paddingTop =6dip
    机器人:文字颜色=#43bd00
    机器人:TEXTSIZE =16SP
    机器人:TEXTSTYLE =黑体/>
<的TextView
    机器人:ID =@ + ID / fineId
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:paddingBottom会=2DIP
    机器人:paddingTop =6dip
    机器人:文字颜色=#43bd00
    机器人:TEXTSIZE =16SP
    机器人:TEXTSTYLE =黑体/>
<的TextView
    机器人:ID =@ + ID /区1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:paddingBottom会=2DIP
    机器人:paddingTop =6dip
    机器人:文字颜色=#43bd00
    机器人:TEXTSIZE =16SP
    机器人:TEXTSTYLE =黑体/>
<的TextView
    机器人:ID =@ + ID / street1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:paddingBottom会=2DIP
    机器人:paddingTop =6dip
    机器人:文字颜色=#43bd00
    机器人:TEXTSIZE =16SP
    机器人:TEXTSTYLE =黑体/>

< / LinearLayout中>
 

解决方案

执行以下操作:

修改

 的JSONObject数据= jsonObj.getJSONObject(JSON);
 

  JSONArray数据= jsonObj.getJSONArray(JSON);
 

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

编辑:

 的for(int i = 0; I< data.length();我++)
{
     JSONObject的OBJ = data.getJSONObject(我);
     字符串值1 = obj.getString(FINE_ID);
        字符串值2 = obj.getString(任何你想要的);
        的System.out.println(值1);
        的System.out.println(值2);
}
 

P.S

  [重presents JSON数组节点

{再presents JSON对象节点
 

I am trying to display data from JSON array to ListView. But it gives me an exception:

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.

Where I am doing mistake please help me to fix it the problem

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"}]

here is my activity code

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

}
}

}

here is my user function class

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>

解决方案

Do the following:

Change

JSONObject data = jsonObj.getJSONObject(json); 

to

JSONArray data = jsonObj.getJSONArray(json);

json is an array, not an object. Thus you are getting an exception.

Edited:

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