【问题标题】:Cinchoo ETL json to csvCinchoo ETL json 到 csv
【发布时间】:2021-07-20 15:23:00
【问题描述】:

我正在尝试将 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 结果中,Monitor 类的格式不正确

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",无,,

使用包:

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

我做错了什么?

【问题讨论】:

    标签: c# json csv etl choetl


    【解决方案1】:

    您需要用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";
      
       
    }
    

    【讨论】:

    • 谢谢,它可以工作,但是如果我不知道数组的长度怎么办?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    • 2019-06-18
    • 2020-12-24
    • 1970-01-01
    • 2020-07-08
    相关资源
    最近更新 更多