【问题标题】:How to handle multiple JSON Object at once using java in Android Studio如何在 Android Studio 中使用 java 一次处理多个 JSON 对象
【发布时间】:2021-01-19 13:57:41
【问题描述】:

我在使用 Volley 的 Recycler Adapter 中一次处理多个 JSON 对象(类)时遇到问题。我尝试了两种方法来解决这个问题,但未能为我的 Recycler 视图获取输出。

第一种方式:- 我创建了一个主类(父类),其中其他 JSON 类扩展了主类,并通过使用主类,我使用多态性一一调用了所有子类。 例如:-

Main.java

public class Main{

}

Model1.java

public class Model1 extends Main {

        @SerializedName("$t")
        @Expose
        private String $t;

        public String get$t() {
            return $t;
        }

        public void set$t(String $t) {
            this.$t = $t;
        }

    }

Model2.java

public class Model2 extends Main {

       @SerializedName("$t")
       @Expose
       private String $t;

       public String get$t() {
           return $t;
       }

       public void set$t(String $t) {
           this.$t = $t;
       }
}

and during call I used

主要参考;

((Model1) (ref)).get$t();

((Model2) (ref)).get$t();

But, Failed to get all value(Please correct if I am worng in code)

and 2nd way:- Just making all the model class as inner class(static) of the main class and calling using

Main.Model1.get$t();

But did get output. Please suggest me any other way where I can use to get my expected output.

【问题讨论】:

    标签: java android android-studio android-recyclerview android-volley


    【解决方案1】:

    你的意思是这样的对象吗?

    {
      "Name": "xxxxx",
      "Age": 27,
      "Parents": [
        {
          "Name": "xxxx",
          "Age": 49
        },
        {
          "Name": "xxxxxx",
          "Age": 45
        }
      ],
      "Location": {
        "Address": "xxxx Sunset Ave.",
        "City": "xxxx"
      }
    }
    

    您可以尝试创建这样的模型。

    Person.java

    public class Person {
        @SerializedName("Name")
        public String name;
    
        @SerializedName("Age")
        public String age;
    
        @SerializedName("Parents")
        public List<Person> parents;
    
        @SerializedName("Location")
        public Location location;
    }
    

    位置.java

    public class Location {
        @SerializedName("Address")
        public String address;
    
        @SerializedName("City")
        public String city;
    }
    

    【讨论】:

    • 谢谢,但我的实际问题是如何在 adpter 类的保存时间同时使用这两个类(Person.java 和 Location.java)。意味着我想同时从他们那里获取数据以供我查看。
    猜你喜欢
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 2022-08-10
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多