Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
This is my json file :
{
    "imgHeight": 1840,
    "imgWidth": 2452,
    "objects": [
        {
            "date": "06-Nov-2017 14:42:59",
            "deleted": 0,
            "draw": true,
            "id": 0,
            "label": "Unlabeled",
            "polygon": [
                [
                    1860.4728789986093,
                    0.0
                ],
                [
                    1599.4436717663423,
                    307.0931849791377
                ],
                [
                    1591.7663421418638,
                    304.53407510431157
                ],
                [
                    1581.5299026425591,
                    317.32962447844227
                ],
                [
                    1566.1752433936022,
                    319.88873435326843
                ],
                [
                    1520.1112656467317,
                    373.6300417246175
                ],
                
                [
                    1417.7468706536856,
                    419.69401947148816
                ],
                [
                    1422.865090403338,
                    427.37134909596665
                ],
                [
                    1427.9833101529903,
                    440.16689847009735
                ],
                [
                    1399.8331015299027,
                    483.6717663421419
                ],
                [
                    1376.8011126564672,
                    519.4993045897079
                ],
                [
                    1384.4784422809457,
                    522.058414464534
                ],
                [
                    1384.4784422809457,
                    616.7454798331015
                ],
               
                [
                    762.614742698192,
                    360.8344923504868
                ],
                [
                    396.66203059805287,
                    92.12795549374131
                ],
                [
                    396.66203059805287,
                    71.65507649513214
                ],
                
                [
                    335.2433936022253,
                    0.0
                ]
            ],
            "user": "Rashmitha",
            "verified": 0
        },


i have to convert in csv file. Actually main task is to convert json to shapefile but i already have solution of csv to shape, so i just want to convert into csv.

in this file i need only :
id:
label:
polygon:
user:
in my csv file.

What I have tried:

C#
public class Object
        {
            public string date { get; set; }
            public int deleted { get; set; }
            public bool draw { get; set; }
            public int id { get; set; }
            public string label { get; set; }
            public List<List<double>> polygon { get; set; }
            public string user { get; set; }
            public int verified { get; set; }
        }

        public class RootObject
        {
            public int imgHeight { get; set; }
            public int imgWidth { get; set; }
            public List<object> objects { get; set; }
        }

        static void Main(string[] args)
        {
            using (StreamReader r = new StreamReader(@"D:\New_Task\000\_cam1_1509438801455_000004.json"))
            {
                string json = r.ReadToEnd();
                var data = JsonConvert.DeserializeObject<rootobject>(json);
               
              jsonStringToCSV(json);        
               
            }
        }
        public static void jsonStringToCSV(string jsonContent)
        {
            //used NewtonSoft json nuget package
            XmlNode xml = JsonConvert.DeserializeXmlNode("{records:{record:" + jsonContent + "}}");
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml.InnerXml);
            XmlReader xmlReader = new XmlNodeReader(xml);
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(xmlReader);
            var dataTable = dataSet.Tables[1];

            //Datatable to CSV
            var lines = new List<string>();
            string[] columnNames = dataTable.Columns.Cast<datacolumn>().
                                              Select(column => column.ColumnName).
                                              ToArray();
            var header = string.Join(",", columnNames);
            lines.Add(header);
            var valueLines = dataTable.AsEnumerable()
                               .Select(row => string.Join(",", row.ItemArray));
            lines.AddRange(valueLines);
            File.WriteAllLines(@"D:\New_Task\000\_cam1_1509438801455_000004.csv", lines);
        }
Posted
Updated 22-Nov-17 6:54am
v2

Google Search usually has all the answers. I used this search: json to csv[^] and found this solution:

JSON string to CSV and CSV to JSON conversion in c# - Stack Overflow[^]
 
Share this answer
 
Hello,
Here is something that could help You to solve the problem.
Program code uses excellent Newtonsoft Json.NET for manipulating Json documents.
At the Url below You can download it and learn how to use it :

Json.NET - Newtonsoft[^]

Here is program code :

void Json_To_Csv(string Json)
{

    JObject JSON = new JObject();

    JSON = JObject.Parse(Json);


    foreach (var objects in JSON["objects"])
    {
        MessageBox.Show(objects["id"].ToString());

        MessageBox.Show(objects["label"].ToString());

        foreach (var polygon in objects["polygon"])
        {
            MessageBox.Show(polygon[0].ToString());

            MessageBox.Show(polygon[1].ToString());
        }

        MessageBox.Show(objects["user"].ToString());
    }

}


And don't forget using directives :

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;


All the best,
Željko Perić
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900