我在将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"
}
答案 0 :(得分:1)
为了解析你的JSON,我会使用略有不同的策略。由于您似乎只对解析“ResultData”感兴趣,因此我创建了 wrap 响应的类,与您已经创建的类非常相似,即:
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...
}
和其他课程,如有必要......
然后,为了解析您的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 :为了遵循Java命名约定,使用注释@SerializedName
对于分离JSON响应中和您的应用中的字段名称很有意义,你的属性没有关注......
注4 :您不应在班级中使用public
属性。建议使用private/protected
属性及其通讯员getters
和setters
...