1

我在将 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);
  }
}

我在 for (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"
}
4

1 回答 1

1

为了解析您的 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...
}

和其他课程,如有必要...

然后,为了解析您的 JSON 响应,您只需执行以下操作:

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

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

data.resultData.gridRecords.transactId;

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

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

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

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

于 2013-04-16T12:34:57.843 回答