我想将JSON String解析为POJO类,但我收到错误。
和 我们可以从java
中的方法返回两个值吗?字符串数据
String PostcreatedMessage = "..."; // see JSON value below
{
"home_page":"/desk",
"message":"Logged In",
"full_name":"Shoaib Shaikh"
}
Main.class
LoginR loginR=new LoginR();
ObjectMapper mapper=new ObjectMapper();
loginR=mapper.readValue(PostcreatedMessage,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
parsejacker.class
public class parsejacker
{
LoginR loginR=null;
String jsonurl;
public parsejacker(String jsonurl) {
super();
this.jsonurl = jsonurl;
}
public void ParseLogin() throws JsonParseException,JsonMappingException,IOException
{
System.out.println(jsonurl+"this parselogin");
ObjectMapper mapper=new ObjectMapper();
loginR=mapper.readValue(jsonurl,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
}
}
LoginR.class
public class LoginR
{
private String homePage;
private String message;
private String fullName;
public String getHomePage(){
return homePage;
}
public void setHomePage(String input){
this.homePage = input;
}
public String getMessage(){
return message;
}
public void setMessage(String input){
this.message = input;
}
public String getFullName(){
return fullName;
}
public void setFullName(String input){
this.fullName = input;
}
}
我收到这些错误。
(我认为错误在PostcreatedMessage
字符串中。
如何克服"home_page":"/desk"
? - >这个反斜杠错误,即分隔或忽略此字符)
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException:
Unrecognized field "home_page" (Class org.greenshoaib.greenshaikh.login.model.LoginR), not marked as ignorable
at [Source: java.io.StringReader@4738a206; line: 1, column: 15]
(through reference chain: org.greenshoaib.greenshaikh.login.model.LoginR["home_page"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2732)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1863)
at org.greenshoaib.greenshaikh.rest.client.RestAPIClient.main(RestAPIClient.java:72 )
答案 0 :(得分:0)
使用以下json String尝试您的方法:
PostcreatedMessage {
"homePage":"/desk",
"message":"Logged In",
"fullName":"Shoaib Shaikh"
}
答案 1 :(得分:0)
问题是你的JSON包含一个字段" home_page"但是在你的pojo中,它被称为homePage
(对于" full_name"同样)。杰克逊并不知道这两者是平等的,所以你需要帮助它,例如将@JsonProperty("home_page")
添加到homePage
,即这样(并记住为fullName
执行相同操作):
@JsonProperty("home_page")
private String homePage;
答案 2 :(得分:0)
您的问题是ObjectMapper
无法按名称识别LoginR
类的属性,例如homePage != home_page
。
您可以注释这些属性以匹配其预期的JSON密钥。
这是一个最小的例子:
public class LoginR {
@JsonProperty(value="home_page")
private String homePage;
@JsonProperty(value="full_name")
private String fullName;
// etc. the rest of your POJO
}
......其他地方......
String json = "{\"home_page\":\"/desk\",\"message\":\"Logged In\",\"full_name\":\"Shoaib Shaikh\"}";
ObjectMapper mapper=new ObjectMapper();
LoginR loginR = mapper.readValue(json,LoginR.class);
System.out.println(loginR.getHomePage());
System.out.println(loginR.getMessage());
System.out.println(loginR.getFullName());
<强>输出强>
/desk
Logged In
Shoaib Shaikh