无法将JSON数组解析为对象

时间:2016-04-09 03:52:28

标签: php android json

Android parse JSONObject

我已经搜索过这样的问题,但无法取得成功。

我有JSON数据 http://oep.esy.es/item_connect.php

{
   "items":[
      {
         "id":"7",
         "name":"bir",
         "image":"6051-1.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"8",
         "name":"d\u00f6rt",
         "image":"1727-4.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"9",
         "name":"iki",
         "image":"2667-2.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"10",
         "name":"\u00fc\u00e7",
         "image":"9998-3.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"11",
         "name":"be\u015f",
         "image":"7005-5.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"12",
         "name":"alt\u0131",
         "image":"9377-6.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"turuncu"
      },
      {
         "id":"13",
         "name":"elma",
         "image":"1656-1001.png",
         "number":"1",
         "shape":"\u015eekilsiz",
         "color":"k\u0131rm\u0131z\u0131"
      }
   ],
   "success":1
}

首先是项目的词,最后有成功的词。有阵列。当我删除它们时,我就能成功。

StringBuilder sb = new StringBuilder();

sb.append(json+"\n");//Log.d(TAG, "sb "+sb.toString());
}

try {Log.d(TAG, "xx ");
      JSONArray mJsonArray = new JSONArray(sb);Log.d(TAG, "xx x");
      JSONObject mJsonObject = new JSONObject();

这里它不能去xx x log,它会进入错误捕获。

但是当我删除页眉和页脚形式json时,它的工作原理。我应该使用字符串操作来删除那些还是正则表达式?

编辑:

我解决了这样的问题:

 try {Log.d(TAG, "xx ");
       JSONArray(sb);Log.d(TAG, "xx x");
                    JSONObject mJsonObject = new JSONObject();//i use this to get invidial value of json, do i need?

                    JSONObject jsono = new JSONObject(sb.toString());

                    Log.d(TAG,"jsono "+jsono.toString());

                    JSONArray items = jsono.getJSONArray("items");


                    Log.d(TAG,"items "+items);
                    Log.d(TAG,"items length "+items.length());

                           for (int i = 0; i < items.length(); i++) {


                           //   Log.d(TAG, "xx " +mJsonObject.getString("image"));
                               Log.d(TAG, "string: "+items.getJSONObject(i).getString("image"));//i need image urls
                               }

此代码可以正常工作,但有时会出现try catch错误,我无法看到任何错误。有时需要很长时间,大约1-2分钟,最后大部分时间都会失败。为什么会这样?

5 个答案:

答案 0 :(得分:0)

整个响应是一个JSONObject,所以改为这样解析:

JSONObject jsono = new JSONObject(json);

你可以从中取出物品和成功:

JSONArray items = jsono.getJSONArray("items");
int success = jsono.getInt("success");

答案 1 :(得分:0)

使用此代码

Gson mGson = new Gson(); mGson.fromJson(jsonResponse.getJSONObject(i).toString(),DataChartDetailsInquiry.class);

答案 2 :(得分:0)

试试这个 解析你的json的方法

public class Modelclass {

    String id;
    String name;
    String image;
    String number;
    String shape;
    String color;


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getShape() {
        return shape;
    }

    public void setShape(String shape) {
        this.shape = shape;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

JSON解析代码

       ArrayList<Modelclass> arrayList = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(jsonstring);
            JSONArray jsonArray = jsonObject.getJSONArray("items");
            Modelclass modelclass;
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject json_child_obje = jsonArray.getJSONObject(i);
                modelclass = new Modelclass();
                modelclass.setId(json_child_obje.getString("id"));
                modelclass.setId(json_child_obje.getString("name"));
                modelclass.setId(json_child_obje.getString("image"));
                modelclass.setId(json_child_obje.getString("number"));
                modelclass.setId(json_child_obje.getString("shape"));
                modelclass.setId(json_child_obje.getString("color"));
                arrayList.add(modelclass);
            }
            Log.d("ArraylistSize", "" + arrayList.size());
        } catch (JSONException e) {
            e.printStackTrace();
        }

答案 3 :(得分:0)

首先创建一个arraylist来存储你的数据

ArrayList<List_items> list_items_arr = new ArrayList<List_items>();

为Lisr_items创建类

      public class List_items{
       String item_id,item_name;
    public void setItem_id(String item_id) {
            this.item_id = item_id;
        }

   public void setItem_name(String item_name) {
        this.item_name = item_name;
      }
  }

从服务器获取响应并解析它

JSONObject jsono = new JSONObject(json);

JSONArray items = jsono.getJSONArray("items");

  for(int i=0;i<items.length();i++){
         JSONObject list_obj=items.getJSONObject(i);
            List_items list_items = new List_items();
            list_items.setItem_name(list_obj.getString("id"));
            list_items.setItem_sku(list_obj.getString("name"));
            list_items.setItem_id(list_obj.getString("image"));
     list_items_arr .add(list_items);
 }

答案 4 :(得分:0)

替换您的UnsafeCell代码

JSONArray mJsonArray = new JSONArray(sb)

因为你的json数据First标签是JSONObject而不是JSONArray,试试它会对你有用