简体   繁体   中英

Parse Json Array to Pojo with Spring and Jackson?

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..

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){
    ....
}

只需在删除成功完成后使用notifydatasetchanged()或从您的适配器创建一个实例然后访问notifydatasetchanged()我希望这能解决您的问题

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

As I do not have sufficient reputation to comment, updating the answer by @Garis M Suero. The update is to use the annotation @JsonProperty("DataFormat") for getter/setter method.

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>();

@JsonProperty("DataFormat")
public List<DateFormat> getDateFormats() {
    return name;
}

@JsonProperty("DataFormat")
public void setDateFormats(List<DateFormat> formats) {
    this.dateFormats= formats;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM