【问题标题】:I want to copy my JSON data to a SQL table using VB.Net我想使用 VB.Net 将我的 JSON 数据复制到 SQL 表
【发布时间】:2019-09-24 05:27:55
【问题描述】:

我在 JSON 文件中有数据,我想将其复制到 SQL 表中。

这是我的 JSON 文件数据:

{
  "SuccessResponse": {
    "Head": {
      "RequestId": "A1",
      "RequestAction": "GetBrands",
      "ResponseType": "Brands",
      "Timestamp": "2019-09-23T22:43:08-05:00"
    },
    "Body": {
      "Brands": [
        {
          "BrandId": 23885,
          "Name": "Brand 1",
          "GlobalIdentifier": "B1"
        },
        {
          "BrandId": 23886,
          "Name": "Brand 2",
          "GlobalIdentifier": "B2"
        },
        {
          "BrandId": 23887,
          "Name": "Brand 3",
          "GlobalIdentifier": "B3"
        }
      ]
    }
  }
}

我已经弄清楚如何使用数组提取数据,但是我想将上述JSON文件中的所有数据记录复制到SQL表中。

Dim x As String = "{'products':[{'title':'Mouse','price':'20$'},{'title':'KeyBoard','price':'30$'}]}"
Dim result = JsonConvert.DeserializeObject(x)

Console.WriteLine(result("products")(0)("title") & " - " & result("products")(0)("price"))
Console.WriteLine(result("products")(1)("title") & " - " & result("products")(1)("price"))

我从上面的代码得到如下结果:

Mouse - 20$
KeyBoard - 30$

但我需要这样的结果:

【问题讨论】:

  • 请编辑问题而不是添加 cmets。
  • 您可以查看此问题中发布的解决方案:stackoverflow.com/questions/12634516/…
  • 感谢您的反馈,您分享的主题是将数据保存到 Sql 但我希望将我的 JSON 文件数据脱盐并复制到 SQL 数据库中。..
  • @SajidSoomro “将数据保存到 Sql ”和“复制到 SQL 数据库”有什么区别?
  • @Mary 完全没有区别,但是我需要我的程序来转换 JSON 脚本中的数据并将其保存到数据库字段中。

标签: json vb.net


【解决方案1】:

我用你提供的 json 保存一个文件。它名为 Sajid.json,位于我的 bin/Debug 目录中。

我使用的类如下。接近 .PasteSpecial 使用的内容,但我更喜欢列表而不是数组。

Public Class Rootobject
    Public Property SuccessResponse As Successresponse
End Class

Public Class Successresponse
    Public Property Head As Head
    Public Property Body As Body
End Class

Public Class Head
    Public Property RequestId As String
    Public Property RequestAction As String
    Public Property ResponseType As String
    Public Property Timestamp As Date
End Class

Public Class Body
    Public Property Brands As List(Of Brand)
End Class

Public Class Brand
    Public Property BrandId As Integer
    Public Property Name As String
    Public Property GlobalIdentifier As String
End Class

在按钮代码中,json 字符串被反序列化为类。该命令是使用参数创建的。

将参数添加到命令中,并为每个插入的参数设置相同的值。我不得不猜测数据类型和字段大小。您需要检查数据库的实际类型和大小。

值改变的参数被添加到循环外,只有值在循环内改变。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim s As String = File.ReadAllText("sajid.json")
    Dim myJs = JsonConvert.DeserializeObject(Of Rootobject)(s)
    Using cn As New SqlConnection("Your connection string")
        Using cmd As New SqlCommand("Insert Into SomeTable (RequestID, RequestAction, ResposeType, TimeStamp, Brands_BrandId, Brands_Name, Brands_GlobalIdentifier)
          Values (@RequestID, @RequestAction, @ResposeType, @TimeStamp, @Brands_BrandId, @Brands_Name, @Brands_GlobalIdentifier);", cn)
            With cmd.Parameters
                .Add("@RequestID", SqlDbType.VarChar, 10).Value = myJs.SuccessResponse.Head.RequestId
                .Add("@RequestAction", SqlDbType.VarChar, 50).Value = myJs.SuccessResponse.Head.RequestId
                .Add("@ResposeType", SqlDbType.VarChar, 50).Value = myJs.SuccessResponse.Head.ResponseType
                .Add("@TimeStamp", SqlDbType.DateTime).Value = myJs.SuccessResponse.Head.Timestamp
                .Add("@Brands_BrandId", SqlDbType.VarChar, 10)
                .Add("@Brands_Name", SqlDbType.VarChar, 10)
                .Add("@Brands_GlobalIdentifier", SqlDbType.VarChar, 10)
            End With
            Dim bList = myJs.SuccessResponse.Body.Brands
            cn.Open()
            For Each b In bList
                cmd.Parameters("@Brands_BrandId").Value = b.BrandId
                cmd.Parameters("@Brands_Name").Value = b.Name
                cmd.Parameters("Brands_GlobalIdentifier").Value = b.GlobalIdentifier
                cmd.ExecuteNonQuery()
            Next
        End Using
    End Using
End Sub

【讨论】:

    【解决方案2】:

    代码就像魅力一样,谢谢。

    附近有一点错别字

        cmd.Parameters("Brands_GlobalIdentifier").Value = b.GlobalIdentifier
    

    我已通过在 Brands_GlobalIdentifier 参数前添加 @Sign 来纠正它

        cmd.Parameters("@Brands_GlobalIdentifier").Value = b.GlobalIdentifier
    

    【讨论】:

      猜你喜欢
      • 2018-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多