如何编写包含CSVHelper数组的对象?

发布于 2025-01-17 18:37:09 字数 1102 浏览 3 评论 0原文

在 C#/.net 中,我将 Api 请求反序列化为对象。该对象包含数组,当我使用 csvHelper 将其写入文件时,如下所示: https://joshclose.github.io/CsvHelper/examples/writing/write-class-objects/ 该对象被写入 csv,但没有数组。

我序列化到的对象如下所示:

public class MyMainObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyMissingArray[] data { get; set; }
}
public class MyMissingArray{
    public string id { get; set; }
    public string value { get; set; }
}

将这些数组放入 csv 文件的最佳方法是什么?

in C#/.net I deserialize a Api-request into a object. This object contains arrays, when i write it to file using csvHelper like this: https://joshclose.github.io/CsvHelper/examples/writing/write-class-objects/
The object is written to csv, but without the arrays.

My object which i serialize into, look like this:

public class MyMainObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyMissingArray[] data { get; set; }
}
public class MyMissingArray{
    public string id { get; set; }
    public string value { get; set; }
}

What is the best way to get these arrays into the csv file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃颗糖壮壮胆 2025-01-24 18:37:09

我认为最简单的方法是在 ClassMap 中使用 Convert。您可以按照您想要的方式格式化数据

void Main()
{
    var records = new List<MyMainObject>
    {
        new MyMainObject { 
            Id = 1, 
            Name = "Object1", 
            data = new MyMissingArray [] 
            {
                new MyMissingArray { id = "one", value = "value1" },
                new MyMissingArray { id = "two", value = "value2" },
                new MyMissingArray { id = "three", value = "value3" }
            }
        }
    };

    using (var csv = new CsvWriter(Console.Out, CultureInfo.InvariantCulture))
    {
        csv.Context.RegisterClassMap<MyMainObjectMap>();
        csv.WriteRecords(records);
    }
}

public class MyMainObjectMap : ClassMap<MyMainObject>
{
    public MyMainObjectMap()
    {
        Map(x => x.Id);
        Map(x => x.Name);
        Map(x => x.data).Name("MyData").Convert(args =>
        {
            var flattenMissingArray = args.Value.data.Select(d => d.id + ":" + d.value);
            return string.Join(",", flattenMissingArray);
        });
    }
}

public class MyMainObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyMissingArray[] data { get; set; }
}
public class MyMissingArray
{
    public string id { get; set; }
    public string value { get; set; }
}

I think the easiest way will be to use Convert in a ClassMap. You can format the data how you want it.

void Main()
{
    var records = new List<MyMainObject>
    {
        new MyMainObject { 
            Id = 1, 
            Name = "Object1", 
            data = new MyMissingArray [] 
            {
                new MyMissingArray { id = "one", value = "value1" },
                new MyMissingArray { id = "two", value = "value2" },
                new MyMissingArray { id = "three", value = "value3" }
            }
        }
    };

    using (var csv = new CsvWriter(Console.Out, CultureInfo.InvariantCulture))
    {
        csv.Context.RegisterClassMap<MyMainObjectMap>();
        csv.WriteRecords(records);
    }
}

public class MyMainObjectMap : ClassMap<MyMainObject>
{
    public MyMainObjectMap()
    {
        Map(x => x.Id);
        Map(x => x.Name);
        Map(x => x.data).Name("MyData").Convert(args =>
        {
            var flattenMissingArray = args.Value.data.Select(d => d.id + ":" + d.value);
            return string.Join(",", flattenMissingArray);
        });
    }
}

public class MyMainObject
{
    public int Id { get; set; }
    public string Name { get; set; }
    public MyMissingArray[] data { get; set; }
}
public class MyMissingArray
{
    public string id { get; set; }
    public string value { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文