【问题标题】:How to convert JSON stirng with multiple key values to JSON object in Android如何将具有多个键值的 JSON 字符串转换为 Android 中的 JSON 对象
【发布时间】:2016-06-24 07:15:31
【问题描述】:

这是我的 JSON 数据:

{  
   "alldata":[  
      {  
         "college1":{  
            "data":[  
               {  
                  "id":"1",
                  "name":"nithin",
                  "location":"alkd",
                  "time":"2016-06-24 12:02:28"
               },
               {  
                  "id":"2",
                  "name":"xxx",
                  "location":"sdsdvxc",
                  "time":"2016-06-21 12:49:38"
               },
               {  
                  "id":"3",
                  "name":"xcxc",
                  "location":"xzxzx",
                  "time":"2016-06-21 12:59:49"
               },
               {  
                  "id":"4",
                  "name":"ZA",
                  "location":"BU",
                  "time":"2016-06-23 13:04:25"
               },
               {  
                  "id":"5",
                  "name":"df",
                  "location":"ok",
                  "time":"2016-06-20{"                  id":"12",
                  "name":"vbnb",
                  "location":"sf",
                  "time":"2016-06-23 11:08:47"
               },
               {  
                  "id":"13",
                  "name":"az",
                  "location":"vgfjghgbhjj",
                  "time":"2016-06-24 12:13:44"
               }
            ],
            "last_date":"2016-06-24 12:13:44"
         }
      },
      {  
         "product1":{  
            "data":[  
               {  
                  "id":"1",
                  "productname":"soapq",
                  "quantity":"2",
                  "time":"2016-06-23 13:12:27"
               },
               {  
                  "id":"2",
                  "productname":"VCVC",
                  "quantity":"3",
                  "time":"2016-06-23 13:12:13"
               },
               {  
                  "id":"3",
                  "productname":"fym",
                  "quantity":"3",
                  "time":"2016-06-23 13:12:53"
               }
            ],
            "last_date":"2016-06-23 13:12:53"
         }
      }
   ]
}

我想将这些数据放入 Android 中的数组列表中。

【问题讨论】:

  • JSON 格式不正确,请更新。
  • 首先尝试你自己,做一些谷歌搜索。提前致谢
  • 此 JSON 响应未格式化。您可以使用任何在线 JSON 格式化程序对其进行检查。
  • arraylist 类变量 ??

标签: php android mysql json


【解决方案1】:

您的 JSON 数据无效,我根据需要对有效 JSON 数据进行了更正。 检查有效的 JSON 数据如下:

{
    "alldata": [{
        "college1": {
            "data": [{
                "id": "1",
                "name": "nithin",
                "location": "alkd",
                "time": "2016-06-24 12:02:28"
            }, {
                "id": "2",
                "name": "xxx",
                "location": "sdsdvxc",
                "time": "2016-06-21 12:49:38"
            }, {
                "id": "3",
                "name": "xcxc",
                "location": "xzxzx",
                "time": "2016-06-21 12:59:49"
            }, {
                "id": "4",
                "name": "ZA",
                "location": "BU",
                "time": "2016-06-23 13:04:25"
            }, {
                "id": "5",
                "name": "df",
                "location": "ok",
                "time": "2016-06-20"
            }, {
                "id": "12",
                "name": "vbnb",
                "location": "sf",
                "time": "2016-06-23 11:08:47"
            }, {
                "id": "13",
                "name": "az",
                "location": "vgfjghgbhjj",
                "time": "2016-06-24 12:13:44"
            }],
            "last_date": "2016-06-24 12:13:44"
        }
    }, {
        "product1": {
            "data": [{
                "id": "1",
                "productname": "soapq",
                "quantity": "2",
                "time": "2016-06-23 13:12:27"
            }, {
                "id": "2",
                "productname": "VCVC",
                "quantity": "3",
                "time": "2016-06-23 13:12:13"
            }, {
                "id": "3",
                "productname": "fym",
                "quantity": "3",
                "time": "2016-06-23 13:12:53"
            }],
            "last_date": "2016-06-23 13:12:53"
        }
    }]
}

然后试试这个教程 - http://www.tutorialspoint.com/android/android_json_parser.htm

【讨论】:

    【解决方案2】:

    亲爱的,现在你JSON 响应格式不正确,因此无法解析。 但是一旦你更正它,你就可以使用谷歌GSON库来解析数组。 在您的项目中包含 Google GSON 库,使用:compile 'com.google.code.gson:gson:2.3'

    您可以将 JSON 响应解析为。

    JSONObject objdataList = jsonObj.getJSONObject("data");
    JSONArray jsonDataList = objDataList.getJSONArray("alldata");
    ArrayList<AllData> records = new Gson().fromJson(jsonDataList.toString(), new TypeToken<List<AllData>>(){}.getType());
    

    快乐编码:)

    【讨论】:

      【解决方案3】:

      首先,你给的json有错误。从json中,我们可以使用gson来解析json,看起来像这样

          Gson gson = new Gson();
          Person person = gson.fromJson(responseString.toString(),Person.class);
          List<Person.AlldataBean> list = new ArrayList<Person.AlldataBean>();
          list.addAll(person.getAlldata());
      

      这样做之后,你可以得到列表对象,然后你可以从 列表对象。

      Person 类在这里

      公共类人{

      /**
       * college1 : {"data":[{"id":"1","name":"nithin","location":"alkd","time":"2016-06-24 12:02:28"},{"id":"2","name":"xxx","location":"sdsdvxc","time":"2016-06-21 12:49:38"},{"id":"3","name":"xcxc","location":"xzxzx","time":"2016-06-21 12:59:49"},{"id":"4","name":"ZA","location":"BU","time":"2016-06-23 13:04:25"},{"id":"5","name":"df","location":"ok","time":"2016-06-20"},{"id":"12","name":"vbnb","location":"sf","time":"2016-06-2311: 08: 47"},{"id":"13","name":"az","location":"vgfjghgbhjj","time":"2016-06-2412: 13: 44"}],"last_date":"2016-06-2412: 13: 44"}
       */
      
      private List<AlldataBean> alldata;
      
      public List<AlldataBean> getAlldata() {
          return alldata;
      }
      
      public void setAlldata(List<AlldataBean> alldata) {
          this.alldata = alldata;
      }
      
      public static class AlldataBean {
          /**
           * data : [{"id":"1","name":"nithin","location":"alkd","time":"2016-06-24 12:02:28"},{"id":"2","name":"xxx","location":"sdsdvxc","time":"2016-06-21 12:49:38"},{"id":"3","name":"xcxc","location":"xzxzx","time":"2016-06-21 12:59:49"},{"id":"4","name":"ZA","location":"BU","time":"2016-06-23 13:04:25"},{"id":"5","name":"df","location":"ok","time":"2016-06-20"},{"id":"12","name":"vbnb","location":"sf","time":"2016-06-2311: 08: 47"},{"id":"13","name":"az","location":"vgfjghgbhjj","time":"2016-06-2412: 13: 44"}]
           * last_date : 2016-06-2412: 13: 44
           */
      
          private College1Bean college1;
      
          public College1Bean getCollege1() {
              return college1;
          }
      
          public void setCollege1(College1Bean college1) {
              this.college1 = college1;
          }
      
          public static class College1Bean {
              private String last_date;
              /**
               * id : 1
               * name : nithin
               * location : alkd
               * time : 2016-06-24 12:02:28
               */
      
              private List<DataBean> data;
      
              public String getLast_date() {
                  return last_date;
              }
      
              public void setLast_date(String last_date) {
                  this.last_date = last_date;
              }
      
              public List<DataBean> getData() {
                  return data;
              }
      
              public void setData(List<DataBean> data) {
                  this.data = data;
              }
      
              public static class DataBean {
                  private String id;
                  private String name;
                  private String location;
                  private String time;
      
                  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 getLocation() {
                      return location;
                  }
      
                  public void setLocation(String location) {
                      this.location = location;
                  }
      
                  public String getTime() {
                      return time;
                  }
      
                  public void setTime(String time) {
                      this.time = time;
                  }
              }
          }
      }
      

      }

      【讨论】:

        猜你喜欢
        • 2021-06-09
        • 2015-10-07
        • 2017-08-03
        • 2018-06-09
        • 2018-01-06
        • 1970-01-01
        • 2014-11-28
        • 1970-01-01
        • 2022-12-09
        相关资源
        最近更新 更多