纠正Retrofit + GSON的JSON响应?

时间:2017-06-23 06:22:56

标签: java android json retrofit2 gson

我对我的某个网络服务有以下回应,并且我使用了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
                                }
                            }
                        ]
                    }
                }
            }
        }
    }
}

Q1。 有没有办法使用模型类(POJO)解析上面的响应或没有它们?我坚持为上述结构生成模型类。如何为JSON生成POJO?

Q2。我能够说服发送以下回复,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));
          }
   }

}

如何检索药物值?

4 个答案:

答案 0 :(得分: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;
}
  }

...享受...

答案 1 :(得分:0)

对于第一个问题:

  

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

第二个:

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

答案 2 :(得分:0)

步骤1:首先从最内层的json对象开始,我可以看到&#34; medicine&#34;

创建一个像这样的POJO类

uninstall

第2步:为&#34; medicineList&#34;创建一个类。这有点像这样

public class Medicine implements android.os.Parcelable {

@SerializedName("id")
private String id;

// Getter setter method for id
// Do it for all the JSON tags

}

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

答案 3 :(得分:0)

Q1的解决方案 -

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