【问题标题】:Handle JSONObject inside other JSONObject在其他 JSONObject 中处理 JSONObject
【发布时间】:2011-04-22 12:54:59
【问题描述】:

我想发送json,里面有另一个json对象,像这样

{
     "key1": "value1",
     "key2": "valu2",
     "content": {
         "nestedkey1": "nestedValue1",
         "nestedkey2": "nestedValue2" 
     }

}

里面的对象没有任何java表示,只是json格式的字符串。如何正确转换?

我的方法不正确,我总是收到嵌套 json 的空字符串。我为这个嵌套对象使用了 Map,但又为空地图。

public class Instance {

private String key1;

private int key2;

private String content;

public String getKey1 {
    return key1;
}

public void setKey1(String key1) {
    this.key1 = key1;
}

public BigDecimal getKey2() {
    return key2;
}

public void setKey2(BigDecimal key2) {
    this.key2 = key2;
}

public String getContent() {
    return content;
}

public void setContent(String content) {
    this.content = content;
}

【问题讨论】:

    标签: java json rest jersey


    【解决方案1】:

    JSON 序列化是如何完成的?如果您没有使用Jackson,那么您应该使用。

    Jackson 可以根据需要将Map 转换为 JSON,无需任何额外配置。另一方面,如果您使用的是 Jersey JSON 插件,则必须编写 Map 的子类,并向其中添加 JAXB 注释 - 有点麻烦。

    【讨论】:

      猜你喜欢
      • 2014-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2018-11-14
      相关资源
      最近更新 更多