gpt4 book ai didi

java - 如何在嵌套的 Json 响应后对 Java 类建模

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:22:15 29 4
gpt4 key购买 nike

如何在嵌套的 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;
}
}
}

然后我想用google的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 "";

}

有人可以帮帮我吗?

最佳答案

从对象的角度思考。该 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
}
}

关于java - 如何在嵌套的 Json 响应后对 Java 类建模,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27475722/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com