将CSV数据标头和值转换为JSON格式 [英] Convert CSV data header and value to JSON format

查看:100
本文介绍了将CSV数据标头和值转换为JSON格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述







如何将值和标题检索从CSV转换为JSON格式?目前,我正在从标题中追加数据并与行内容结合并与另一列结合。



以下是我的工作: -



Hi,


How to convert value and header retrieve from CSV to JSON format? Currently, I'm appending data from header and combine with row content and join with another column.

Below is what I do:-

Imports LumenWorks.Framework.IO.Csv

Private Async Function ReadFromCSV() As Task
    Try
        Using csv As New CsvReader(New StreamReader("E:\DATA\Data001.csv"), True)
            csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent
            AddHandler csv.ParseError, AddressOf csv_ParseError
            Dim fieldCount As Integer = csv.FieldCount
            Dim headers() As String = csv.GetFieldHeaders()
            Do While csv.ReadNextRecord()
                For i As Integer = 0 To fieldCount - 1
                    Console.Write(String.Format("{0}:{1};", headers(i), csv(i)))
                Next i
                Console.WriteLine()
            Loop
        End Using
        Await Task.Delay(5000)
    Catch ex As Exception
        Console.WriteLine("{0} > Sending message failed. Ex - {1}", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss:fff"), ex.Message)
    End Try
End Function



除此之外,我希望它能够发送到Iot Hub。我想象的是: -




Beside, I want it to be able to send to Iot Hub. So I'm imagining:-

Dim telemetryDataPoint = New With {Key .csv_header(i) = csv_value(i)}





动态添加到JSON之后,如何动态循环到 New With {Key} ?因为目前我我将使用手动写入。将来会有更多的CSV文件具有动态标头数。例如File001.csv有7个标头,File002.csv有16个标头。所以有可能有1个函数将它转换为动态对象创造?



我尝试过:



使用csv作为新CsvReader(New StreamReader(E:\DATA \ Data0 01.csv),True)

csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent

AddHandler csv.ParseError,AddressOf csv_ParseError

Dim fieldCount As Integer = csv.FieldCount

Dim headers()As String = csv.GetFieldHeaders()

Do csv.ReadNextRecord()

For i As Integer = 0到fieldCount - 1

Console.Write(String.Format({0}:{1};,headers(i),csv(i)))

接下来我

Console.WriteLine()

循环

结束使用



After I dynamically add to JSON, how to dynamically loop into New With {Key}? Because currently I'm using manual write. In future, there will be some more CSV file that having dynamic header count. E.g. File001.csv have 7 headers, File002.csv have 16 headers. So is there a possible 1 function to translate it to dynamic object creation?

What I have tried:

Using csv As New CsvReader(New StreamReader("E:\DATA\Data001.csv"), True)
csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent
AddHandler csv.ParseError, AddressOf csv_ParseError
Dim fieldCount As Integer = csv.FieldCount
Dim headers() As String = csv.GetFieldHeaders()
Do While csv.ReadNextRecord()
For i As Integer = 0 To fieldCount - 1
Console.Write(String.Format("{0}:{1};", headers(i), csv(i)))
Next i
Console.WriteLine()
Loop
End Using

推荐答案

如果您还没有这样做,请使用NuGet添加对 Json.NET 的引用[ ^ ]:

If you haven't done so already, use NuGet to add a reference to Json.NET[^]:
Install-Package Newtonsoft.Json



导入LINQ到JSON命名空间:


Import the LINQ to JSON namespace:

Imports Newtonsoft.Json.Linq



然后,使用 JArray [ ^ ], JObject [ ^ ]和 JProperty [ ^ ]构建对象:


Then, use JArray[^], JObject[^] and JProperty[^] to build your objects:

Dim fieldCount As Integer = csv.FieldCount
Dim headers() As String = csv.GetFieldHeaders()
Dim jsonData As New JArray()

Do While csv.ReadNextRecord()
    Dim record As New JObject()
    
    For i As Integer = 0 To fieldCount - 1
        record.Add(New JProperty(headers(i), csv(i)))
    Next
    
    jsonData.Add(record)
Loop

Console.WriteLine(jsonData);



创建JSON [ ^ ]


这篇关于将CSV数据标头和值转换为JSON格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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