【问题标题】:Empty string when using GSON for Java -> JSon将 GSON 用于 Java -> JSon 时为空字符串
【发布时间】:2016-04-07 20:55:51
【问题描述】:

尝试将 POJO 转换为 Json 表示,我的输出非常令人惊讶:empty

这里是 POJO 类:

public class AccountDTO extends BasicDBObject {

    /**
     * 
     */
    public static final String COLLECTION_NAME = "account-data";
    private static final long serialVersionUID = 1L;
    private String customerFirstName;
    private String customerLastName;
    private long customerId;
    private String IBAN;
    private float balance;
    private String accountCurrency;




    public AccountDTO(Account account, Customer customer) {
        super();
        this.customerFirstName = customer.getFirstname();
        this.customerLastName = customer.getLastname();
        this.customerId = customer.getCustomerId();
        this.IBAN = account.getIBAN();
        this.balance = account.getBalance();
        this.accountCurrency = account.getAccountCurrency();
    }

    public String getCustomerName() {
        return customerFirstName;
    }
    public void setCustomerName(String customerName) {
        this.customerFirstName = customerName;
    }
    public String getCustomerLastName() {
        return customerLastName;
    }
    public void setCustomerLastName(String customerLastName) {
        this.customerLastName = customerLastName;
    }
    public long getCustomerId() {
        return customerId;
    }
    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }
    public String getIBAN() {
        return IBAN;
    }
    public void setIBAN(String iBAN) {
        IBAN = iBAN;
    }
    public float getBalance() {
        return balance;
    }
    public void setBalance(float balance) {
        this.balance = balance;
    }
    public String getCustomerFirstName() {
        return customerFirstName;
    }
    public void setCustomerFirstName(String customerFirstName) {
        this.customerFirstName = customerFirstName;
    }
    public String getAccountCurrency() {
        return accountCurrency;
    }
    public void setAccountCurrency(String accountCurrency) {
        this.accountCurrency = accountCurrency;
    }

    @Override
    public String toString() {
        return "AccountDTO [customerFirstName=" + customerFirstName + ", customerLastName=" + customerLastName
                + ", customerId=" + customerId + ", IBAN=" + IBAN + ", balance=" + balance + ", accountCurrency="
                + accountCurrency + "]";
    }







}

转换器:

 public abstract class AccountDTODigester {
          public static String digestJavaToJson(AccountDTO dto){
              Gson gson = new Gson();
              String json = gson.toJson(dto);
              return json;
          }
    }

杰克逊代码:

    public abstract class AccountDTODigester {
      public static String digestJavaToJson(AccountDTO dto) throws JsonProcessingException{

          ObjectMapper mapper = new ObjectMapper();
          String jsonInString = new String();
          jsonInString = mapper.writeValueAsString(dto);
          return jsonInString;
      }
}

最后是亚军:

public class DAOTest {
    AccountDTO accountDTO;

    @Before
    public void initialize(){
        Account account = new Account("FRkk BBBB BGGG GGCC CCCC CCCC CKK", 0, "euro");
        Customer customer = new Customer("XXXXXX", "YYYYYY", 1, account);
        this.accountDTO = new AccountDTO(account, customer);
    }
    @Test
    public void toJson(){
        Assert.assertNotEquals(AccountDTODigester.digestJavaToJson(accountDTO),new String("{}"));
}

控制台输出:

AccountDTO [customerFirstName=XXXXXX, customerLastName=YYYYYY, customerId=1, IBAN=FRkk BBBB BGGG GGCC CCCC CCCC CKK, balance=0.0, accountCurrency=euro]
{}

当我运行测试时,我的 json 字符串是 { },我的测试被标记为失败。 Gson 似乎很容易使用,我不明白为什么我得到这个空的 Json 而不是一个 String 填充了我的 AccountDTO 对象的 json 表示

【问题讨论】:

    标签: java json string jackson gson


    【解决方案1】:

    好的,我找到了解决方案:

    我的 JSON 是 null 因为 extends BasicDBObject ,序列化似乎被这个卡住了。

    仍在寻找更好的解释,但现在我的 json 没问题。

    【讨论】:

      猜你喜欢
      • 2017-01-24
      • 2013-05-19
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多