我是Json Parsing问题的新手我有下面的Json对象,我需要从下面的Json中获取User对象
{
"aud": "RoomyClinetApps",
"sub": "AAAA",
"User": {
"firtsName": "Godavarthi",
"LastName": "chaitanya"
},
"iss": "Roomy",
"iat": 1499279510
}
以下是我试图拉动User对象的代码,其中上面的json在jsonInString变量中,当我尝试打印时我得到null
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
User user= mapper.readValue(jsonInString.toString(), User.class);
System.out.println(user.getFirtsName()); --> Null
public class User {
@JsonProperty
private String firtsName;
@JsonProperty
private String LastName;
}
有人请给我一个更有意义的工作代码。
由于 切塔尼亚
答案 0 :(得分:0)
测试了以下代码。它对我有用。试一试。
public class Test {
public static void main(String[] args) throws IOException {
String jsonInString = "{\n"
+ " \"aud\": \"RoomyClinetApps\",\n"
+ " \"sub\": \"AAAA\",\n"
+ " \"user\": {\n"
+ " \"firtsName\": \"Godavarthi\",\n"
+ " \"LastName\": \"chaitanya\"\n"
+ " },\n"
+ " \"iss\": \"Roomy\",\n"
+ " \"iat\": 1499279510\n"
+ "}";
ObjectMapper mapper = new ObjectMapper().configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonObject jobj = mapper.readValue(jsonInString.toString(), JsonObject.class);
System.out.println(jobj.getUser().getFirtsName());
}
}
class User {
private String firtsName;
private String LastName;
public String getFirtsName() {
return firtsName;
}
public void setFirtsName(String firtsName) {
this.firtsName = firtsName;
}
public String getLastName() {
return LastName;
}
public void setLastName(String LastName) {
this.LastName = LastName;
}
}
class JsonObject {
private String aud;
private String sub;
private User user;
private String iss;
private long iat;
public String getAud() {
return aud;
}
public void setAud(String aud) {
this.aud = aud;
}
public String getSub() {
return sub;
}
public void setSub(String sub) {
this.sub = sub;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getIss() {
return iss;
}
public void setIss(String iss) {
this.iss = iss;
}
public long getIat() {
return iat;
}
public void setIat(long iat) {
this.iat = iat;
}
}
希望这有用。
答案 1 :(得分:-1)
public class User {
@JsonProperty
private String firtsName;
@JsonProperty
private String LastName;
}
public class JsonObject{
public string aud;
public String sub;
public User user;
public String iss;
public long iat;
}
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JsonObject user= mapper.readValue(jsonInString.toString(), JsonObject.class);