我使用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?
发布于 2012-10-15 19:45:51
这是因为Person不是public。将您的类定义更改为
public class Person
{
public string type;
public string id;
public string name;
}我试着按原样运行你的代码,得到了同样的异常。我将Person修改为public,然后异常就消失了。
https://stackoverflow.com/questions/12893027
复制相似问题