fastJson反序列化未处理的异常

Cha*_*erQ 5 c# json deserialization fastjson

我正在使用fastJson库将json字符串反序列化为"Person"对象.Person类定义如下:

class Person
{
    public string type;
    public string id;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

Json字符串是:

[{
"type": "/basketball/basketball_player", 
"id": "/en/rasheed_wallace", 
"name": "Rasheed Wallace"
},
{
"type": "/basketball/basketball_player", 
"id": "/en/tayshaun_prince", 
"name": "Tayshaun Prince"
}]
Run Code Online (Sandbox Code Playgroud)

当我使用代码时:

var obj = fastJSON.JSON.Instance.ToObject<List<Person>>(str);
Run Code Online (Sandbox Code Playgroud)

它显示了一个未处理的异常

Failed to fast create instance for type
'System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null]]' from assemebly  
System.Collections.Generic.List`1[[JsonSample.Person, JsonSample, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, 
PublicKeyToken=b77a5c561934e089'
Run Code Online (Sandbox Code Playgroud)

但是如果我使用代码,那么在Newtonsoft.Json库中一切正常:

var obj = JsonConvert.DeserializeObject<List<Person>>(str);
Run Code Online (Sandbox Code Playgroud)

那么这是fastJson的错误还是我没有以正确的方式使用fastJson?

Mik*_*Two 5

这是因为Person不是public.将您的班级定义更改为

public class Person
{
    public string type;
    public string id;
    public string name;
}
Run Code Online (Sandbox Code Playgroud)

我尝试按原样运行您的代码并获得相同的异常.我修改Personpublic,异常消失了.

  • 我有相同的错误消息,但在我的情况下,问题是缺少默认构造函数.要修复我的错误,我可以添加一个默认构造函数,或使用JSONParameters ParametricConstructorOverride,如下所示:`var newobj = fastJSON.JSON.ToObject(jsonText,new fastJSON.JSONParameters {ParametricConstructorOverride = true});` (2认同)