Deserializar matriz en JSON.NET

I have a JSON output that I need to deserialize into a .NET object:

{   "title": "Stock data",
    "raw_data": [
    [
      1088553600000,
      1.3635
    ],
    [
      1091232000000,
      1.3538
    ],
    [
      1093910400000,
      1.3563
    ]]
}

If I'm using JsonConvert, how should I structure my class such that the deserialization works?

preguntado el 12 de junio de 12 a las 21:06

What are the types in the "raw_data" that you're expecting? An int and a double, or are they both doubles? If they're different, they you may have a problem as the JSON is technically for "raw_data" is an array of arrays. Tho you could implement a custom serializer for your class and put it into anything you want. -

Both doubles should work fine for me. I couldn't find an example on how to structure a class for an array of arrays. Do you have any hints on how I should declare the raw_data property? -

Sure, I would think double[][] would work just fine, tho I haven't tried it. -

Appreciate the help - I went with Reinard's suggestion and it worked. Your suggestions probably works as well. -

Yep, it works too, just tried it myself. -

2 Respuestas

If the types for raw_data are both doubles then something like this:

public class MyClass
{
    public string title { get; set; }
    public List<List<double>> raw_data { get; set; }
}

Cool website you might want to check out: http://json2csharp.com/

Respondido el 12 de junio de 12 a las 21:06

Sorry, my original answer was worthless, here's a better answer.

You probably ran into trouble because you saw those types as an integer and double value (natural for a C# programmer), however Json knows nothing of these types and represents numbers simply as decimals.

As another answer has said, you can simply deserialize as a list of lists of doubles. Interestingly, there is an option to convert it to a more natural representation.

However, I am not recommending this as it's not exactly typesafe when deserializing (The type of the original element is lost when converting to decimal, you can only suppose this way would work)!

public class BarArrayConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        // Not properly implemented!
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        List<Bar> bars = new List<Bar>();

        // Read the first token after the start array
        reader.Read();

        // Parse elements of the array
        while (reader.TokenType == JsonToken.StartArray)
        {
            Bar b = new Bar();

            // Convert these as necessary to your internal structure. Note that they are nullable types.
            b.First = reader.ReadAsDecimal();
            b.Second = reader.ReadAsDecimal();


            // Read end array for this element
            reader.Read();
            // Pull off next array in list, or end of list
            reader.Read();

            bars.Add(b);
        }

        return bars;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }
}


class Bar
{
    public decimal? First;
    public decimal? Second;
}

class Foo
{
    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonConverter(typeof(BarArrayConverter))]
    [JsonProperty("raw_data")]
    public List<Bar> RawData { get; set; }
}

It is almost certainly more sensible to use the decimal types. The only reason I can see to do this is if there were a requirement to leave the first value as an integer, this would be a way to restrict the value of the variable in a more natural way.

Respondido el 12 de junio de 12 a las 23:06

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.