【问题标题】:Correct JSON response for Retrofit+GSON?Retrofit+GSON 的正确 JSON 响应?
【发布时间】:2017-06-23 06:22:55
【问题描述】:

我对我的一项网络服务有以下响应,我正在使用 Retrofit 和 GSON。

    {
    "error": false,
    "Timeline": {
        "Date": "2040-06-15",
        "bandList": {
            "breakfast": {
                "dosageList": {
                    "01": {
                        "packed": "true",
                        "medicineList": [
                            {
                                "medicine": {
                                    "id": "01",
                                    "name": "glipizide 5 mg tablet, 100 ",
                                    "category": "regular",
                                    "image": null,
                                    "indication": "NIDDM",
                                    "packed": true,
                                    "med_id": "352",
                                    "dosage": 1
                                }
                            },
                            {
                                "medicine": {
                                    "id": "04",
                                    "name": "Frusemide (Terry White Chemists) 20 mg uncoated tablet, 100 ",
                                    "category": "regular",
                                    "image": null,
                                    "indication": "Fluid",
                                    "packed": true,
                                    "med_id": "4",
                                    "dosage": 2
                                }
                            }
                        ]
                    },
                    "02": {
                        "packed": "false",
                        "medicineList": [
                            {
                                "medicine": {
                                    "id": "05",
                                    "name": "Refresh Tears Plus 0.5% eye drops solution, 15 mL ",
                                    "category": "regular",
                                    "image": null,
                                    "indication": "Dry Eyes",
                                    "packed": false,
                                    "med_id": "372",
                                    "dosage": 1
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

第一季度。 有没有办法使用模型类(POJO)或不使用它们来解析上述响应?我坚持为上述结构生成模型类。如何为上述 JSON 生成 POJO?

第二季度。我处于如何说服发送以下响应的位置,JSON 的正确结构/格式是什么?是否有任何 JSON 标准可以向 Web 开发人员展示以获取这种 JSON 格式? (注:我可以解析这个结构)

{
"error": false,
"Timeline": {
    "Date": "2040-06-15",
    "band": [
        {
            "name": "breakfast",
            "dosage": [
                {
                    "id": "01",
                    "packed": "true",
                    "medicine": [
                        {
                            "id": "01",
                            "name": "glipizide 5 mg tablet, 100 ",
                            "category": "regular",
                            "image": null,
                            "indication": "NIDDM",
                            "packed": true,
                            "med_id": "52",
                            "dosage": 1
                        },
                        {
                            "id": "04",
                            "name": "Frusemide (Terry White Chemists) 20 mg uncoated tablet, 100 ",
                            "category": "regular",
                            "image": null,
                            "indication": "Fluid",
                            "packed": true,
                            "med_id": "54",
                            "dosage": 2
                        }
                    ]
                },
                {
                    "id": "02",
                    "packed": "false",
                    "medicine": [
                        {
                            "id": "05",
                            "name": "Refresh Tears Plus 0.5% eye drops solution, 15 mL ",
                            "category": "regular",
                            "image": null,
                            "indication": "Dry Eyes",
                            "packed": false,
                            "med_id": "372",
                            "dosage": 1
                        }
                    ]
                }
            ]
        }
    ]
}

}

提前谢谢你。

编辑

我曾经使用这些站点自动生成 POJO,但它对某些类给出了以下响应。如何将其转换为正确的类?

package ;
public class DosageList
{
    private 01 01;

    private 02 02;

    public void set01(01 01){
        this.01 = 01;
    }
    public 01 get01(){
        return this.01;
    }
    public void set02(02 02){
        this.02 = 02;
    }
    public 02 get02(){
        return this.02;
    }
}

编辑 2

我几乎完成了第一个 JSON 的解析,但卡在了这里。

for (String bandName: event.getTimeline().getBand().keySet()) {

  Log.d("<<<--Band-->>>", "Value " + event.getTimeline().getBand().get(bandName));
  Band band = event.getTimeline().getBand().get(bandName);
  for (String dosageName:band.getDosage().keySet()) {
     Dosage dosage = band.getDosage().get(dosageName);
     Log.d("<<<--Dosage-->>>", "Value " + dosage.getMedicine());
          for (Medicine medicine: dosage.getMedicine()) {
               Log.d("<<<--Medicine-->>>", "Value " + dosage.getMedicine().get(0));
          }
   }

}

如何检索医学值?

【问题讨论】:

    标签: java android json retrofit2 gson


    【解决方案1】:
     public class Medicine
         {
        private String id;
    
    private String name;
    
    private String category;
    
    private String image;
    
    private String indication;
    
    private boolean packed;
    
    private String med_id;
    
    private int dosage;
    
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setCategory(String category){
        this.category = category;
    }
    public String getCategory(){
        return this.category;
    }
    public void setImage(String image){
        this.image = image;
    }
    public String getImage(){
        return this.image;
    }
    public void setIndication(String indication){
        this.indication = indication;
    }
    public String getIndication(){
        return this.indication;
    }
    public void setPacked(boolean packed){
        this.packed = packed;
    }
    public boolean getPacked(){
        return this.packed;
    }
    public void setMed_id(String med_id){
        this.med_id = med_id;
    }
    public String getMed_id(){
        return this.med_id;
    }
    public void setDosage(int dosage){
        this.dosage = dosage;
    }
    public int getDosage(){
        return this.dosage;
    }
    }
    
    
    
    
    
    
       import java.util.ArrayList;
     import java.util.List;
     public class Dosage
      {
    private String id;
    
    private String packed;
    
    private List<Medicine> medicine;
    
    public void setId(String id){
        this.id = id;
    }
    public String getId(){
        return this.id;
    }
    public void setPacked(String packed){
        this.packed = packed;
    }
    public String getPacked(){
        return this.packed;
    }
    public void setMedicine(List<Medicine> medicine){
        this.medicine = medicine;
    }
    public List<Medicine> getMedicine(){
        return this.medicine;
    }
     }
    
    
     import java.util.ArrayList;
     import java.util.List;
     public class Band
      {
    private String name;
    
    private List<Dosage> dosage;
    
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return this.name;
    }
    public void setDosage(List<Dosage> dosage){
        this.dosage = dosage;
    }
    public List<Dosage> getDosage(){
        return this.dosage;
    }
      }
    
    
     import java.util.ArrayList;
      import java.util.List;
       public class Timeline
      {
    private DateTime Date;
    
    private List<Band> band;
    
    public void setDate(DateTime Date){
        this.Date = Date;
    }
    public DateTime getDate(){
        return this.Date;
    }
    public void setBand(List<Band> band){
        this.band = band;
    }
    public List<Band> getBand(){
        return this.band;
    }
     }
    
    
     public class Root
     {
    private boolean error;
    
    private Timeline Timeline;
    
    public void setError(boolean error){
        this.error = error;
    }
    public boolean getError(){
        return this.error;
    }
    public void setTimeline(Timeline Timeline){
        this.Timeline = Timeline;
    }
    public Timeline getTimeline(){
        return this.Timeline;
    }
      }
    

    ...享受...

    【讨论】:

    • 嗨,Sanjana,在您和其他人的帮助下,我几乎完成了。请告诉我如何获得药物价值? (请检查我的编辑 2)
    【解决方案2】:

    第一个问题:

    您可以在没有 POJO 类的情况下解析 JSON,但建议使用 他们和关于你坚持从 JSON 生成它们 你可以用jsonschema2pojo我想 这是最好的。

    第二个:

    是的,有 JSON 标准,您可以在 JSON 网站上找到它们。

    【讨论】:

    • 感谢您的回答。请检查我编辑的问题。
    【解决方案3】:

    第 1 步:首先从我能看到的最里面的 json 对象开始,它是“药”

    创建一个类似这样的 POJO 类

    public class Medicine implements android.os.Parcelable {
    
    @SerializedName("id")
    private String id;
    
    // Getter setter method for id
    // Do it for all the JSON tags
    
    }
    

    第 2 步:为“medicineList”创建一个类,这有点像这样

    public class MedicineList implements Parcelable {
    
    @SerializedName("medicineList")
    private List<Medicine > medicine;
    
    // Getter setter can go here
    }
    

    以类似的方式,在 JSON 响应中移动到您的基本标记。这让我很容易理解。我不会发布完整的解决方案,因为那是你在 EOD 的作业。

    【讨论】:

    • 感谢您的回答。请检查我编辑的问题。
    【解决方案4】:

    第一季度的解决方案 -

    是的,您可以通过在 Android Studio 中安装 DTO 插件,使用模型类解析上述响应。该插件将自动为响应创建 POJO 类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2015-06-15
      • 2023-04-01
      相关资源
      最近更新 更多