Robospice动态映射

时间:2014-02-18 10:41:31

标签: java json robospice

有人可以告诉我如何在我的“RequestResult.java”中使用RoboSpice进行动态映射吗?

我的JSON看起来像这样:

"result": [
    {
      "attributeX1" "...",
      "type": "x",
      "attributeXi" "..."
    },
    {
      "attributeX1": "...",
      "type": "x",
      "attributeXi": "..."
     }
]

"result": [
    {
      "attributeY1" "...",
      "type": "y",
      "attributeYi" "..."
    },
    {
      "attributeY1" "...",
      "type": "y",
      "attributeYi" "..."
    }
]

在请求中,类型为x或y。因此,如果“type”:“x”,我必须使用模型“x.java”,如果“type”:“y”需要使用模型“y.java”

1 个答案:

答案 0 :(得分:0)

这完全取决于您使用的JSON库。 Jackson 2提供了一种可能对您感兴趣的多态类型:http://programmerbruce.blogspot.com.es/2011/05/deserialize-json-with-jackson-into.html

官方文件在这里:http://wiki.fasterxml.com/JacksonPolymorphicDeserialization