Cinchoo ETL json 转 csv

Cinchoo ETL json to csv

我正在尝试将 json 格式化为 csv 文件。

这里是数据结构

public class Location
{
    [JsonProperty("latitude")]
    public double Latitude { get; set; }

    [JsonProperty("longitude")]
    public double Longitude { get; set; }
}

public class Monitor
{
    [JsonProperty("channelId")]
    public int ChannelId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("alias")]
    public string Alias { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("typeId")]
    public int TypeId { get; set; }

    [JsonProperty("pollutantId")]
    public int PollutantId { get; set; }

    [JsonProperty("units")]
    public string Units { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }
}

public class StationCall
{
    [JsonProperty("stationId")]
    public int StationId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("shortName")]
    public string ShortName { get; set; }

    [JsonProperty("stationsTag")]
    public string StationsTag { get; set; }

    [JsonProperty("location")]
    public Location Location { get; set; }

    [JsonProperty("timebase")]
    public int Timebase { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("owner")]
    public string Owner { get; set; }

    [JsonProperty("regionId")]
    public int RegionId { get; set; }

    [JsonProperty("monitors")]
    public Monitor[] Monitor { get; set; }

    [JsonProperty("StationTarget")]
    public string StationTarget { get; set; }

    [JsonProperty("additionalTimebases")]
    public string AdditionalTimebases { get; set; }

    [JsonProperty("isNonContinuous")]
    public string IsNonContinuous { get; set; }

    public static string fileName = "stations.txt";
  
   
}

问题出在 csv 结果中,class 监视器格式不正确

StationId,Name,ShortName,StationsTag,Location.Latitude,Location.Longitude,Timebase,Active,Owner,RegionId,Monitor,StationTarget,AdditionalTimebases,IsNonContinuous 63,משושים-דרדרה,Meshushim,Meshushim,00.0000,00.3583,60,True,None,16,"Sfika_App.Entities.Monitor,Sfika_App.Entities.Monitor,Sfika_App.Entities.Monitor",None,,

使用包:

 using (var parser = new ChoCSVWriter<StationCall>("D:/Export.csv").WithFirstLineHeader().UseNestedKeyFormat(true))
        {
            //parser.with
            parser.Write(jsonContent);
        }

我做错了什么?

您需要用 RangeAttribute 修饰 Monitor 属性 以指定可能的预期数组范围

public class StationCall
{
    [JsonProperty("stationId")]
    public int StationId { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("shortName")]
    public string ShortName { get; set; }

    [JsonProperty("stationsTag")]
    public string StationsTag { get; set; }

    [JsonProperty("location")]
    public Location Location { get; set; }

    [JsonProperty("timebase")]
    public int Timebase { get; set; }

    [JsonProperty("active")]
    public bool Active { get; set; }

    [JsonProperty("owner")]
    public string Owner { get; set; }

    [JsonProperty("regionId")]
    public int RegionId { get; set; }

    [JsonProperty("monitors")]
    [Range(0, 1)]
    public Monitor[] Monitor { get; set; }

    [JsonProperty("StationTarget")]
    public string StationTarget { get; set; }

    [JsonProperty("additionalTimebases")]
    public string AdditionalTimebases { get; set; }

    [JsonProperty("isNonContinuous")]
    public string IsNonContinuous { get; set; }

    public static string fileName = "stations.txt";
  
   
}