如何从POJO转换此JSON文件,反之亦然

时间:2018-12-13 10:16:05

标签: java android json gson

library(tidyverse)
df1 %>%
    mutate_if(is.numeric, funs(replace(., . < 0, NA)))

我在JSON字符串上面有这个,我需要它作为Android代码的输出,以及实现它的方法。

上面的输出具有复杂的“ Complaint_retuarn”和“ Complaint_product” 实体,如何使用POJO类中的实体进行转换?

2 个答案:

答案 0 :(得分:1)

我使用以下在线工具将您的JSON转换为POJO; http://www.jsonschema2pojo.org/

下面是生成的JSON(带有Gson注释)。首先是 D 类。

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class D {

    @SerializedName("ComplaintNo")
    @Expose
    private String complaintNo;
    @SerializedName("Status")
    @Expose
    private String status;
    @SerializedName("UpdateDate")
    @Expose
    private String updateDate;
    @SerializedName("UpdateTime")
    @Expose
    private String updateTime;
    @SerializedName("ComplaintReason")
    @Expose
    private String complaintReason;
    @SerializedName("ClosureType")
    @Expose
    private String closureType;
    @SerializedName("Ibase")
    @Expose
    private String ibase;
    @SerializedName("Component")
    @Expose
    private String component;
    @SerializedName("ProductId")
    @Expose
    private String productId;
    @SerializedName("ProductDescription")
    @Expose
    private String productDescription;
    @SerializedName("Identification")
    @Expose
    private String identification;
    @SerializedName("Cat1")
    @Expose
    private String cat1;
    @SerializedName("Cat2")
    @Expose
    private String cat2;
    @SerializedName("Cat3")
    @Expose
    private String cat3;
    @SerializedName("StatusReason")
    @Expose
    private String statusReason;
    @SerializedName("VisitDate")
    @Expose
    private String visitDate;
    @SerializedName("VisitTime")
    @Expose
    private String visitTime;
    @SerializedName("NoOfVisit")
    @Expose
    private String noOfVisit;
    @SerializedName("SerialNo")
    @Expose
    private String serialNo;
    @SerializedName("OtherSpecify")
    @Expose
    private String otherSpecify;
    @SerializedName("Complaint_product")
    @Expose
    private List<ComplaintProduct> complaintProduct = null;
    @SerializedName("Complaint_retuarn")
    @Expose
    private List<ComplaintRetuarn> complaintRetuarn = null;

    public String getComplaintNo() {
        return complaintNo;
    }

    public void setComplaintNo(String complaintNo) {
        this.complaintNo = complaintNo;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUpdateDate() {
        return updateDate;
    }

    public void setUpdateDate(String updateDate) {
        this.updateDate = updateDate;
    }

    public String getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(String updateTime) {
        this.updateTime = updateTime;
    }

    public String getComplaintReason() {
        return complaintReason;
    }

    public void setComplaintReason(String complaintReason) {
        this.complaintReason = complaintReason;
    }

    public String getClosureType() {
        return closureType;
    }

    public void setClosureType(String closureType) {
        this.closureType = closureType;
    }

    public String getIbase() {
        return ibase;
    }

    public void setIbase(String ibase) {
        this.ibase = ibase;
    }

    public String getComponent() {
        return component;
    }

    public void setComponent(String component) {
        this.component = component;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public void setProductDescription(String productDescription) {
        this.productDescription = productDescription;
    }

    public String getIdentification() {
        return identification;
    }

    public void setIdentification(String identification) {
        this.identification = identification;
    }

    public String getCat1() {
        return cat1;
    }

    public void setCat1(String cat1) {
        this.cat1 = cat1;
    }

    public String getCat2() {
        return cat2;
    }

    public void setCat2(String cat2) {
        this.cat2 = cat2;
    }

    public String getCat3() {
        return cat3;
    }

    public void setCat3(String cat3) {
        this.cat3 = cat3;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public String getVisitDate() {
        return visitDate;
    }

    public void setVisitDate(String visitDate) {
        this.visitDate = visitDate;
    }

    public String getVisitTime() {
        return visitTime;
    }

    public void setVisitTime(String visitTime) {
        this.visitTime = visitTime;
    }

    public String getNoOfVisit() {
        return noOfVisit;
    }

    public void setNoOfVisit(String noOfVisit) {
        this.noOfVisit = noOfVisit;
    }

    public String getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    public String getOtherSpecify() {
        return otherSpecify;
    }

    public void setOtherSpecify(String otherSpecify) {
        this.otherSpecify = otherSpecify;
    }

    public List<ComplaintProduct> getComplaintProduct() {
        return complaintProduct;
    }

    public void setComplaintProduct(List<ComplaintProduct> complaintProduct) {
        this.complaintProduct = complaintProduct;
    }

    public List<ComplaintRetuarn> getComplaintRetuarn() {
        return complaintRetuarn;
    }

    public void setComplaintRetuarn(List<ComplaintRetuarn> complaintRetuarn) {
        this.complaintRetuarn = complaintRetuarn;
    }

}

然后 投诉产品

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class ComplaintProduct {

    @SerializedName("SequenceNo")
    @Expose
    private String sequenceNo;
    @SerializedName("SparepartId")
    @Expose
    private String sparepartId;
    @SerializedName("Quantity")
    @Expose
    private String quantity;
    @SerializedName("Group")
    @Expose
    private String group;
    @SerializedName("Model")
    @Expose
    private String model;

    public String getSequenceNo() {
        return sequenceNo;
    }

    public void setSequenceNo(String sequenceNo) {
        this.sequenceNo = sequenceNo;
    }

    public String getSparepartId() {
        return sparepartId;
    }

    public void setSparepartId(String sparepartId) {
        this.sparepartId = sparepartId;
    }

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

}

最后, ComplaintRetuarn (请在此处更正拼写)。

package com.example;

public class ComplaintRetuarn {

}

您可以使用Gson在JSON和POJO之间来回转换。首先,请确保您已将Gson包含在您的项目中。

dependencies {
  implementation 'com.google.code.gson:gson:2.8.5'
}

要从JSON转换为POJO,请使用

D d = new Gson().fromJson(json, D.class);

然后返回JSON;

String json = gson.toJson(d);

答案 1 :(得分:0)

您可以像这样为该json创建相应的bean,

public class Complaint_product{
    private String Model;
    private String Quantity;
    private String SparepartId;
    private String SequenceNo;
    private String Group;
//getter setter
}

public class D{
    private String UpdateTime;
    private String VisitTime;
    private String ComplaintNo;
    private Complaint_product[] Complaint_product;
    private String OtherSpecify;
    private String VisitDate;
    private String Identification;
    private String[] Complaint_retuarn;
    private String ProductId;
    private String StatusReason;
    private String Status;
    private String Cat2;
    private String Cat3;
    private String ComplaintReason;
    private String Cat1;
    private String NoOfVisit;
    private String ClosureType;
    private String SerialNo;
    private String ProductDescription;
    private String UpdateDate;
    private String Component;
    private String Ibase;
//getter setter
}

public class MyPojo
{
    private D d;
//getter setter
}