5

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を正しい方法で使用していませんか?

4

1 に答える 1

5

Personはないからですpublic。クラス定義を次のように変更します

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

コードをそのまま実行してみましたが、同じ例外が発生しました。私はに変更Personpublic、例外はなくなりました。

于 2012-10-15T11:45:50.867 に答える