1

I have been searching for days, hours at a time trying to find an answer to my question. I have the following JSON string:

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

That is a sample of a variety of messages that I need to deserialize, but it is the one that is causing me heartache right now. The problematic portion for me is the "status" array. My class to accept the results of deserializing the string is:

    [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; }
        }
    }

The code to do the deserializing is:

ReceivedMsg m = new ReceivedMsg();

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

Where strResp is the string containg the JSON data.

I initially tried using the JSON library that is a part of the .NET framework, but got stuck on the "status" portion. That's what prompted me to try Json.NET.

The error I am getting is:

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.

Any help would be greatly appreciated. Of course I will be happy to furnish additional information as needed. I tried doing a custom converter, but I think my C# knowledge is not quite at that level yet. I have been trying to decipher the solutions offered in answer to similar questions, but concluded that I must be missing something.

My sincere thanks to the community for taking the time to read my lengthy question. Your generosity continues to amaze me!

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
  • By your code it doesn't seem like you are using JSON.NET. That is pure .NET and JSON. This is JSON.NET: https://www.nuget.org/packages/Newtonsoft.Json/ – BrunoLM Oct 20 '14 at 05:34

1 Answers1

1

If you're using Json.NET (and if not you can install it using the NuGet package manager)

PM> Install-Package Newtonsoft.Json

This should then point you in the right direction :)

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; }
}

File structure

Aydin
  • 15,016
  • 4
  • 32
  • 42
  • Aydin, please accept my sincere thanks! Your answer is exactly what I need. I am still in the toddler stage of learning C#, and there are a lot of things I have to learn. I am unfamiliar with using object, but after doing a little reading on it your use of it makes perfect sense. Every day is a good day, but when I learn something it makes the day even better. Thanks again for your help and making my good day even better! – alot_to_learn Oct 20 '14 at 16:46