【问题标题】:How to parse json Data Android如何解析json数据Android
【发布时间】:2019-07-21 20:18:53
【问题描述】:

如何将同名系列解析为一个数组列表

所以我得到了第 1 季和第 2 季的标题名称

最好的方法是什么

我的 Json 数据

{
"series":[
{
"title":"Jumping cat", "genre":"comedy", "year":2018, "season":1, "imdb":7, "info":"comdey series", "episodes":10, "cover":"poster" }, {
"title":"Jumping cat", "genre":"comedy", "year":2019, "season":2, "imdb":7, "info":"comdey series", "episodes":11, "cover":"poster" } ] }

【问题讨论】:

    标签: android json android-volley


    【解决方案1】:

    以下代码将创建一个带有字符串键和 ArrayList 值的“HashMap”。 ArrayList 包含每个系列的模型:

    try{
        JSONObject reader = new JSONObject(str);
        JSONArray array = reader.optJSONArray("series");
        HashMap<String, ArrayList<YourModel>> map =  new HashMap<>();
        for(int i=0;i<array.length();i++){
            JSONObject innerObject = array.getJSONObject(i);
            if(map.get(innerObject.getString("title")) != null){ // check if the title already exists, then add it to it's list
                ArrayList<YourModel> arrayList = map.get(innerObject.getString("title"));
                arrayList.add(new YourModel(innerObject));
            }else{ // if the title does not exist, create new ArrayList
                ArrayList<YourModel> arrayList = new ArrayList<>();
                arrayList.add(new YourModel(innerObject));
                map.put(innerObject.getString("title"),arrayList);
            }
        }
    }catch (JSONException e){
        // Do error handling
    }
    

    【讨论】:

      【解决方案2】:

      如果您不想添加其他第 3 方。您可以在几行内完成此操作。是的,它是手工劳动,但它会节省大量添加到 APK 中的字节码。

          public class Serie {
              public String title;
              public String genere;
              public int year;
              public int season;
              public int imdb;
              public String info;
              public int episodes;
              public String cover;
      
              public static Serie toObject(JSONObject o) throws JSONException {
                  Serie s = new Serie();
                  s.title = o.getString("title");
                  s.genere = o.getString("genre");
                  s.year = o.getInt("year");
                  s.season = o.getInt("season");
                  s.imdb = o.getInt("imdb");
                  s.info = o.getString("info");
                  s.episodes = o.getInt("episodes");
                  s.cover = o.getString("cover");
                  return s;
              }
      
               public static List<Serie> toArray(String json) throws JSONException {
                  JSONObject oo = new JSONObject(json);
                  JSONArray a = oo.getJSONArray("series");
                  List<Serie> l = new ArrayList<>(a.length());
                  for (int i =0; i<a.length(); i++ ) {
                      JSONObject o = a.getJSONObject(i);
                      l.add(Serie.toObject(o));
                  }
                  return l;
              }
          }
      
          // usage 
          try {
             List<Serie> ll = Serie.toArray(s);
             System.out.println(ll.toString());
          } catch (JSONException e) {
             e.printStackTrace();
          }
      

      【讨论】:

        【解决方案3】:

        要获取 Json 的信息,您总是需要两件事

        1. POJO 的模型类 ob 您将获得的对象
        2. 选择要使用的 JsonParser(Java 原生的 JsonParser 或第三方的 Gson)

        希望对你有帮助:D

        【讨论】:

          【解决方案4】:

          您的回复以列表开头

          @SerializedName("series")
          @Expose
          private List<Series> series = null;
          

          列出模型类

          @SerializedName("title")
          @Expose
          private String title;
          @SerializedName("genre")
          @Expose
          private String genre;
          @SerializedName("year")
          @Expose
          private Integer year;
          @SerializedName("season")
          @Expose
          private Integer season;
          @SerializedName("imdb")
          @Expose
          private Integer imdb;
          @SerializedName("info")
          @Expose
          private String info;
          @SerializedName("episodes")
          @Expose
          private Integer episodes;
          @SerializedName("cover")
          @Expose
          private String cover;
          

          并创建getter setter方法

          【讨论】:

            【解决方案5】:

            您可以使用 Google 的 gson 库来简单地将 json 解析为 java 类,反之亦然。找到了一个如何使用gson的例子here

            【讨论】:

              猜你喜欢
              • 2016-11-19
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-12-31
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多