json 反序列化器到目标对象

发布于 2024-11-02 11:43:02 字数 806 浏览 2 评论 0原文

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

这是来自客户端的 json:

{"affiliateId":1,"name":"First Affiliate","email":"[email protected]","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

谢谢

Can you recommend on a Json Deserializer that can deserialize into existing object (merge 2 objects)?
When the user submit a form I want to save that into the db this way:

this is the json from the client:

{"affiliateId":1,"name":"First Affiliate","email":"[email protected]","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

Meaning that I want the affiliateFromJson to be interpolated into affiliateFromDb.
And than I will call

 affiliateApi.save(affiliateFromDb);

Note that the json contains deep deserialize, user.userName

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-11-09 11:43:02

使用 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(反)序列化器都会遇到同样的问题。你必须使用这样的东西来代替:

{"affiliateId":1,"name":"First Affiliate","email":"[email protected]","user": {"userName":"test","password":"pass-hashed"},"employee.employeeId":1}

Use Gson! In particular, see the Object Examples.

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);   

The only catch here — but you will have this same problem with any other JSON (de)serializer — is the nonstandard "deep" object format you want to work with. You would have to use something like this instead:

{"affiliateId":1,"name":"First Affiliate","email":"[email protected]","user": {"userName":"test","password":"pass-hashed"},"employee.employeeId":1}
孤者何惧 2024-11-09 11:43:02

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

JSONObject jsonResponse = new JSONObject(responseString);

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

JSONObject jsonResponse = new JSONObject(responseString);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文