如何使用GSON为我的DTO制作Json对象

时间:2015-12-17 07:09:54

标签: android json gson

我是android新手,我想使用GSON lib生成JSON

主要的Dto文件:

| |

我也为Customer和LeadService dto创建。 在我的android中我尝试了类似的代码:

public class LeadDto {
    public String CompanyId;
    public String CompanyKey;
    public String UserName;
    public String CreatedDateTime ;
    public String CreatedBy;
    public CustomerDto Customer;
    public List<LeadServicesDto> LeadServices;
    public String getCompanyId() {
        return CompanyId;
    }

    public void setCompanyId(String companyId) {
        CompanyId = companyId;
    }

    public String getCompanyKey() {
        return CompanyKey;
    }

    public void setCompanyKey(String companyKey) {
        CompanyKey = companyKey;
    }

    public String getCreatedDateTime() {
        return CreatedDateTime;
    }

    public void setCreatedDateTime(String createdDateTime) {
        CreatedDateTime = createdDateTime;
    }

    public String getCreatedBy() {
        return CreatedBy;
    }

    public void setCreatedBy(String createdBy) {
        CreatedBy = createdBy;
    }


    public String getUserName() {
        return UserName;
    }

    public void setUserName(String userName) {
        UserName = userName;
    }

    public List<LeadServicesDto> getLeadServices() {
        return LeadServices;
    }

    public void setLeadServices(List<LeadServicesDto> leadServices) {
        LeadServices = leadServices;
    }

    public CustomerDto getCustomer() {
        return Customer;
    }

    public void setCustomer(CustomerDto customer) {
        Customer = customer;
    }
}

结果仅显示:

LeadDto lead = new LeadDto();
lead.setCompanyId(mList.get(i).getAuto_id());
lead.setCompanyKey(mList.get(i).getAuto_id());
lead.setUserName(mList.get(i).getUserName());
lead.setCreatedBy(mList.get(i).getAuto_id());
lead.setCreatedDateTime(mList.get(i).getSendDate());
CustomerDto customerObj = new CustomerDto();
customerObj.setAccountNumber(mList.get(i).getAccNo());
customerObj.setFirstName(mList.get(i).getFname());
customerObj.setLastName(mList.get(i).getLname());
customerObj.setPrimaryPhone(mList.get(i).getPhno());
customerObj.setPrimaryEmail(mList.get(i).getEmail_id());
customerObj.setAddress1("nil");
customerObj.setAddress2("nil");
customerObj.setCitySysName("nil");
customerObj.setStateSysName("nil");
customerObj.setCountrySysName("nil");
customerObj.setZip("nil");

for (int j = 0; j < TotalSelectedServicesList.size(); j++) {
    LeadServicesDto leadServicesObj = new LeadServicesDto();
    leadServicesObj.setServiceSysName(TotalSelectedServicesList.get(i).getServiceName());
    leadServicesObj.setUrgencySysName(TotalSelectedServicesList.get(i).getUrgency());
    leadServicesObj.setComment(TotalSelectedServicesList.get(i).getComment());
    if (TotalSelectedServicesList.get(j).getImageName() != null || !TotalSelectedServicesList.get(j).getImageName().equalsIgnoreCase("")) {
        String[] imageArrayForUploading = TotalSelectedServicesList.get(j).getImageName().split(",");
        for (int k = 0; k < imageArrayForUploading.length; k++) {
            if (k != imageArrayForUploading.length) {
                LeadMediaDto leadMediaObj = new LeadMediaDto();
                leadMediaObj.setMediaTypeSysName("Images");
                leadMediaObj.setName(imageArrayForUploading[k].toString());
            }
        }
    }
}
Gson gson = new Gson();
String json = gson.toJson(lead);

如何在json上面显示或放置数据以保留在对象下面。

{
    "CompanyId":"1450335815920",
    "CompanyKey":"1450335815920",
    "CreatedBy":"1450335815920",
    "CreatedDateTime":"12/17/2015 2:3 AM",
    "UserName":"gaurav.jagtap"
}

请帮帮我。

3 个答案:

答案 0 :(得分:0)

如果你正在使用Gson lib而不需要在dto文件中设置值。

U只能将你的班级名称dto放在Gson中,而Gson会自动设置所有值。

实施例

LeadDto leaddto = new Gson().fromJson(webServiceResponse,  LeadDto.class);

现在,只要需要,就可以从leaddto对象中获取价值。

答案 1 :(得分:0)

您未在List<LeadServicesDto>中设置新创建的CustomerDtoLeadDto objs。这就是为什么他们没有进来导致JSON字符串。

只需使用他们的setter方法设置它们。然后它会工作。

答案 2 :(得分:0)

我会举例说明。

private List<RelationItem> Relations;

在RelationItem类

public class RelationItem {

    private String Id;

    private String Value;
    private String Type;




    public String GetId() {
        return Id;
    }
    public void SetId(String id){Id = id;}



    public String GetValue() {
        return Value;
    }
    public void SetValue (String value){Value = value;}

    public String GetType(){
        return Type;
    }
    public  void SetType(String type){ Type = type;}

}

获取数据

WebApi webApi = new WebApi();
                String response = webApi.get("http://192.168.0.4");

                JSONObject jsonObj ;
                jsonObj = new JSONObject(response);


                Relations = jsonObj.getJSONArray(TAG_RELATION);


                for (int i = 0; i < Relations.length(); i++) {
                    JSONObject c = Relations.getJSONObject(i);


                    String Values = c.getString(TAG_VALUE);
                    String Types = c.getString(TAG_TYPE);


                    HashMap<String, String> contact = new HashMap<String, String>();


                    contact.put(TAG_VALUE, Values);
                    contact.put(TAG_TYPE,Types);

                    // adding contact to contact list
                    RelationList.add(contact);

对于使用以下代码的Put数据:

 RelationItem reitem = new RelationItem();
                reitem.SetId(null);

                reitem.SetType(rspinner.getSelectedItem().toString());
                reitem.SetValue(post_Edittext44.getText().toString());

                reitem.SetUserId(post_Edittext33.getText().toString());


                Gson gson = new Gson();
                String Relation = gson.toJson(reitem);

                WebApi webApi = new WebApi();
                String response = webApi.post("http://192.168.0.4", Relation);
                String msg = response;