Android Studio中的Json字符串解析- Java

9vw9lbht  于 2023-11-15  发布在  Java
关注(0)|答案(2)|浏览(135)

我尝试使用GSON对JSON字符串进行格式化,但格式化后所有值都显示为JSON。这是JSON字符串(我通过Rest API从服务器获取此字符串):
{"getInformationResult ":{
"BE_Active":false,"BE_Owner_ID":0,"BT_ID":0,"CLC_销售":0,"CLC_Shared_Balance":false,"Cash":false,"Client_Active":false,"Client_Client":null,"Client_ID":0,"Clients_Balance_By_Brand":null,"Contact_Active":false,"Contact_ID":0,"Credit":false,"Currency_ID":0,"Error_Codes":null,"Errors":[{" Error_Code":" 101"," Error_Description":" Login:Invalid User or Password"," Error_ID":27," Error_Name":" Invalid User Or Password "}]," Father_ID":0," Labels":null," Language_ID":0," Parent_ID":0," Schedule_Time_End":null," Schedule_Time_Start":null," Server_Date_Time":null," Server_TZ_ISO_Notation":null," TT_ID":0," TZ_ISO_Notation":null," Template_ID":0," User_Active":false," User_Email":null," User_Name":null}}
这就是我用来解释的:
Gson gson = new Gson(); getInformationResult getInformationResult = new getInformationResult(); getInformationResult respuesta = gson. fromJson(response,getInformationResult.class);
public class Uncategorized {

@SerializedName("Errors")
public Errors Errors;
@SerializedName("User_Name")
public String User_Name;
@SerializedName("BE_Active")
public String BE_Active;
@SerializedName("BE_Owner_ID")
public String BE_Owner_ID;
@SerializedName("BT_ID")
public String BT_ID;
@SerializedName("CLC_Sale")
public String CLC_Sale;
@SerializedName("CLC_Shared_Balance")
public String CLC_Shared_Balance;
@SerializedName("Cash")
public String Cash;
@SerializedName("Client_Active")
public String Client_Active;
@SerializedName("Client_Alias")
public String Client_Alias;
@SerializedName("Client_ID")
public String Client_ID;
@SerializedName("Clients_Balance_By_Brand")
public String Clients_Balance_By_Brand;
@SerializedName("Contact_Active")
public String Contact_Active;
@SerializedName("Contact_ID")
public String Contact_ID;
@SerializedName("Credit")
public String Credit;
@SerializedName("Currency")
public String Currency;
@SerializedName("Error_Codes")
public String Error_Codes;
@SerializedName("Father_ID")
public String Father_ID;
@SerializedName("Labels")
public String Labels;
@SerializedName("Language_ID")
public String Language_ID;
@SerializedName("Parent_ID")
public String Parent_ID;
@SerializedName("Schedule_Time_End")
public String Schedule_Time_End;
@SerializedName("Schedule_Time_Start")
public String Schedule_Time_Start;
@SerializedName("Server_Date_Time")
public String Server_Date_Time;
@SerializedName("Server_TZ_ISO_Notation")
public String Server_TZ_ISO_Notation;
@SerializedName("TZ_ISO_Notation")
public String TZ_ISO_Notation;
@SerializedName("TT_ID")
public String TT_ID;
@SerializedName("Template_ID")
public String Template_ID;
@SerializedName("User_Active")
public String User_Active;
@SerializedName("User_Email")
public String User_Email;

 public getInformationResult(Errors Errors, String User_Name,String BE_Active,String BE_Owner_ID, String BT_ID,String CLC_Sale,String CLC_Shared_Balance, String Cash,
                            String Client_Active, String Client_Alias,String Client_ID,String Clients_Balance_By_Brand, String Server_TZ_ISO_Notation,String Contact_Active,
                            String Contact_ID, String Credit, String Currency,String Error_Codes,String Father_ID,String Labels,String Language_ID,String Parent_ID,
                            String Schedule_Time_End,String Schedule_Time_Start,String Server_Date_Time, String TZ_ISO_Notation,String TT_ID, String Template_ID, String User_Active, String User_Email ) {
    this.Errors = Errors;
    this.User_Name = User_Name;
    this.BE_Active = BE_Active;
    this.BE_Owner_ID = BE_Owner_ID;
    this.BT_ID=BT_ID;
    this.CLC_Sale = CLC_Sale;
    this.CLC_Shared_Balance=CLC_Shared_Balance;
    this.Cash=Cash;
    this.Client_Active=Client_Active;
    this.Client_Alias = Client_Alias;
    this.Client_ID = Client_ID;
    this.Clients_Balance_By_Brand = Clients_Balance_By_Brand;
    this.Server_TZ_ISO_Notation = Server_TZ_ISO_Notation;
    this.Contact_Active=Contact_Active;
    this.Contact_ID=Contact_ID;
    this.Credit=Credit;
    this.Currency=Currency;
    this.Error_Codes=Error_Codes;
    this.Father_ID=Father_ID;
    this.Labels=Labels;
    this.Language_ID=Language_ID;
    this.Parent_ID=Parent_ID;
    this.Schedule_Time_End=Schedule_Time_End;
    this.Schedule_Time_Start=Schedule_Time_Start;
    this.TZ_ISO_Notation=TZ_ISO_Notation;
    this.TT_ID=TT_ID;
    this.Server_Date_Time=Server_Date_Time;
    this.Template_ID=Template_ID;
    this.User_Active=User_Active;
    this.User_Email=User_Email;
}

@NonNull
@Override
public String toString() {
    return new Gson().toJson(this);
}

字符串
}

5lwkijsr

5lwkijsr1#

您不需要 *SerializedName**注解 *。
这些是针对 keys 不是有效的 Java 变量名的情况,例如 “abc-123”“123 abc”
这里有一个例子。

class Example {
    String key1;
    int key2;
}

个字符

cwtwac6a

cwtwac6a2#

如果有人需要答案,我用这样的代码解决了我的问题:
public class Uncategorized {

public List<Errors> errorsList;
public String User_Name;
public String BE_Active;
public String BE_Owner_ID;
public String BT_ID;
public String CLC_Sale;
public String CLC_Shared_Balance;
public String Cash;
public String Client_Active;
public String Client_Alias;
public String Client_ID;
public String Clients_Balance_By_Brand;
public String Contact_Active;
public String Contact_ID;
public String Credit;
public String Currency;
public String Error_Codes;
public String Father_ID;
public String Labels;
public String Language_ID;
public String Parent_ID;
public String Schedule_Time_End;
public String Schedule_Time_Start;
public String Server_Date_Time;
public String Server_TZ_ISO_Notation;
public String TZ_ISO_Notation;
public String TT_ID;
public String Template_ID;
public String User_Active;
public String User_Email;

public List<Errors> getErrorsList() {
    return errorsList;
}

public void setErrorsList(List<Errors> errorsList) {
    this.errorsList = errorsList;
}

public String getUser_Name() {
    return User_Name;
}

public void setUser_Name(String user_Name) {
    User_Name = user_Name;
}

public String getBE_Active() {
    return BE_Active;
}

public void setBE_Active(String BE_Active) {
    this.BE_Active = BE_Active;
}

public String getBE_Owner_ID() {
    return BE_Owner_ID;
}

public void setBE_Owner_ID(String BE_Owner_ID) {
    this.BE_Owner_ID = BE_Owner_ID;
}

public String getBT_ID() {
    return BT_ID;
}

public void setBT_ID(String BT_ID) {
    this.BT_ID = BT_ID;
}

public String getCLC_Sale() {
    return CLC_Sale;
}

public void setCLC_Sale(String CLC_Sale) {
    this.CLC_Sale = CLC_Sale;
}

public String getCLC_Shared_Balance() {
    return CLC_Shared_Balance;
}

public void setCLC_Shared_Balance(String CLC_Shared_Balance) {
    this.CLC_Shared_Balance = CLC_Shared_Balance;
}

public String getCash() {
    return Cash;
}

public void setCash(String cash) {
    Cash = cash;
}

public String getClient_Active() {
    return Client_Active;
}

public void setClient_Active(String client_Active) {
    Client_Active = client_Active;
}

public String getClient_Alias() {
    return Client_Alias;
}

public void setClient_Alias(String client_Alias) {
    Client_Alias = client_Alias;
}

public String getClient_ID() {
    return Client_ID;
}

public void setClient_ID(String client_ID) {
    Client_ID = client_ID;
}

public String getClients_Balance_By_Brand() {
    return Clients_Balance_By_Brand;
}

public void setClients_Balance_By_Brand(String clients_Balance_By_Brand) {
    Clients_Balance_By_Brand = clients_Balance_By_Brand;
}

public String getContact_Active() {
    return Contact_Active;
}

public void setContact_Active(String contact_Active) {
    Contact_Active = contact_Active;
}

public String getContact_ID() {
    return Contact_ID;
}

public void setContact_ID(String contact_ID) {
    Contact_ID = contact_ID;
}

public String getCredit() {
    return Credit;
}

public void setCredit(String credit) {
    Credit = credit;
}

public String getCurrency() {
    return Currency;
}

public void setCurrency(String currency) {
    Currency = currency;
}

public String getError_Codes() {
    return Error_Codes;
}

public void setError_Codes(String error_Codes) {
    Error_Codes = error_Codes;
}

public String getFather_ID() {
    return Father_ID;
}

public void setFather_ID(String father_ID) {
    Father_ID = father_ID;
}

public String getLabels() {
    return Labels;
}

public void setLabels(String labels) {
    Labels = labels;
}

public String getLanguage_ID() {
    return Language_ID;
}

public void setLanguage_ID(String language_ID) {
    Language_ID = language_ID;
}

public String getParent_ID() {
    return Parent_ID;
}

public void setParent_ID(String parent_ID) {
    Parent_ID = parent_ID;
}

public String getSchedule_Time_End() {
    return Schedule_Time_End;
}

public void setSchedule_Time_End(String schedule_Time_End) {
    Schedule_Time_End = schedule_Time_End;
}

public String getSchedule_Time_Start() {
    return Schedule_Time_Start;
}

public void setSchedule_Time_Start(String schedule_Time_Start) {
    Schedule_Time_Start = schedule_Time_Start;
}

public String getServer_Date_Time() {
    return Server_Date_Time;
}

public void setServer_Date_Time(String server_Date_Time) {
    Server_Date_Time = server_Date_Time;
}

public String getServer_TZ_ISO_Notation() {
    return Server_TZ_ISO_Notation;
}

public void setServer_TZ_ISO_Notation(String server_TZ_ISO_Notation) {
    Server_TZ_ISO_Notation = server_TZ_ISO_Notation;
}

public String getTZ_ISO_Notation() {
    return TZ_ISO_Notation;
}

public void setTZ_ISO_Notation(String TZ_ISO_Notation) {
    this.TZ_ISO_Notation = TZ_ISO_Notation;
}

public String getTT_ID() {
    return TT_ID;
}

public void setTT_ID(String TT_ID) {
    this.TT_ID = TT_ID;
}

public String getTemplate_ID() {
    return Template_ID;
}

public void setTemplate_ID(String template_ID) {
    Template_ID = template_ID;
}

public String getUser_Active() {
    return User_Active;
}

public void setUser_Active(String user_Active) {
    User_Active = user_Active;
}

public String getUser_Email() {
    return User_Email;
}

public void setUser_Email(String user_Email) {
    User_Email = user_Email;
}

@NonNull
@Override
public String toString() {
    return new Gson().toJson(this);
}

字符串
}
public class Uncategorized {

public List<Error_Description>errorDescriptions;

public String Error_ID;
public String Error_Name;
public static String Error_Code;

public static String getError_Code() {
    return Error_Code;
}

public void setError_Code(String error_Code) {
    Error_Code = error_Code;
}

public String getError_ID() {
    return Error_ID;
}

public void setError_ID(String error_ID) {
    Error_ID = error_ID;
}

public String getError_Name() {
    return Error_Name;
}

public void setError_Name(String error_Name) {
    Error_Name = error_Name;
}


@NonNull
@Override
public String toString() {
    return new Gson().toJson(this);
}


}
public class Uncategorized {

public String getLogin() {
    return Login;
}

public void setLogin(String login) {
    Login = login;
}

public String Login;

    @NonNull
    @Override
    public String toString() {
        return new Gson().toJson(this);
    }
}

相关问题