org.json.JSON.typeMismatch(JSON.java:111)

时间:2018-05-26 09:57:13

标签: android arrays json string

尝试创建org.json.JSON.typeMismatch(JSON.java:111)数组时出现JSON错误。收到的数据如下所示:

{
 "result":[
  {
   "@type":"d",
   "@rid":"#-2:0",
   "@version":0,
   "a_adres":"kepez merkez geliyor herkez",
   "takipkodu":"464664",
   "agirlik":50
  },
  {
   "@type":"d",
   "@rid":"#-2:1",
   "@version":0,
   "a_adres":"merkez mahallesi 4 sok",
   "takipkodu":"sd4fs654f64",
   "agirlik":60
  }
 ]
}

代码:

 protected String doInBackground(String... urls) {
        try {
            String getResponse = get("http://178.18.206.128:2480/command/nakliye/sql/select a_adres,takipkodu,agirlik from yukilan");
            return getResponse;
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(String getResponse) {
        ListView liste=(ListView)findViewById(R.id.list_view);

        try {
            JSONArray yukilanarray =  new JSONArray(getResponse);
            List<Yukilan> yukilanlar = new ArrayList<>();
            //Populate the EmployeeDetails list from response
            for (int i = 0; i<yukilanarray.length();i++){
                Yukilan yeniilan = new Yukilan();
                JSONObject yukilanobject = yukilanarray.getJSONObject(i);
                yeniilan.setAdres(yukilanobject.getString(KEY_ADRES));
                yeniilan.setAgirlik(yukilanobject.getString(KEY_AGIRLIK));
                yeniilan.setTakip_kod(yukilanobject.getString(KEY_TAKIPKODU));

                yukilanlar.add(yeniilan);
            }
            //Create an adapter with the EmployeeDetails List and set it to the LstView
            yukilanadapter = new Yukilanadapter(yukilanlar,getApplicationContext());
            liste.setAdapter(yukilanadapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
       //  System.out.println(getResponse);
    }

请帮忙。

1 个答案:

答案 0 :(得分:0)

我认为你在阅读json时犯了一个错误。第一个元素是JSONObject而不是JSONArray.Below是修改后的代码。

   protected String doInBackground(String... urls) {
        try {
            String getResponse = get("http://178.18.206.128:2480/command/nakliye/sql/select a_adres,takipkodu,agirlik from yukilan");
            return getResponse;
        } catch (Exception e) {
            this.exception = e;
            return null;
        }
    }

    protected void onPostExecute(String getResponse) {
        ListView liste=(ListView)findViewById(R.id.list_view);

        try {
            JSONObject jsonObject = new JSONObject(getResponse);
            JSONArray yukilanarray =  jsonObject.getJSONArray("result");
            List<Yukilan> yukilanlar = new ArrayList<>();
            //Populate the EmployeeDetails list from response
            for (int i = 0; i<yukilanarray.length();i++){
                Yukilan yeniilan = new Yukilan();
                JSONObject yukilanobject = yukilanarray.getJSONObject(i);
                yeniilan.setAdres(yukilanobject.getString(KEY_ADRES));
                yeniilan.setAgirlik(yukilanobject.getString(KEY_AGIRLIK));
                yeniilan.setTakip_kod(yukilanobject.getString(KEY_TAKIPKODU));

                yukilanlar.add(yeniilan);
            }
            //Create an adapter with the EmployeeDetails List and set it to the LstView
            yukilanadapter = new Yukilanadapter(yukilanlar,getApplicationContext());
            liste.setAdapter(yukilanadapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
// System.out.println(getResponse);

    }