杰克逊:Json 有 json 数组到 java class 有另一个 Java class 的列表
Jackson: Json having json array to java class having list of another Java class
我正在使用杰克逊 2。6.x
我无法将 json 转换为 java class
public class ParentTest {
@JsonProperty("ParentTest")
public List<Test> getTestList() {
return testList;
}
public void setTestList(List<Test> testList) {
this.testList = testList;
}
@JsonProperty("ParentTest")
List<Test> testList;
public ParentTest(List<Test> testList)
{
this.testList = testList;
}
public ParentTest()
{
super();
testList = new ArrayList<Test>();
}
public String toString()
{
String r = "";
if(testList!=null)
for(Test t : testList)
{
r += "Test:"+t.field1+t.field2;
}
else
r = "null";
return r;
}}
用于列表的 class 是
@JsonRootName("Test")public class Test {
@JsonProperty("field1")
String field1;
@JsonProperty("field2")
String field2;
public Test(String field1,String field2)
{
this.field1 = field1;
this.field2 = field2;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}}
我的Json是
{"ParentTest": [{
"Test":{
"field1":"t1",
"field2":"t2"
}
},
{
"Test":{
"field1":"t3",
"field2":"t4"
}
}]}
为了阅读它,我使用了
public final class HandlerJacksonUtils {
private static ObjectMapper m = new ObjectMapper();
private static JsonFactory jf = new JsonFactory();
public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass) throws IOException {
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return m.readValue(jsonAsString, pojoClass);
}}
然后
ParentTest pt = (ParentTest) HandlerJacksonUtils.fromJson(json, ParentTest.class);
ParentTest 对象 pt 在 System.out.print(pt.toString);
上不打印任何内容
@JsonRootName
不能单独工作,如 javadocs 中所述。
您应该将它与 SerializationFeature#WRAP_ROOT_VALUE
and/or DeserializationFeature#UNWRAP_ROOT_VALUE
结合使用。
但它仅适用于您想要 serialize/deserialize 的根对象。因此,它不适用于您的情况。
相反,您可以为 Test
制作包装器 class
public class TestWrapper {
@JsonProperty("Test")
private Test test;
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
}
并在 ParentTest
中使用它代替 Test
我正在使用杰克逊 2。6.x 我无法将 json 转换为 java class
public class ParentTest {
@JsonProperty("ParentTest")
public List<Test> getTestList() {
return testList;
}
public void setTestList(List<Test> testList) {
this.testList = testList;
}
@JsonProperty("ParentTest")
List<Test> testList;
public ParentTest(List<Test> testList)
{
this.testList = testList;
}
public ParentTest()
{
super();
testList = new ArrayList<Test>();
}
public String toString()
{
String r = "";
if(testList!=null)
for(Test t : testList)
{
r += "Test:"+t.field1+t.field2;
}
else
r = "null";
return r;
}}
用于列表的 class 是
@JsonRootName("Test")public class Test {
@JsonProperty("field1")
String field1;
@JsonProperty("field2")
String field2;
public Test(String field1,String field2)
{
this.field1 = field1;
this.field2 = field2;
}
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
public String getField2() {
return field2;
}
public void setField2(String field2) {
this.field2 = field2;
}}
我的Json是
{"ParentTest": [{
"Test":{
"field1":"t1",
"field2":"t2"
}
},
{
"Test":{
"field1":"t3",
"field2":"t4"
}
}]}
为了阅读它,我使用了
public final class HandlerJacksonUtils {
private static ObjectMapper m = new ObjectMapper();
private static JsonFactory jf = new JsonFactory();
public static <T> Object fromJson(String jsonAsString, Class<T> pojoClass) throws IOException {
m.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return m.readValue(jsonAsString, pojoClass);
}}
然后
ParentTest pt = (ParentTest) HandlerJacksonUtils.fromJson(json, ParentTest.class);
ParentTest 对象 pt 在 System.out.print(pt.toString);
上不打印任何内容@JsonRootName
不能单独工作,如 javadocs 中所述。
您应该将它与 SerializationFeature#WRAP_ROOT_VALUE
and/or DeserializationFeature#UNWRAP_ROOT_VALUE
结合使用。
但它仅适用于您想要 serialize/deserialize 的根对象。因此,它不适用于您的情况。
相反,您可以为 Test
public class TestWrapper {
@JsonProperty("Test")
private Test test;
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
}
并在 ParentTest
Test