【问题标题】:Can not parse JSON array to objects无法将 JSON 数组解析为对象
【发布时间】:2016-04-09 03:52:28
【问题描述】:

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 日志,它进入错误捕获。

但是当我删除 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 分钟,而且最终几乎不会失败。为什么会这样?

【问题讨论】:

    标签: php android json


    【解决方案1】:

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

    JSONObject jsono = new JSONObject(json);
    

    你可以从中拉出物品并取得成功:

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

    【讨论】:

    • 这段代码可以正常工作,但有时会出现 try catch 错误,我看不到任何错误。有时它需要很长时间,大约 1-2 分钟,而且最终几乎不会失败。为什么会这样?
    【解决方案2】:

    使用此代码

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

    【讨论】:

      【解决方案3】:

      首先创建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);
       }
      

      【讨论】:

      • 为什么要将其保存为 json 数据,并在需要时从 json 中获取数据?
      【解决方案4】:

      替换您的JSONArray mJsonArray = new JSONArray(sb) 代码

      JSONObject jsnobject = new JSONObject(sb);
      

      因为你的 json 数据第一个标签是 JSONObject 而不是 JSONArray ,试试它会为你工作

      【讨论】:

      • 这段代码可以正常工作,但有时会出现 try catch 错误,我看不到任何错误。有时它需要很长时间,大约 1-2 分钟,而且最终几乎不会失败。为什么会这样?
      【解决方案5】:

      试试这个 解析 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();
              }
      

      【讨论】:

      • 为什么要将其保存为 json 数据,并在需要时从 json 中获取数据?
      • 它是json格式的服务器响应。您可以在需要时获取数据。
      • 对不起,我的意思是为什么不保留为 json
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-02
      相关资源
      最近更新 更多