1

私は自分の質問に対する答えを見つけようと、何日も何時間も探し続けてきました。次の JSON 文字列があります。

{
    "id":   "658@787.000a35000122",
    "take": [{
            "level":    [0],
            "status":   [[3, [0]]]
        }]
}

これは、逆シリアル化する必要があるさまざまなメッセージのサンプルですが、今私が心を痛めているのはこのメッセージです。私にとって問題のある部分は、「ステータス」配列です。文字列を逆シリアル化した結果を受け入れる私のクラスは次のとおりです。

    [DataContract]
    public class ReceivedMsg
    {
        public ReceivedMsg()
        {
            move = new List<MoveOperation>();
        }

        [DataMember]
        public string id { get; set; }

        [DataMember]
        public List<MoveOperation> move { get; set; }

        [DataContract]
        public class Status
        {
            [DataMember]
            public int destination { get; set; }

            [DataMember]
            public int[] source { get; set; }
        }

        [DataContract]
        public class MoveOperation
        {
            public MoveOperation()
            {
                status = new List<Status>();
            }

            [DataMember]
            public int[] level;

            [DataMember]
            public List<Status> status { get; set; }
        }
    }

デシリアライズを行うコードは次のとおりです。

ReceivedMsg m = new ReceivedMsg();

m = JsonConvert.DeserializeObject<ReceivedMsg>(strResp, new JsonSerializerSettings { TraceWriter = traceWriter });

strResp は、JSON データを含む文字列です。

最初は .NET フレームワークの一部である JSON ライブラリを使用しようとしましたが、「ステータス」部分で行き詰まりました。それが、Json.NET を試すきっかけになりました。

私が得ているエラーは次のとおりです。

An unhandled exception of type 'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll

Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Roper.Roper+ReceivedMsg+Status' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

Path 'move[0].status[0]', line 6, position 16.

どんな助けでも大歓迎です。もちろん、必要に応じて追加情報を提供させていただきます。カスタム コンバーターを作成してみましたが、私の C# の知識はまだそのレベルに達していないと思います。同様の質問に対する回答として提供された解決策を解読しようとしましたが、何かが欠けているに違いないと結論付けました。

私の長い質問を読んでくれたコミュニティに心から感謝します。あなたの寛大さは私を驚かせ続けています!

4

1 に答える 1

1

Json.NETを使用している 場合 (そうでない場合は、NuGet パッケージ マネージャーを使用してインストールできます)

PM> Install-Package Newtonsoft.Json

これは正しい方向にあなたを指すはずです:)

void Main()
{
    string json = @"{
    ""id"":   ""658@787.000a35000122"",
    ""take"": [{
            ""level"":    [0],
            ""status"":   [[3, [0]]]
        }]
}";
    RootObject root = JsonConvert.DeserializeObject<RootObject>(json);

}

public class Take
{
    [JsonProperty("level")]
    public int[] Level { get; set; }

    [JsonProperty("status")]
    public object[][] Status { get; set; }
}

public class RootObject
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("take")]
    public Take[] Take { get; set; }
}

ファイル構造

于 2014-10-20T05:36:45.843 に答える