如何在C#中将json文件转换为csv文件? [英] How do I convert json file to csv file in C#?

查看:155
本文介绍了如何在C#中将json文件转换为csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的json文件:

 {
imgHeight:1840,
imgWidth:2452,
对象:[
{
date:06-Nov-2017 14:42:59,
已删除:0,
draw:true ,
id:0,
label:未标记,
多边形:[
[
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
]
],
用户:Rashmitha,
已验证:0
},





i必须转换为csv文件。实际上主要任务是将json转换为shapefile,但我已经有csv解决方案的形状,所以我只想转换成csv。



在这个文件中我只需要:

id:

标签:

多边形:

用户:

in我的csv文件。



我的尝试:



  public   class  对象 
{
public string date {获得; set ; }
public int 已删除{ get ; set ; }
public bool draw { get ; set ; }
public int id { get ; set ; }
public string label { get ; set ; }
public 列表< List< double>> polygon { get ; set ; }
public string user { get ; set ; }
public int 已验证{获取; set ; }
}

public class RootObject
{
public int imgHeight { get ; set ; }
public int imgWidth { get ; set ; }
public 列表< object>对象{获取; set ; }
}

静态 void Main( string [] args)
{
使用(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)
{
// 使用NewtonSoft json nuget包
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 ];

// 数据到CSV
var lines = new List< string>();
string [] columnNames = dataTable.Columns.Cast< datacolumn>()。
选择(列= > 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);
}

解决方案

Google搜索通常包含所有答案。我使用了这个搜索: json to csv [ ^ ]并找到了这个解决方案:



JSON字符串到c#中的CSV和CSV到JSON转换 - 堆栈溢出 [ ^ ]


您好,

这可以帮助您解决问题。

程序代码使用优秀的Newtonsoft Json .NET用于操作Json文档。

在下面的Url你可以下载它并学习如何使用它:



Json.NET - Newtonsoft [ ^ ]



这是程序代码:



 void Json_To_Csv(string Json )
{

JObject JSON = new JObject();

JSON = JObject.Parse(Json);


foreach(JSON [objects中的var对象])
{
MessageBox.Show(objects [id]。ToString());

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

foreach(对象中的var多边形[polygon])
{
MessageBox.Show(polygon [0] .ToString());

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

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

}





不要忘记使用指令:



使用Newtonsoft.Json; 
使用Newtonsoft.Json.Converters;
使用Newtonsoft.Json.Linq;





一切顺利,
$ b $bŽeljkoPerić


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

解决方案

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[^]


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ć


这篇关于如何在C#中将json文件转换为csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆