【问题标题】:Variable type in JSON using Jersey+MOXy使用 Jersey+MOXy 的 JSON 中的变量类型
【发布时间】:2015-02-08 17:36:24
【问题描述】:

我正在使用 Jersey 和 MOXy 编写 REST 服务。 我需要创建一个 JSON 对象数组,如下所示:

{
    indexes:[
    {
       "name": "ABC",
       "value": "abcdef"
       "displayValue": "abcdef",
       "type": "string"
     },
     {
        "name": "XYZ",
        "value": 12345
        "displayValue": "12345",
        "type": "number"
     },
     ...
     ]
}

字符串类型应该被引用,而“数字”类型应该不被引用。

我尝试在服务请求中返回以下 POJO:

public class Index {
    String name;
    Object value;
    String displayValue;
    String type;
}

我定义了 setValue 以根据“类型”将整数或字符串分配给值。 同样,我将 getValue 定义为返回 Integer 或 String。

这是我的输出,不出所料,它创建了另一个级别的花括号:

{
     indexes:[
     {
          "name": "ABC",
      "value": {
          "type": "xsd:string",
          "value": "abcdef"
      },
      "displayValue": "abcdef",
      "indexType": "string"
     },
     {
          "name": "XYZ",

          "value": {
            "type": "xsd:int",
            "value": 812501
        },
     "displayValue": "812501",
     "indexType": "number"
     },
     ...

有什么方法可以实现我需要的,“value”字段的变量类型值吗?

【问题讨论】:

    标签: java json jersey moxy


    【解决方案1】:

    我不确定您的问题到底出在哪里。您使用的是什么 JSON 库?另外,您能否提供有关您的 Index 课程的更多详细信息?我不确定为什么你的 setValue(Object value) 需要做任何特别的事情。如果我使用带有以下代码的 Jackson json 库:

    public static void main(String[] args) throws JsonProcessingException {
        Index[] indices = new Index[2];
        indices[0] = new Index();
        indices[0].name = "ABC";
        indices[0].value = "abcdef";
        indices[0].displayValue = indices[0].value + "";
        indices[0].type = "string";
        indices[1] = new Index();
        indices[1].name = "XYZ";
        indices[1].value = 123456;
        indices[1].displayValue = indices[1].value + "";
        indices[1].type = "number";
        System.out.println(new ObjectMapper().writeValueAsString(indices));
    }
    
    public static class Index {
    
        String name;
        Object value;
        String displayValue;
        String type;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Object getValue() {
            return value;
        }
    
        public void setValue(Object value) {
            this.value = value;
        }
    
        public String getDisplayValue() {
            return displayValue;
        }
    
        public void setDisplayValue(String displayValue) {
            this.displayValue = displayValue;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    }
    

    我得到以下输出:

    [{"name":"ABC","value":"abcdef","displayValue":"abcdef","type":"string"},
     {"name":"XYZ","value":123456,"displayValue":"123456","type":"number"}]
    

    我相信这就是您正在寻找的。​​p>

    【讨论】:

    • 感谢您的回复。是的,你的例子正是我所需要的。但是,我不使用 Jackson(这不是我的决定),而是使用为 Object 生成“类型”的 MOXy。
    • 另外,我没有直接将 POJO 转换为 JSON;我将 POJO 作为服务响应返回,所以 MOXy 之后会发挥它的魔力。
    猜你喜欢
    • 1970-01-01
    • 2018-12-23
    • 2016-03-30
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2014-12-18
    相关资源
    最近更新 更多