问题
i have this Json String with an Array of Objects:
{DateFormat :[{column: 0, pattern: "yyyyMMdd"},
{column: 2, pattern: "yyyyMMdd"} ]}
and i user Spring MVC with the Jackson Parser..
How must the Java Pojo looks like that i can parse this Json String automaticly to an Object ?
in Spring i do it normally like this:
public @ResponseBody String login(@RequestBody DateFormat dataFormaPojo){
}
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.
EDIT: 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;
}
}
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;
}
}
and in 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 "";
}
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"
}
]
}
Can I set a custom name for the Objects with Jackson if i want that that the Object DataFormat
has an other name ?
and why did my class not parse the json string ? i got no error message.
EDIT3: 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..
回答1:
You will need an object like:
public class DateFormat implements Serializable {
private static final long serialVersionUID = 1L;
private int column;
private String pattern;
public String getPattern() {
return name;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public int getColumn() {
return name;
}
public void setColumn(String column) {
this.column = column;
}
}
And then your main object like:
public class DateFormatList implements Serializable {
private static final long serialVersionUID = 2514719982327593095L;
private List<DateFormat> dateFormats = new ArrayList<DateFormat>();
public List<DateFormat> getDateFormats() {
return name;
}
public void setDateFormats(List<DateFormat> formats) {
this.dateFormats= formats;
}
}
The method in your controller will look like:
public @ResponseBody String login(@RequestBody DateFormatList dataFormaPojo){
....
}
回答2:
Just use notifydatasetchanged()
after the deletion is completed successfully
or just make an instance from your adapter and then access notifydatasetchanged()
I hope this will solve your problem
回答3:
user object mapper to get object of java class with corresponding json string
java docs for object mapper
here is example might help you enter link description here
来源:https://stackoverflow.com/questions/22993174/parse-json-array-to-pojo-with-spring-and-jackson