使用CHOETL将JSON转换为CSV即可获取JSON数组中的第一项

马修·布朗

一直在努力解决以下问题:我收到来自我们的电子商务网站(Shopify)的订单的JSON响应。我需要根据响应创建一个CSV文件。在我获得订单项详细信息之前,一切对我都很好。我只得到数组中的第一项。我看到了其他解决方案,这些解决方案将其他数组项显示为其他列,但是我需要将其视为行。我所见过的许多示例也都在C#中使用,但我并不擅长。

订单类别

Imports ChoETL

Public Class Order
    <ChoJSONRecordField>
    Public Property Name As String
    <ChoJSONRecordField>
    Public Property Email As String
    <ChoJSONRecordField(JSONPath:="financial_status")>
    Public Property Financial_Status As String
    <ChoJSONRecordField(JSONPath:="line_items[*].title")>
    Public Property Title As String

End Class

创建CSV子

Private Shared Sub UsingPOCO()
    Using csv = New ChoCSVWriter("order3.csv").WithFirstLineHeader()

        Using json = New ChoJSONReader(Of Order)("order2.json")
            csv.Write(json)
        End Using
    End Using
End Sub

样本JSON

{
  "email": "[email protected]",
  "financial_status": "paid",
  "name": "#CCC94440",
  "line_items": [
    {
      "title": "product1",
      "quantity": 3
    },
    {
      "title": "product2",
      "quantity": 2
    },
    {
      "title": "product3",
      "quantity": 1
    }
  ]
}

CSV输出

在此处输入图片说明

我需要的

在此处输入图片说明

要么

在此处输入图片说明

更新#1我在另一个问题上找到了这个答案,这似乎是我想要的。但是我不知道如何将其转换为VB.net。我认为会用的答案是所选的答案更新#2。https://stackoverflow.com/a/57166153/2037475

更新#2,我能够将C#从其他答案转换为VB.net...。但是,我仍然看到以下错误,但仍在调查中:“'选择'不是'Dynamic()'的成员”

    Using fw = New StreamWriter("order3.csv", True)

        Using w = New ChoCSVWriter(fw).WithFirstLineHeader()

            Using r = New ChoJSONReader("order2.json").WithJSONPath("$.line_items[*]")
                w.Write(r.SelectMany(Function(r1) (CType(r1.line_items, Dynamic())).[Select](Function(r2) New With {r1.name, r2.title})))
            End Using
        End Using
    End Using

    Console.WriteLine(File.ReadAllText("order3.csv"))

更新3

我不需要坚持使用CHOETL,这只是我发现自己成功的第一件事。开放任何建议。

谢谢,马特

金鸡

这是VB.NET中的工作示例

        Dim json As String

        json = "
{
  ""email"": ""[email protected]"",
  ""financial_status"": ""paid"",
  ""name"": ""#CCC94440"",
  ""line_items"": [
    {
      ""title"": ""product1"",
      ""quantity"": 3
    },
    {
      ""title"": ""product2"",
      ""quantity"": 2
    },
    {
      ""title"": ""product3"",
      ""quantity"": 1
    }
  ]
}"
        Dim csv As New StringBuilder

        Using w = New ChoCSVWriter(csv).WithFirstLineHeader()
            Using r = ChoJSONReader.LoadText(json)
                w.Write(r.SelectMany(Function(r1) (CType(r1.line_items, Object())).[Select](Function(r2) New With {r1.email, r1.financial_status, r1.name, r2.title, r2.quantity})))
            End Using
        End Using

        Console.WriteLine(csv.ToString())

输出:

email,financial_status,name,title,quantity
[email protected],paid,#CCC94440,product1,3
[email protected],paid,#CCC94440,product2,2
[email protected],paid,#CCC94440,product3,1

更新#1:

从运输项目中检索价格

        json = "
{
  ""email"": ""[email protected]"",
  ""financial_status"": ""paid"",
  ""name"": ""#CCC94440"",
  ""line_items"": [
    {
      ""title"": ""item0"",
      ""quantity"": 3
    },
    {
      ""title"": ""item1"",
      ""quantity"": 2
    }
  ],
  ""shipping_lines"": [
    {
      ""title"": ""Free Shipping"",
      ""price"": ""1.00""
    }
  ]
}
"
        Dim csv As New StringBuilder

        Using w = New ChoCSVWriter(csv).WithFirstLineHeader()
            Using r = ChoJSONReader.LoadText(json)
                w.Write(r.SelectMany(Function(r1) CType(r1.line_items, Object()).[Select](Function(r2)
                                                                                              Return New With
                                                                                                {
                                                                                                    r1.email,
                                                                                                    r1.financial_status,
                                                                                                    r1.name,
                                                                                                    r2.title,
                                                                                                    r2.quantity,
                                                                                                    CType(r1.shipping_lines, Object())(0).price
                                                                                                }
                                                                                          End Function)))
            End Using
        End Using

        Console.WriteLine(csv.ToString())

输出:

email,financial_status,name,title,quantity,price
[email protected],paid,#CCC94440,item0,3,1.00
[email protected],paid,#CCC94440,item1,2,1.00

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章