1

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. So lets start with the csv file:

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:

{
"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; }
    }
Robmeister89
  • 97
  • 1
  • 9
  • The json output that I posted is straight from the game that I'm trying to replicate. The previous game output was csv so I'm trying to convert it myself. – Robmeister89 Aug 18 '17 at 21:09
  • If you simply need to read a CSV file, you can use the .Net built-in type `TextFieldParser` as explained [here](https://stackoverflow.com/a/3508572/3744182). – dbc Aug 18 '17 at 21:25
  • Okay but how I do make column 0 = Id, column 1 = Sea, etc, etc.. I've never had to do this while coding before so I'm new to this.. – Robmeister89 Aug 18 '17 at 21:48

1 Answers1

0

Here is another simple way to convert CSV to JSON object structure using Cinchoo ETL

First load the CSV file using ChoCSVReader, it gives you list of dynamic objects represents each CSV row. Then transform them to Player by exposing constructor taking CSV dynamic object and load them to its members as below

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.

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.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34
  • I'm getting errors for PlayerAttr, PlayerPer, and PlayerSkills? – Robmeister89 Sep 05 '17 at 18:00
  • Added the missing classes to the answer. – Cinchoo Sep 06 '17 at 00:34
  • Okay, that worked! Now I have another issue... It says "Argument 1: cannot convert from 'WindowsFormsApplication1.Form1.Players' to 'System.Collections.Generic.IEnumerable' – Robmeister89 Sep 06 '17 at 13:41
  • Figured it out, had Player instead of Players in the var w line... Now the e has an error saying it cannot be declared in this cope because that name is used in an enclosing local scope to define a local or parameter... Also, anyway you can help me with the csvreader? – Robmeister89 Sep 06 '17 at 15:02
  • the error is pretty clear that you already defined e variable in the defined scope. pls change one of them. – Cinchoo Sep 06 '17 at 19:05