尝试反序列化JSON时,我遇到以下异常
找不到类型
[simple type, class MyObj$obj$Card]:
的合适构造函数无法在[Source: java.io.StringReader@4344ee21; line: 1, column: 201]
(通过引用链:MyObj["obj"]->Obj["cards"]
)
JSON是
{
"obj":{
"api":"OK",
"cache":false,
"cards":[
{
"id":1232995897,
"items":[
{
"id":"vmdSJLpnY",
"cat":50,
"rating":0.0
}
]
},
{
"id":0005897,
"items":[
{
"id":"vxdSJLpnY",
"cat":50,
"rating":0.0
}
]
}
]
}
}
在Obj
课程中,我有以下声明
@JsonProperty("cards") private Card[] cards;
产生上述异常。将类型Card[]
更改为Object[]
不会产生异常,但缺少我希望得到的正确映射。
任何线索如何解决?一个片段将是伟大的! 这个错误究竟意味着什么?
更新
我已经包含了Java类。
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
@JsonIgnoreProperties(ignoreUnknown=true)
public final class MyObj {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Obj {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Card {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Item {
@JsonProperty("id") private String id;
@JsonProperty("cat") private String cat;
@JsonProperty("rating") private String rating;
public final String getId() { return id; }
public final String getCat() { return cat; }
public final String getRating() { return ranting; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("items") private Item[] items;
public final Item[] getItems() { return items; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("cards") private Card[] cards;
public Card[] getCards() { return cards; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("obj") MyObj obj;
public final Card[] getCards(){ return apiServiceResultsNoLists.getCards(); }
}
答案 0 :(得分:38)
我认为问题很可能是卡对象
修改强>
我有两件事:
*你没有制定者
*您没有可以设置文件的公共构造函数。
如果你没有给它任何(合法*)手段,反序列化器应该如何填充你的字段呢?
解决方案:
- >将公共设置者添加到班级中
- >或创建使用@JsonCreator
*:当然,解析器可以做反射性的“mofidy the visibility”技巧,但是来吧,这不是“它的意思是玩”
<强> EDIT2 强> 我认为这应该有用,但是我无法测试它 - 我现在没有杰克逊正确设置的项目(这只是它的一部分,但我认为很容易解释我想要的东西)显示。)注意,我将数组更改为List:
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Card {
@JsonIgnoreProperties(ignoreUnknown=true)
public final class Item {
@JsonProperty("id") private String id;
@JsonProperty("cat") private String cat;
@JsonProperty("rating") private String rating;
@JsonCreator
public Item(@JsonProperty("id") String id, @JsonProperty("cat") String cat, @JsonProperty("rating") String rating) {
this.id = id;
this.cat = cat;
this.rating = rating;
}
public final String getId() { return id; }
public final String getCat() { return cat; }
public final String getRating() { return ranting; }
public final String toString() { return new Gson().toJson(this); }
}
@JsonProperty("items") private List<Item> items;
@JsonCreator
public Card(@JsonProperty("items") List<Item> items) {
this.items = items;
}
public final List<Item> getItems() { return items; }
public final String toString() { return new Gson().toJson(this); }
}
答案 1 :(得分:8)
我前几天有同样的症状并接受了关于JsonCreator
和JsonProperty
的建议,但我得到了同样的错误信息。事实证明,我正在研究的项目是使用quickxml和codehaus版本的jackson。我将codehaus属性放在我的类上,但使用了quickxml的ObjectMapper来进行解析。他们喜欢过渡性的依赖。
答案 2 :(得分:6)
我一直面临同样的问题。我的Json数据集如下
{ "count": 1917, "data": [ { "id": "1", "generated_id": "Z1-1156", "first_name": "Maruf", "last_name": "Ahmed", "full_name": "Mr. Maruf Ahmed", "email": "maruf1990@gmail.com", "phone": "+8801676798306", "company_name": "S M Style Ltd.", "website": "http:\\/\\/smartex-bd.com\\/", "address": "\\\nRoad No:5 House No:18\\\nLevel:3\\\nLane:\\\nBlock:F\\\nSector:2\\\nArea:Mirpur\\\nDhaka 1216\\\nMirpur\\\nBangladesh", "industry_type": "Apparel Retailer", "job_title": "Designer", "department": "Research & Development", "date": "7\\/10\\/2015 1:04:43 PM" }, { "id": "2", "generated_id": "Z2-1157", "first_name": "Akramul", "last_name": "Hoque", "full_name": "Mr. Akramul Hoque", "email": "akram@nassagroup.org", "phone": "+8801732005564", "company_name": "Nassa Group", "website": "www,nassagroup.org", "address": "Nassa Group\\\nRoad No:238 House No:\\\nLevel:\\\nLane:\\\nBlock:\\\nSector:\\\nArea:Gulshan li\\\nDhaka 1208\\\nTejgaon Industrial Area\\\nBangladesh", "industry_type": "Denim Garments Manufacturer", "job_title": "Merchandiser (Senior)", "department": "Merchandising", "date": "7\\/9\\/2015 10:56:22 PM" } ] }
private DataTable GetData()
{
//Needed to have both connections open in order to preserve the scope of var foamOrders and var webOrders, which are both needed in order to perform the comparison.
using (var foam = Databases.Foam(isDebug))
{
using (MySqlConnection web = new MySqlConnection(Databases.ConnectionStrings.Web(isDebug)))
{
var foamOrders = foam.DataTableEnumerable(@"
SELECT foreignID
FROM Orders
WHERE order_id NOT LIKE 'R35%'
AND originpartner_code = 'VN000011'
AND orderDate > Getdate() - 7 ")
.Select(o => new
{
order = o[0].ToString()
.Trim()
}).ToList();
var webOrders = web.DataTableEnumerable(@"
SELECT ORDER_NUMBER FROM transactions AS T WHERE
(Str_to_date(T.ORDER_DATE, '%Y%m%d %k:%i:%s') >= DATE_SUB(Now(), INTERVAL 7 DAY))
AND (STR_TO_DATE(T.ORDER_DATE, '%Y%m%d %k:%i:%s') <= DATE_SUB(NOW(), INTERVAL 1 HOUR))
", 300)
.Select(o => new
{
order = o[0].ToString()
.Trim()
}).ToList();
List<OrderNumber> on = new List<OrderNumber>();
foreach (var w in webOrders)
{
if (!foamOrders.Contains(w))
{
OrderNumber o = new OrderNumber();
o.orderNumber = w.order;
on.Add(o);
}
}
return on.ToDataTable();
}
}
}
public class OrderNumber
{
public string orderNumber { get; set; }
}
}
问题是内部类声明,它应该是静态的。因为杰克逊需要像
那样访问它 @JsonIgnoreProperties(ignoreUnknown = true)
public class Visitors {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Visitor{
@JsonProperty("id")
public String mId;
@JsonProperty("generated_id")
public String mGenId;
@JsonProperty("first_name")
public String mFirstName;
@JsonProperty("last_name")
public String mLastName;
@JsonProperty("full_name")
public String mFullName;
@JsonProperty("email")
public String mEmail;
@JsonProperty("phone")
public String mPhone;
@JsonProperty("company_name")
public String mCompanyName;
@JsonProperty("website")
public String mWebsite;
@JsonProperty("address")
public String mAdress;
@JsonProperty("industry_type")
public String mIndustryType;
@JsonProperty("job_title")
public String mJobtitle;
@JsonProperty("department")
public String mDepartment;
@JsonProperty("date")
public String mDate;
public Visitor(@JsonProperty("id") String Id,
@JsonProperty("generated_id") String GenId,
@JsonProperty("first_name") String FirstName,
@JsonProperty("last_name") String LastName,
@JsonProperty("full_name") String FullName,
@JsonProperty("email") String Email,
@JsonProperty("phone") String Phone,
@JsonProperty("company_name") String CompanyName,
@JsonProperty("website") String Website,
@JsonProperty("address") String Adress,
@JsonProperty("industry_type") String IndustryType,
@JsonProperty("job_title") String Jobtitle,
@JsonProperty("department") String Department,
@JsonProperty("date") String date)
{
mId = Id;
mGenId = GenId;
mFirstName = FirstName;
mLastName= LastName;
mFullName= FullName;
mEmail= Email;
mPhone= Phone;
mCompanyName= CompanyName;
mWebsite= Website;
mAdress= Adress;
mIndustryType= IndustryType;
mJobtitle= Jobtitle;
mDepartment= Department;
mDate= date;
}
}
@JsonProperty("count") public String mCount;
@JsonProperty("data") public ArrayList<Visitor> mVisitorList;
@JsonCreator
public Visitors( @JsonProperty("count") String Count,
@JsonProperty("data") ArrayList<Visitor> visitorList)
{
mCount = Count;
mVisitorList = visitorList;
}
}
所以我将内部new Visitors.Visitor();
类声明为Visitor
。它对我有用。
答案 3 :(得分:3)
我认为你在其中一个类中缺少默认构造函数 或者:将@JsonCreator注释添加到构造函数
答案 4 :(得分:1)
查看您的Card类 - 很可能没有可访问的默认构造函数或构造函数缺少正确的映射。
答案 5 :(得分:0)
将默认构造函数添加到该类和所有嵌套的用户定义的类中。这样就可以了。