繁体   English   中英

json反序列化器插入目标对象

[英]json Deserializer in to target object

您能推荐一个可以反序列化到现有对象( 合并2个对象 )的Json Deserializer吗?
当用户提交表单时,我想通过以下方式将其保存到数据库中:

这是来自客户端的json:

{"affiliateId":1,"name":"First Affiliate","email":"email@gmail.com","user.userName":"test","user.password":"pass-hashed","employee.employeeId":1}


Affiliate affiliateFromDb = affiliateApi.getFromDbById(1);

SomeDeserialization json = new SomeDeserialization();
affiliateFromDb = json.fromJson(affiliateFromJson  , affiliateFromDb );//affiliateFromDb = target bean

这意味着我希望将affiliateFromJson插入到affiliateFromDb中。
然后我会打电话给

 affiliateApi.save(affiliateFromDb);

请注意,json包含深层反序列化user.userName

谢谢

使用Gson 特别是,请参阅对象示例

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   

唯一要注意的是-您使用的非标准“深层”对象格式是任何其他JSON(de)序列化器都会遇到的相同问题。 您将不得不使用如下所示的内容:

{"affiliateId":1,"name":"First Affiliate","email":"email@gmail.com","user": {"userName":"test","password":"pass-hashed"},"employee.employeeId":1}

http://www.json.org/javadoc/org/json/JSONObject.html

JSONObject jsonResponse = new JSONObject(responseString);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM