Jackson-将嵌套的json对象存储为JSONObject [英] Jackson - Store nested json object as JSONObject

查看:1313
本文介绍了Jackson-将嵌套的json对象存储为JSONObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在反序列化包含嵌套json对象的json对象.

I am deserializing a json object, which contains a nested json object.

{
  "id:" 1,
  "name:" "test",
  "settings": {
   "color": "#ff0000"
  }
}

这是我的汽车课:

public class Car {
  private long id;
  private String name;
  private JSONObject settings;

  // id getter/setter...
  // name gett/setter...

  public void setSettings(JSONObject settings) {
    this.settings = settings;
  }

  public JSONObject getSettings() {
    return settings;
  }
}

我想将设置保留为JSONObject,并在以后根据需要对其进行解析.我也想避免创建Settings类(因为我是如何使用Sugar ORM缓存模型的).当我尝试用Jackson进行反序列化时,设置始终返回"{}".我该如何与杰克逊做到这一点?

I would like to keep settings as a JSONObject and parse it later as I need it. I am also wanting to avoid creating a Settings class (because of how I'm caching my models with Sugar ORM). When I try to deserialize this with Jackson, settings is always returning "{}". How can I do this with Jackson?

更新:

这是解析Car的代码:

Here is the code that parses Car:

ObjectMapper objectMapper = new ObjectMapper(); 
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Car car = objectMapper.readValue(jsonObject.toString(), Car.class);

汽车可能是别的东西中的嵌套对象,因此不会总是被调用.如果我总是可以调用它,那么我已经可以访问json对象,并且可以在此处手动将Settings对象设置为Car.

Car may be a nested object inside of something else, so this won't always be called. If I could always call this, this I would already have access to the json object and could just set the Settings object to the Car manually here.

推荐答案

将JSONObject更改为JsonObject似乎已解决了该问题.

Changing JSONObject to JsonObject seemed to have fixed the problem.

这篇关于Jackson-将嵌套的json对象存储为JSONObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆