我的javascript看起来像这样:
{
a: "This is a Test",
b: {
test1: "bla",
test2: "blub
}
}
现在我将此对象作为字符串化Jason Object发送到我的Java后端(Jax-RS),并希望将其解析回Java对象。我正在使用杰克逊。问题是,我不知道如何在其中映射具有不同类型的对象。 (字符串/地图) 有人可以帮忙吗?
答案 0 :(得分:1)
像这样的json:
{
"a": "This is a Test","
"b": {
"test1": "bla",
"test2": "blub"
}
}
您可以尝试以下代码:
public static void main(String[] args)
throws JsonParseException, JsonMappingException,IOException {
String json = "{\"a\": \"This is a Test\",\"b\": {\"test1\": \"bla\",\"test2\": \"blub\"}}";
System.out.println(json);
JObj obj = new ObjectMapper().readValue(json, JObj.class);
System.out.println(obj);
}
static class JObj {
String a;
Map<String, String> b;
public String getA() {return a;}
public void setA(String a) {this.a = a;}
public Map<String, String> getB() {return b;}
public void setB(Map<String, String> b) {this.b = b;}
@Override
public String toString() {return "JObj [a=" + a + ", b=" + b + "]";}
}