我正在尝试将以下jsonString
结构转换为Java
列表/对象数组:
[
[1,21940000,1905386136,null,"KR","akshay","04/06/2017","03/06/2017",2017,9,40,"JPY",7478,"JPY",7478,"WHT (Residen",null,0,"03/06/2017","03/06/2017","20170604",null],
[2,21940000,1903732187,null,"KR",null,"06/06/2017","05/06/2017",2017,9,40,"JPY",608547485,"JPY",608547485,"WHT (Non-Resi",null,0,"05/06/2017","05/06/2017","20170606",null],
[3,21940000,2001898163, ............... ]
.
.
.
.
.
.
.
.
]
以下是Java
代码:
ObjectMapper mapper = new ObjectMapper();
MyData[][] data = mapper.readValue(jsonString, MyData[][].class);
但是,我得到以下错误:
com.fasterxml.jackson.databind.JsonMappingException:
Can not construct instance of com.org.model.MyData:
no String-argument constructor/factory method to deserialize from String value ('KR')
at [Source: java.io.StringReader@1327cf05; line: 1, column: 30] (through reference chain: java.lang.Object[][0]->java.lang.Object[][4])
有人可以帮帮我吗?感谢
编辑:以下是我的POJO MyData.java
代码:
@Entity
@Table(schema = "My_Schema", name = "My_Data_Table")
@SuppressFBWarnings(value = { "EI_EXPOSE_REP", "EI_EXPOSE_REP2" }, justification = "I prefer to suppress these FindBugs warnings")
public class MyData implements Serializable {
/**
*
*/
private static final long serialVersionUID = -6936461726389768288L;
public MyData() {
super();
}
/**
* @param id
*/
public MyData(Long id) {
super();
this.id = id;
}
@Id
private Long id;
@Column(name = "ACCOUNT")
private long account;
@Column(name = "DOC_NUMBER")
private long docNumber;
@Column(name = "TYPE")
private String type;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "DOC_DATE")
private Date docDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "POSTING_DATE")
private Date postingDate;
@Column(name = "YEAR")
private long year;
@Column(name = "PERIOD")
private long period;
@Column(name = "PK")
private long pk;
@Column(name = "TAX_CODE")
private String taxCode;
@Column(name = "CCY")
private String ccy;
@Column(name = "DOC_CCY_AMT")
private long docCcyAmt;
@Column(name = "LOCAL_CCY")
private String localCcy;
@Column(name = "LOCAL_CCY_AMT")
private long localCcyAmt;
@Column(name = "TEXT")
private String text;
@Column(name = "DOC_HEADER_TEXT")
private String docHeaderText;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "CLEARING_DATE")
private Date clearingDate;
@Column(name = "CLEARING_DOC")
private long clearingDoc;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "ENTRY_DATE")
private Date entryDate;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd/MM/yyyy")
@Column(name = "VALUE_DATE")
private Date valueDate;
@Column(name = "ASSIGNMENT")
private String assignment;
@Column(name = "REMARKS")
private String remarks;
// Getters and setters to follow .....
所以,问题是我的输入JSON字符串是一个数组数组,我希望它是一个Java表示形式,无论是ArrayList还是普通数组......
答案 0 :(得分:0)
您可能缺少" KR"在你的" MyData"类。在此处发布MyData类的定义之前,请先查看此代码。它一定会帮到你。
class Student {
private String name;
private int age;
public Student(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString(){
return "Student [ name: "+name+", age: "+ age+ " ]";
}
}
并测试它
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
public class JacksonTester {
public static void main(String args[]){
ObjectMapper mapper = new ObjectMapper();
String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";
//map json to student
try{
Student student = mapper.readValue(jsonString, Student.class);
System.out.println(student);
mapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
jsonString = mapper.writeValueAsString(student);
System.out.println(jsonString);
}
catch (JsonParseException e) { e.printStackTrace();}
catch (JsonMappingException e) { e.printStackTrace(); }
catch (IOException e) { e.printStackTrace(); }
}
}