c# - fastJson反序列化未处理的异常

标签 c# json deserialization fastjson

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

class Person
{
    public string type;
    public string id;
    public string name;
}

Json字符串为:

[{
"type": "/basketball/basketball_player", 
"id": "/en/rasheed_wallace", 
"name": "Rasheed Wallace"
},
{
"type": "/basketball/basketball_player", 
"id": "/en/tayshaun_prince", 
"name": "Tayshaun Prince"
}]

当我使用代码时:

var obj = fastJSON.JSON.Instance.ToObject<List<Person>>(str);

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

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'

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

var obj = JsonConvert.DeserializeObject<List<Person>>(str);

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

最佳答案

这是因为Person 不是public。将您的类定义更改为

public class Person
{
    public string type;
    public string id;
    public string name;
}

我尝试按原样运行您的代码,但遇到了同样的异常。我将 Person 修改为 public 并且异常消失了。

关于c# - fastJson反序列化未处理的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12893027/

相关文章:

java - 我尝试序列化不可序列化的对象列表不起作用

c# - 无法访问的函数和方法对 C# 中的性能有多大影响

c# - 生成字母表中的字母数组

c# - 如何从字符串中删除逗号分隔值?

c++ - Qt QSqlQuery 返回 json

android - 错误 Android - 收到 HTML 而不是 JSON

java - 我无法在 Java 中序列化和反序列化对象

C# 如何在 Word Interop 中获取标题的自动编号

php - 更改 json key 名称 [ 使用 json_encode 生成的 json ]

Java 序列化 - java.io.InvalidClassException 本地类不兼容