简体   繁体   English

在JSON.NET中反序列化数组

[英]Deserializing array in JSON.NET

I have a JSON output that I need to deserialize into a .NET object: 我有一个JSON输出,需要反序列化为.NET对象:

{   "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? 如果我使用的是JsonConvert,应该如何构造我的类以使反序列化起作用?

If the types for raw_data are both doubles then something like this: 如果raw_data的类型均为double,则如下所示:

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/ 您可能想查看的超酷网站: 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. 您可能会遇到麻烦,因为您将这些类型视为整数和双精度值(对于C#程序员而言是自然的),但是Json不了解这些类型,只能将数字表示为小数。

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. 我能看到的唯一原因是,如果需要将第一个值保留为整数,这将是一种更自然地限制变量值的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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