简体   繁体   中英

Deserializing array in 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?

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM