简体   繁体   English

CSV至C#中的JSON

[英]CSV to JSON in C#

I've used the online converters but they don't do exactly what I need them to do so I was hoping to write a small program to get it to convert the csv to json in the exact format I need. 我已经使用了在线转换器,但是它们并没有完全按照我的需要去做,所以我希望编写一个小程序来获取它,以将csv转换为所需的确切格式。 So lets start with the csv file: 因此,让我们从csv文件开始:

Id,Sea,First,Last,Team,Coll,Num,Age,Hgt,Wgt,Pos,Attr/Str,Attr/Agi,Attr/Arm,Attr/Spe,Attr/Han,Attr/Intel,Attr/Acc,Attr/PBl,Attr/RBl,Attr/Tck,Attr/KDi,Attr/KAc,Attr/End,Per/Lea,Per/Wor,Per/Com,Per/TmPl,Per/Spor,Per/Soc,Per/Mny,Per/Sec,Per/Loy,Per/Win,Per/PT,Per/Home,Per/Mkt,Per/Mor,Skills/QB,Flg,Trait,Skills/RB,Skills/FB,Skills/G,Skills/T,Skills/C,Skills/WR,Skills/TE,Skills/CB,Skills/SS,Skills/FS,Skills/DE,Skills/LB,Skills/DT,Skills/K,Skills/P
,2018,David,Bush,,Stanford,19,21,76,212,QB,68,55,89,70,31,96,99,1,5,24,1,1,74,34,71,62,33,76,88,15,15,40,14,31,33,9,94,,,,,,,,,,,,,,,,,,

Now this is the output in json: 现在这是json中的输出:

{
"Players": [{
    "Id": 2938,
    "Sea": 2018,
    "First": "Harold",
    "Last": "Dalton",
    "Team": "0",
    "Coll": "Western Kentucky",
    "Num": 87,
    "Age": 20,
    "Hgt": 76,
    "Wgt": 224,
    "Pos": "WR",
    "Attr": {
        "Str": 59,
        "Agi": 79,
        "Arm": 1,
        "Spe": 87,
        "Han": 77,
        "Intel": 38,
        "Acc": 1,
        "PBl": 1,
        "RBl": 11,
        "Tck": 21,
        "KDi": 1,
        "KAc": 1,
        "End": 58
    },
    "Per": {
        "Lea": 62,
        "Wor": 76,
        "Com": 61,
        "TmPl": 58,
        "Spor": 62,
        "Soc": 94,
        "Mny": 92,
        "Sec": 32,
        "Loy": 31,
        "Win": 68,
        "PT": 90,
        "Home": 36,
        "Mkt": 45,
        "Mor": 70
    },
    "Skills": {
        "WR": 53,
        "TE": 31
    },
    "Flg": "None",
    "Trait": "None"
},

Yes its a different player, that's not the point here :) 是的,它是一个不同的玩家,这不是重点:)

And here's the code I have so far, without the conversion process... 这是到目前为止我没有转换过程的代码...

    SaveFileDialog sfd = new SaveFileDialog();
    OpenFileDialog ofd = new OpenFileDialog();
    public static string fileName;

    private void open_btn_Click(object sender, EventArgs e)
    {
        ofd.Filter = "CSV Files (.csv)|*.csv";
        ofd.Title = "Open CSV File";

        if (ofd.ShowDialog() == DialogResult.OK)
        {
            richTextBox1.LoadFile(ofd.FileName, RichTextBoxStreamType.PlainText);

            fileName = ofd.FileName;
            open_label.Text = System.IO.Path.GetFileName(fileName);
        }
    }

    private void save_btn_Click(object sender, EventArgs e)
    {
        sfd.Filter = "JSON Files (.json)|*.json";
        sfd.Title = "Save JSON File";

        if (sfd.ShowDialog() == DialogResult.OK)
        {
            //how do I convert the loaded .csv file into the json format below???
        }
    }

    public class Rootobject
    {
        public Player[] Players { get; set; }
    }

    public class Player
    {
        public int Id { get; set; }
        public int Sea { get; set; }
        public string First { get; set; }
        public string Last { get; set; }
        public string Team { get; set; }
        public string Coll { get; set; }
        public int Num { get; set; }
        public int Age { get; set; }
        public int Hgt { get; set; }
        public int Wgt { get; set; }
        public string Pos { get; set; }
        public Attr Attr { get; set; }
        public Per Per { get; set; }
        public Skills Skills { get; set; }
        public string Flg { get; set; }
        public string Trait { get; set; }
    }

    public class Attr
    {
        public int Str { get; set; }
        public int Agi { get; set; }
        public int Arm { get; set; }
        public int Spe { get; set; }
        public int Han { get; set; }
        public int Intel { get; set; }
        public int Acc { get; set; }
        public int PBl { get; set; }
        public int RBl { get; set; }
        public int Tck { get; set; }
        public int KDi { get; set; }
        public int KAc { get; set; }
        public int End { get; set; }
    }

    public class Per
    {
        public int Lea { get; set; }
        public int Wor { get; set; }
        public int Com { get; set; }
        public int TmPl { get; set; }
        public int Spor { get; set; }
        public int Soc { get; set; }
        public int Mny { get; set; }
        public int Sec { get; set; }
        public int Loy { get; set; }
        public int Win { get; set; }
        public int PT { get; set; }
        public int Home { get; set; }
        public int Mkt { get; set; }
        public int Mor { get; set; }
    }

    public class Skills
    {
        public int G { get; set; }
        public int T { get; set; }
        public int C { get; set; }
        public int WR { get; set; }
        public int TE { get; set; }
        public int DT { get; set; }
        public int DE { get; set; }
        public int LB { get; set; }
        public int SS { get; set; }
        public int CB { get; set; }
        public int FS { get; set; }
        public int RB { get; set; }
        public int FB { get; set; }
        public int QB { get; set; }
        public int K { get; set; }
        public int P { get; set; }
    }

Here is another simple way to convert CSV to JSON object structure using Cinchoo ETL 这是使用Cinchoo ETL将CSV转换为JSON对象结构的另一种简单方法

First load the CSV file using ChoCSVReader, it gives you list of dynamic objects represents each CSV row. 首先使用ChoCSVReader加载CSV文件,它为您提供了代表每个CSV行的动态对象列表。 Then transform them to Player by exposing constructor taking CSV dynamic object and load them to its members as below 然后通过使用CSV动态对象公开构造函数将它们转换为Player,并将它们加载到其成员,如下所示

public class Player
{
    public Player(dynamic obj)
    {
        Id = ChoUtility.CastTo<int>(obj.Id);
        Sea = ChoUtility.CastTo<int>(obj.Sea);
        First = obj.First;
        Last = obj.Last;
        Team = obj.Team;
        Coll = obj.Coll;
        Num = ChoUtility.CastTo<int>(obj.Num);
        Age = ChoUtility.CastTo<int>(obj.Age);
        Hgt = ChoUtility.CastTo<int>(obj.Hgt);
        Wgt = ChoUtility.CastTo<int>(obj.Wgt);
        Pos = obj.Pos;
        Flg = String.IsNullOrEmpty(obj.Flg) ? "None" : obj.Flg;
        Trait = String.IsNullOrEmpty(obj.Trait) ? "None" : obj.Trait;

        Attr = new PlayerAttr();
        Attr.Str = ChoUtility.CastTo<int>(obj.Attr_Str);
        Attr.Agi = ChoUtility.CastTo<int>(obj.Attr_Agi);

        Per = new PlayerPer();
        Per.Lea = ChoUtility.CastTo<int>(obj.Per_Lea);
        Per.Wor = ChoUtility.CastTo<int>(obj.Per_Wor);


        Skills = new PlayerSkills();
        Skills.WR = ChoUtility.CastTo<int>(obj.Skills_WR);
        Skills.TE = ChoUtility.CastTo<int>(obj.Skills_TE);
    }

    public int Id { get; set; }
    public int Sea { get; set; }
    public string First { get; set; }
    public string Last { get; set; }
    public string Team { get; set; }
    public string Coll { get; set; }
    public int Num { get; set; }
    public int Age { get; set; }
    public int Hgt { get; set; }
    public int Wgt { get; set; }
    public string Pos { get; set; }

    public PlayerAttr Attr { get; set; }
    public PlayerPer Per { get; set; }

    public PlayerSkills Skills { get; set; }
    public string Flg { get; set; }
    public string Trait { get; set; }
}  

UPDATE: 更新:

public class Players
{
    public Player[] players { get; set; }
}
public class PlayerAttr
{
    public int Str { get; set; }
    public int Agi { get; set; }

}
public class PlayerPer
{
    public int Lea { get; set; }
    public int Wor { get; set; }

}
public class PlayerSkills
{
    public int WR { get; set; }
    public int TE { get; set; }

}

Finally use the ChoJSONWriter object to write the final object. 最后,使用ChoJSONWriter对象编写最终对象。

using (var p = new ChoCSVReader("Players.csv").WithFirstLineHeader())
{
    using (var w = new ChoJSONWriter<Players>("Players.json").Configure( c=> c.UseJSONSerialization = true).Configure(c => c.SupportMultipleContent = true))
    {
        w.Write(new RootObject { players = p.Select(e => new Player(e)).ToArray() });
    }
}

Hope this helps. 希望这可以帮助。

Disclaimer: I'm the author of this library. 免责声明:我是这个图书馆的作者。

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

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