gpt4 book ai didi

java - Json 数据分配给 java 类

转载 作者:行者123 更新时间:2023-11-30 03:39:38 25 4
gpt4 key购买 nike

我在将 json 数据分配到 java 类时遇到问题。请帮助任何人,我的 java 类就像,

public class ListofGridRecords<T> {
public int Totalrecords;
public List<T> GridRecords;//using TraderTransaction class.
}

TraderTransaction 类是,

public class TraderTransaction {

public Date AddedTime;
public String TransactId;
public TransactStatus Status;
public String OtherPartyAccountNo;
public Double AmountPaid;
public Double AmountRecieved;
public Double ClosingBalance;
public TransactionTypes TransType;
public String Narration;

public TraderTransaction() {
super();
}
}

我的 json 转换函数看起来像,

JsonObject returndata = JsonObject.parse(responseString);
String operationresult = returndata.get("OperationResult").toString();

if (Result.values()[Integer.parseInt(operationresult)] == Result.Success) {
Gson gson = new Gson();

@SuppressWarnings("unchecked")
ListofGridRecords<TraderTransaction> traderlist =
gson.fromJson(returndata.get("ResultData").toString(), ListofGridRecords.class);

Log.i("LIST DATA:", "" + traderlist);
for (TraderTransaction trader: traderlist.GridRecords) {
HashMap<String, String> map = new HashMap<String, String>();
map.put(TRANS_FIRST_COLUMN, currentformatter.format(trader.AddedTime));
map.put(TRANS_SECOND_COLUMN, trader.TransactId);
map.put(TRANS_THIRD_COLUMN, trader.OtherPartyAccountNo);
map.put(TRANS_FOURTH_COLUMN, trader.AmountPaid.toString());
map.put(TRANS_FIFTH_COLUMN, trader.AmountRecieved.toString());
map.put(TRANS_SIXTH_COLUMN, OpenOrClosed.values()[Integer.parseInt(trader.TransType.toString())].toString());
list.add(map);
}
}

我在 (TraderTransaction trader : traderlist.GridRecords) 处遇到转换错误。

我的 Json 数据看起来像,

{
"Messages":"RESULTS_RETRIEVAL_SUCCESSFULL",
"OperationResult":0,
"ResultData":{
"GridRecords":[
{
"AddedBy":"Distributor-9787457361-Rathinavel",
"AddedTime":"2013-04-12T16:26:24.0140117",
"AmountPaid":0.0,
"AmountRecieved":10000.0,
"ClosingBalance":10000.0,
"Narration":null,
"OtherPartyAccountNo":"0102849015327675",
"Status":2,
"TransType":2,
"TransactId":"TDRF483679051236"
},
{
"AddedBy":"Distributor-9787457361-Rathinavel",
"AddedTime":"2013-04-12T16:20:54.8681857",
"AmountPaid":0.0,
"AmountRecieved":0.0,
"ClosingBalance":0.0,
"Narration":null,
"OtherPartyAccountNo":"0102849015327675",
"Status":0,
"TransType":2,
"TransactId":"TDRF706925413802"
}
],
"Totalrecords":2
},
"UpdateAvailable":"0"
}

最佳答案

为了解析您的 JSON,我会使用稍微不同的策略。由于您似乎只对解析“ResultData”感兴趣,因此我将创建类来包装响应,与您已经创建的类非常相似,即:

public class Response {
@SerializedName("ResultData")
public ResultData resultData;
}

和,

public class ResultData {
@SerializedName("GridRecords")
public List<GridRecord> gridRecords;
@SerializedName("Totalrecords")
public int totalrecords;
}

和,

public class GridRecord {
@SerializedName("AddedTime")
public String addedTime;
@SerializedName("TransactId")
public String transactId;
//other fields...
}

如有必要,还有其他类(class)......

然后,为了解析您的 JSON 响应,您只需要做:

Gson gson = new Gson();
Response data = gson.fromJson(responseString, Response.class);

您将能够访问任何字段,例如:

data.resultData.gridRecords.transactId;

注意 1:如果您对 JSON 响应的更多字段感兴趣,则只需根据 JSON 响应向您的wrap 类添加更多字段。 ..

注意 2:我已将 addedTime 的类型更改为 String,而不是 Date,因为它为无法解析的日期抛出异常。无论如何,我通常将 Response 对象中的类型保留为简单的 String ,然后在我检索响应的类中,我在创建对象时进行正确的格式化,因为例如,当您将值放入 Map...

注解 3:使用注解 @SerializedName 将 JSON 响应和您的应用程序中的字段名称分开很有趣,以便遵循 Java您的属性未遵循的命名约定...

注意 4:您不应在类中使用 public 属性。更推荐使用 private/protected 属性及其对应的 getterssetters...

关于java - Json 数据分配给 java 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16010199/

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