用Spring和杰克逊将Json阵列解析为Pojo? [英] Parse Json Array to Pojo with Spring and Jackson?
问题描述
我有一个带有数组对象的Json字符串:
i have this Json String with an Array of Objects:
{DateFormat :[{column: 0, pattern: "yyyyMMdd"},
{column: 2, pattern: "yyyyMMdd"} ]}
我使用Jackson Parser使用Spring MVC ..
and i user Spring MVC with the Jackson Parser..
Java Pojo看起来如何能够将这个Json String自动解析为Object?
How must the Java Pojo looks like that i can parse this Json String automaticly to an Object ?
在Spring我通常会这样做:
in Spring i do it normally like this:
public @ResponseBody String login(@RequestBody DateFormat dataFormaPojo){
}
这适用于简单的Json字符串但是我怎样才能将我的对象数组解析为Java对象或带有对象的ArrayList?我希望Jackson解析器在收到json文件时自动处理它。
this works for an easy Json String but how can i parse my Array of Objects to an Java Object or to an ArrayList with Objects in it? I want that the Jackson parser handles this automaticly if it recieves the json file.
编辑:我已经扩展了json文件,创建了这个java类,但它没有工作:
i have extended the json file a little an created this java classes but it didn`t work:
import java.io.Serializable;
public class DataFormat implements Serializable
{
private static
final long serialVersionUID = 1L;
private Integer column;
private String type;
private String pattern;
public Integer getColumn()
{
return column;
}
public void setColumn(Integer column)
{
this.column = column;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
public String getPattern()
{
return pattern;
}
public void setPattern(String pattern)
{
this.pattern = pattern;
}
}
和DataFormList类
And the DataFormList class
import java.io.Serializable;
import java.util.ArrayList;
import
java.util.List;
public class DataFormatList implements Serializable
{
private static final long serialVersionUID = 2514719982327593095L;
private List<DataFormat> DataFormat = new ArrayList<DataFormat> ();
public List<DataFormat> getDateFormats()
{
return DataFormat;
}
public void setDateFormats(List<DataFormat> formats)
{
this.DataFormat= formats;
}
}
和Spring:
@RequestMapping(value="/save",method=
{
RequestMethod.GET,RequestMethod.POST
}
)
public @ResponseBody String createDataSource( @RequestBody DataFormatList dataFormaPojo)
{
for(DataFormat df: dataFormaPojo.getDateFormats())
{
System.out.println(df.getType());
}
return "";
}
我得到的Json字符串如下所示:
The Json String that i gett looks like this:
{
"DataFormat": [
{
"column": 0,
"type": "number"
},
{
"column": 1,
"type": "number"
},
{
"column": 2,
"type": "number"
},
{
"column": 3,
"type": "number"
}
]
}
我可以设置自定义名称如果我想要对象 DataFormat
有另一个名字?对于带杰克逊的对象?
Can I set a custom name for the Objects with Jackson if i want that that the Object DataFormat
has an other name ?
为什么我的class不解析json字符串?我没有收到任何错误消息。
and why did my class not parse the json string ? i got no error message.
EDIT3:如果我查看goolge chrome dev工具我得到400 Bad Request Message但是在Spring我没有收到错误来自Jackson的消息他无法将我的数据解析为Pojo ..
if i look in the goolge chrome dev tool i get a 400 Bad Request Message but in Spring i get no error Message from Jackson that he can not parse my Data to a Pojo..
推荐答案
用户对象映射器获取具有相应json字符串的java类的对象
user object mapper to get object of java class with corresponding json string
这里的示例可能对您有帮助
在此输入链接说明
这篇关于用Spring和杰克逊将Json阵列解析为Pojo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!