微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将JSON数组转换为Java类对象列表

我有一个来自WFC服务的JSON字符串.当我尝试将JSON数组转换为List对象时,我遇到以下错误

".JsonMappingException: Can not deserialize instance of java.util.ArrayList out of START_OBJECT token at [Source: java.io.StringReader@41f27f18; line: 1, column: 1]"

java类(卡类):

public class Card {
    public String ID;
    public String CompanyID;
    public String CompanyName;
    public String FiscalCode;
    public String Limit;
    public String StateID;
    public String CardState;
    public String Deleted;
    public String Sold;
    public String StartDate;
    public String InvoiceStartDate;
    public String Quantity;
    public String Value;
    public String CardTypeID;
    public String CardType;
    public String SoldChanged;
    public String DriverName;
    public String VehiclePlateNumber;
    public String VehicleID;
    public String discount;
    public String ContractID;
    public String discountPerMonth;
    public String ProductID;
    public String ProductStateID;
    public String Mail;
    public String WithoutLimit;
    public String ContractSold;
    public String ContractLimit;
    public String NumberOfTransactions;
    public String DriverNameOnly;
    public String DriverSurnameOnly;
}

反序列化的Java代码

strResponse = responseHandler.handleResponse(response);
if (strResponse.contains("Credit") || strResponse.contains("Debit")) {
    ObjectMapper mapper = new ObjectMapper();
    strResponse= strResponse.replace("\"GetCardsResult\":", "");
    userCards = mapper.readValue(strResponse, mapper.getTypeFactory().constructCollectionType(List.class, Card.class));
}

JSON字符串:

{     "GetCardsResult":"[{\"ID\":3,\"CompanyID\":1155,\"CompanyName\":\"test\",\"FiscalCode\":null,\"Code\":\"1423127205\",\"Limit\":0.000,\"StateID\":1,\"CardState\":\"Activ\",\"Deleted\":false,\"Sold\":0.000,\"StartDate\":\"\/Date(1412974800000+0300)\/\",\"InvoiceStartDate\":\"\/Date(-62135596800000+0200)\/\",\"Quantity\":null,\"Value\":0.0,\"CardTypeID\":1,\"CardType\":\"Credit\",\"SoldChanged\":false,\"DriverName\":\"\",\"VehiclePlateNumber\":\"B 222 ART\",\"VehicleID\":null,\"discount\":null,\"ContractID\":15,\"discountPerMonth\":null,\"ProductID\":null,\"ProductStateID\":null,\"Mail\":\"\",\"WithoutLimit\":true,\"ContractSold\":null,\"ContractLimit\":null,\"NumberOfTransactions\":null,\"DriverNameOnly\":null,\"DriverSurnameOnly\":null},{\"ID\":2881,\"CompanyID\":1155,\"CompanyName\":\"test\",\"FiscalCode\":null,\"Code\":\"test0000\",\"Limit\":125.000,\"StateID\":1,\"CardState\":\"Activ\",\"Deleted\":false,\"Sold\":132.330,\"StartDate\":\"\/Date(1436130000000+0300)\/\",\"InvoiceStartDate\":\"\/Date(-62135596800000+0200)\/\",\"Quantity\":null,\"Value\":0.0,\"CardTypeID\":1,\"CardType\":\"Credit\",\"SoldChanged\":false,\"DriverName\":\"aaa aaa\",\"VehiclePlateNumber\":\"aaa\",\"VehicleID\":null,\"discount\":null,\"ContractID\":15,\"discountPerMonth\":null,\"ProductID\":null,\"ProductStateID\":null,\"Mail\":\"\",\"WithoutLimit\":true,\"ContractSold\":null,\"ContractLimit\":null,\"NumberOfTransactions\":null,\"DriverNameOnly\":null,\"DriverSurnameOnly\":null}]" }

提前致谢!

解决方法:

试试这个:

    try {            
         JSONObject jsonObject = null;
         yourjsonString.replace("\\", "");
         jsonObject = new JSONObject(yourjsonString);
         String newJSONString = jsonObject.get("GetCardsResult").toString();
         JSONArray jsonMainArr = new JSONArray(newJSONString);
         //Now just loop the json Array
         for (int i = 0; i < jsonMainArr.length(); ++i) {                     
         JSONObject rec = jsonMainArr.getJSONObject(i);                     
         card.set_id(rec.get("ID").toString());                     
         //....      
       }                                                       
       } catch (JSONException e) {
         e.printstacktrace();
       } 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐