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:
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)
{
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];
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);
}