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

时间:2016-01-27 10:10:17

标签: java android json

我有一个来自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}]" }

提前致谢!

3 个答案:

答案 0 :(得分:2)

试试这个:

-Xms256m
-Xmx3080m
-XX:MaxPermSize=350m
-XX:ReservedCodeCacheSize=225m
-XX:+UseCompressedOops

答案 1 :(得分:0)

  1. 在编译正则表达式之前或之后,您将什么传递给mapper?字符串?

  2. 您是否尝试过像 Gson这样的外部库?您需要的只是new Gson().fromJson(strResponse, new TypeToken<List<Card>>() {}.getType(););

答案 2 :(得分:0)

尝试使用GSON非常高效且易于实现,下面的示例将是您的POJO类。

public class Post {

    @SerializedName("id")
    public long ID;
    public String title;
    public String author;
    public String url;
    @SerializedName("date")
    public Date dateCreated;
    public String body;

    public List tags;

    public Post() {

    }
}

//Tag.java
public class Tag {

    public String name;
    public String url;

    public Tag() {

    }
}

这就是你将JSON字符串解析为Object Class的方法,

Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("M/d/yy hh:mm a");
Gson gson = gsonBuilder.create();
List<Post> posts = new ArrayList<Post>();
posts = Arrays.asList(gson.fromJson(reader, Post[].class));
content.close();