Deserialize JSON array of arrays into List of Tuples using Newtonsoft(使用 Newtonsoft 将 JSON 数组反序列化为元组列表)
问题描述
我从在线服务提供商处收到如下所示的数据:
I am receiving data that looks like this from an online service provider:
{
  name: "test data",
  data: [
    [ "2017-05-31", 2388.33 ],
    [ "2017-04-30", 2358.84 ],
    [ "2017-03-31", 2366.82 ],
    [ "2017-02-28", 2329.91 ]
  ],
}       
我想将其解析成如下所示的对象:
I would like to parse it into an object that looks like this:
public class TestData
{
   public string Name;
   public List<Tuple<DateTime, double>> Data;
}
我唯一能找到的是如何将对象数组解析为元组列表,例如:Json.NET 元组的反序列化<...>在另一种类型中不起作用?
The only thing I have been able to find is how to parse an array of objects into a list of tulples, for example: Json.NET deserialization of Tuple<...> inside another type doesn't work?
有没有办法编写一个自定义转换器来处理这个问题?
Is there a way to write a custom converter that would handle this?
推荐答案
我从这里获取了通用的 TupleConverter:Json.NET 的 Tuple 反序列化<...>在另一种类型中不起作用?并制作了一个通用的 TupleListConverter.
I took the generic TupleConverter from here: Json.NET deserialization of Tuple<...> inside another type doesn't work? And made a generic TupleListConverter.
用法:
public class TestData
{
    public string Name;
    [Newtonsoft.Json.JsonConverter(typeof(TupleListConverter<DateTime, double>))]
    public List<Tuple<DateTime, double>> Data;
}
public void Test(string json)
{
    var testData = JsonConvert.DeserializeObject<TestData>(json);
    foreach (var tuple in testData.data)
    {
        var dateTime = tuple.Item1;
        var price = tuple.Item2;
        ... do something...
    }
}
转换器:
public class TupleListConverter<U, V> : Newtonsoft.Json.JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(Tuple<U, V>) == objectType;
    }
    public override object ReadJson(
        Newtonsoft.Json.JsonReader reader,
        Type objectType,
        object existingValue,
        Newtonsoft.Json.JsonSerializer serializer)
    {
        if (reader.TokenType == Newtonsoft.Json.JsonToken.Null)
            return null;
        var jArray = Newtonsoft.Json.Linq.JArray.Load(reader);
        var target = new List<Tuple<U, V>>();
        foreach (var childJArray in jArray.Children<Newtonsoft.Json.Linq.JArray>())
        {
            var tuple = new Tuple<U, V>(
                childJArray[0].ToObject<U>(),
                childJArray[1].ToObject<V>()
            );
            target.Add(tuple);
        }
        return target;
    }
    public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
    {
        serializer.Serialize(writer, value);
    }
}
                        这篇关于使用 Newtonsoft 将 JSON 数组反序列化为元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 Newtonsoft 将 JSON 数组反序列化为元组列表
				
        
 
            
        - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - C#MongoDB使用Builders查找派生对象 2022-09-04
 - C# 中多线程网络服务器的模式 2022-01-01
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 
						
						
						
						
						
				
				
				
				