【发布时间】:2018-09-23 20:20:40
【问题描述】:
我正在尝试转换下一个字符串:
"{ \"contacts\": [{\"name\":\"1\",\"phone\":\"+123456\"}]}"
到一些自定义对象:
public class CustomObject{
private List<Contact> contacts;
public CustomObject(){
}
public CustomObject(List<Contact> contacts) {
this.contacts = contacts;
}
public List<Contact> getContactList() {
return contacts;
}
public void setContactList(List<Contact> contacts) {
this.contacts = contacts;
}
}
另外,这个CustomObject里面还有一个对象:
public class Contact {
private String name;
private String phone;
public Contact() {
}
public Contact(String name, String phone) {
this.name = name;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
现在,我正在尝试执行以下操作:
private List<Contact> parseString(String jsonAsString) throws IOException {
ObjectMapper mapper = new ObjectMapper();
CustomObject customObject = mapper.readValue(jsonAsString, CustomObject .class);
return customObject .getContactList();
}
但我收到下一个错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: No content to map due to end-of-input
at [Source: (String)""{ \"contacts\": [{\"name\":\"1\",\"phone\":\"+972545519713\"}]}""; line: 1, column: 1]
【问题讨论】:
-
你能试试这个吗:
"[{\"name\":\"1\",\"phone\":\"+123456\"}]" -
抱歉,没用...
标签: java json string jackson objectmapper