如何在嵌套的json响应之后建模java对象。我试图使用谷歌的GSON库来解析这个响应,所以我想从json字符串转到java
示例
Json回复
{
"iamTicket": {
"ticket": "W1-104-Q36w4qw0hdklirrgtuioqbp",
"userId": "13748569064",
"userIdPseudonym": null,
"agentId": "839298462",
"realmId": null,
"authenticationLevel": "20",
"identityAssuranceLevel": null,
"namespaceId": "50000003",
"role": [],
"access": "",
"scoped": null,
"authTime": null
},
"userExtInfo": {
"user": {
"userId": null,
"username": null,
"userType": null,
"namespaceId": null,
"password": null,
"securityLevel": null,
"challengeQuestionAnswer": [],
"umRole": null,
"credentialStatus": null,
"fullName": [],
"displayName": [],
"characteristics": null,
"address": [],
"phone": [
{
"parentId": null,
"debugInfo": null,
"id": null,
"type": "MOBILE",
"primary": null,
"phoneNumber": "7603042083",
"status": null
}
],
"email": {
"id": null,
"type": null,
"primary": null,
"address": "mhaddad@apple.com",
"status": null
},
"alternateEmail": [],
"i18NAttribute": null,
"creditCard": [],
"preferredCCId": null,
"isRecoveryInfoSet": null,
"taxIdentifier": []
},
"authOfferingUsage": null,
"realmIds": [],
"grant": [],
"taxStatus": []
},
"needContactInfoUpdate": false,
"altAuthInfoSet": true,
"action": "CHALLENGE",
"riskLevel": "MED"
}
以下是我为响应创建的Java对象:
IUSAuth.java
package com.apple.confedential.internal.helper;
public class IUSAuth {
public IUSAuth() {}
IAMTicket iamTicket;
UserExtInfo userExtInfo;
Boolean needContactInfoUpdate;
Boolean altAuthInfoSet;
String action;
String riskLevel;
public static class IAMTicket {
String ticket;
String userId;
Integer userIdPseudonym;
String agentId;
String realmId;
String authenticationLevel;
Integer identityAssuranceLevel;
String namespaceId;
String[] role;
String access;
Integer scoped;
Integer authTime;
public String getTicket() {
return ticket;
}
public void setTicket(String ticket) {
this.ticket = ticket;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public Integer getUserIdPseudonym() {
return userIdPseudonym;
}
public void setUserIdPseudonym(Integer userIdPseudonym) {
this.userIdPseudonym = userIdPseudonym;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getRealmId() {
return realmId;
}
public void setRealmId(String realmId) {
this.realmId = realmId;
}
public String getAuthenticationLevel() {
return authenticationLevel;
}
public void setAuthenticationLevel(String authenticationLevel) {
this.authenticationLevel = authenticationLevel;
}
public Integer getIdentityAssuranceLevel() {
return identityAssuranceLevel;
}
public void setIdentityAssuranceLevel(Integer identityAssuranceLevel) {
this.identityAssuranceLevel = identityAssuranceLevel;
}
public String getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(String namespaceId) {
this.namespaceId = namespaceId;
}
public String[] getRole() {
return role;
}
public void setRole(String[] role) {
this.role = role;
}
public String getAccess() {
return access;
}
public void setAccess(String access) {
this.access = access;
}
public Integer getScoped() {
return scoped;
}
public void setScoped(Integer scoped) {
this.scoped = scoped;
}
public Integer getAuthTime() {
return authTime;
}
public void setAuthTime(Integer authTime) {
this.authTime = authTime;
}
}
public static class UserExtInfo {
User user;
String authOfferingUsage;
RealmIds[] realmIds;
Grant[] grant;
TaxStatus[] taxStatus;
public static class User{
String userId;
String username;
String userType;
String namespaceId;
String password;
String securityLevel;
String[] challengeQuestionAnswer;
String umRole;
String credentialStatus;
String[] fullName;
String[] displayName;
String characteristics;
String[] address;
Phone phone;
Email email;
AlternateEmail[] alternateEmail;
String i18NAttribute;
String[] creditCard;
String preferredCCId;
String isRecoveryInfoSet;
String taxIdentifier;
public static class AlternateEmail {
}
public static class Email {
String id;
String type;
String primary;
String address;
String status;
}
public static class Phone {
String parentId;
String debugInfo;
String id;
String type;
String primary;
String phoneNumber;
String status;
public String getParentId() {
return parentId;
}
public void setParentId(String parentId) {
this.parentId = parentId;
}
public String getDebugInfo() {
return debugInfo;
}
public void setDebugInfo(String debugInfo) {
this.debugInfo = debugInfo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPrimary() {
return primary;
}
public void setPrimary(String primary) {
this.primary = primary;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserType() {
return userType;
}
public void setUserType(String userType) {
this.userType = userType;
}
public String getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(String namespaceId) {
this.namespaceId = namespaceId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSecurityLevel() {
return securityLevel;
}
public void setSecurityLevel(String securityLevel) {
this.securityLevel = securityLevel;
}
public String[] getChallengeQuestionAnswer() {
return challengeQuestionAnswer;
}
public void setChallengeQuestionAnswer(String[] challengeQuestionAnswer) {
this.challengeQuestionAnswer = challengeQuestionAnswer;
}
public String getUmRole() {
return umRole;
}
public void setUmRole(String umRole) {
this.umRole = umRole;
}
public String getCredentialStatus() {
return credentialStatus;
}
public void setCredentialStatus(String credentialStatus) {
this.credentialStatus = credentialStatus;
}
public String[] getFullName() {
return fullName;
}
public void setFullName(String[] fullName) {
this.fullName = fullName;
}
public String[] getDisplayName() {
return displayName;
}
public void setDisplayName(String[] displayName) {
this.displayName = displayName;
}
public String getCharacteristics() {
return characteristics;
}
public void setCharacteristics(String characteristics) {
this.characteristics = characteristics;
}
public String[] getAddress() {
return address;
}
public void setAddress(String[] address) {
this.address = address;
}
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
}
public static class RealmIds{
}
public static class Grant{
}
public static class TaxStatus{
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public String getAuthOfferingUsage() {
return authOfferingUsage;
}
public void setAuthOfferingUsage(String authOfferingUsage) {
this.authOfferingUsage = authOfferingUsage;
}
public RealmIds[] getRealmIds() {
return realmIds;
}
public void setRealmIds(RealmIds[] realmIds) {
this.realmIds = realmIds;
}
public Grant[] getGrant() {
return grant;
}
public void setGrant(Grant[] grant) {
this.grant = grant;
}
public TaxStatus[] getTaxStatus() {
return taxStatus;
}
public void setTaxStatus(TaxStatus[] taxStatus) {
this.taxStatus = taxStatus;
}
}
}
然后我想使用谷歌的GSON库解析来获取票证数据:
public String parseIUSResponse(String jsonResponse) {
Gson gson = new Gson();
IUSAuth response = gson.fromJson(jsonResponse, IUSAuth.class);
// How to get ticket from iamticket object??
return "";
}
有人可以帮帮我吗?
答案 0 :(得分:2)
根据对象思考。 JSON字符串中有两个对象。所有其他属性都是内部对象或属性。
package com.apple.internal.helper;
public class IUSAuth {
IAMTicket iamTicket;
UserExtInfo userExtInfo;
public IUSAuth() {}
public class IAMTicket {
// ADD ALL YOUR IAMTicket Properties Here
String ticket;
}
public class UserExtInfo{
// ADD ALL YOUR USEREXTINFO Properties here
}
}