将JSON转换为VB.NET数据表的问题

时间:2019-09-16 09:20:11

标签: json vb.net

以下是我的JSON响应文本。我验证了响应,没有错误。 我正在尝试将其转换为数据表,但它却为null或错误。

JSON响应:

    {
  "data": {
    "b2b": [
      {
        "inv": [
          {
            "itms": [
              {
                "num": 1,
                "itc": {
                  "tx_cs": 0,
                  "elg": "ip",
                  "tx_i": 180
                },
                "itm_det": {
                  "csamt": 0,
                  "rt": 18,
                  "txval": 1000,
                  "iamt": 180
                }
              }
            ],
            "val": 1000,
            "inv_typ": "R",
            "flag": "N",
            "updby": "S",
            "pos": "27",
            "idt": "24-07-2017",
            "rchrg": "N",
            "cflag": "U",
            "inum": "191001",
            "chksum": "52d0e920428464d85721bfcd7f3bfb4f16fd00d93a9df7d6a6f0814bed716c28"
          },
          {
            "itms": [
              {
                "num": 1,
                "itc": {
                  "tx_cs": 0,
                  "elg": "ip",
                  "tx_i": 18
                },
                "itm_det": {
                  "csamt": 0,
                  "rt": 18,
                  "txval": 100,
                  "iamt": 18
                }
              }
            ],
            "val": 100,
            "inv_typ": "R",
            "flag": "N",
            "updby": "S",
            "pos": "27",
            "idt": "24-07-2017",
            "rchrg": "N",
            "cflag": "U",
            "inum": "191002",
            "chksum": "aaa1efcf335549b58059c9f3d03807d7c41b007022216f8a90db12c60cd2b9ef"
          }
        ],
        "cfs": "N",
        "ctin": "1225586"
      }
    ]
  },
  "header": {
    "email": "test@test.com",
    "gstin": "65656451",
    "retperiod": "072017",
    "gst_username": "sampleaccount",
    "state_cd": "27",
    "ip_address": "192.168.2.200",
    "txn": "s4f5sdf54sdf5s4df5",
    "client_id": "removedfortest",
    "client_secret": "removedfortest",
    "authorization": "Basic a4s5df45asdf54as5d4f",
    "ret_period": "072017"
  },
  "status_cd": "1",
  "status_desc": "request succeeds"
}

以下是我定义的类

Public Class Itc
        Public Property tx_cs As Integer
        Public Property elg As String
        Public Property tx_i As Integer
    End Class

    Public Class ItmDet
        Public Property csamt As Integer
        Public Property rt As Integer
        Public Property txval As Integer
        Public Property iamt As Integer
    End Class

    Public Class Itm
        Public Property num As Integer
        Public Property itc As Itc
        Public Property itm_det As ItmDet
    End Class

    Public Class Inv
        Public Property itms As Itm()
        Public Property val As Integer
        Public Property inv_typ As String
        Public Property flag As String
        Public Property updby As String
        Public Property pos As String
        Public Property idt As String
        Public Property rchrg As String
        Public Property cflag As String
        Public Property inum As String
        Public Property chksum As String
    End Class

    Public Class B2b
        Public Property inv As Inv()
        Public Property cfs As String
        Public Property ctin As String
    End Class

    Public Class Data
        Public Property b2b As B2b()
    End Class

    Public Class Header
        Public Property email As String
        Public Property gstin As String
        Public Property retperiod As String
        Public Property gst_username As String
        Public Property state_cd As String
        Public Property ip_address As String
        Public Property txn As String
        Public Property client_id As String
        Public Property client_secret As String
        Public Property authorization As String
        Public Property ret_period As String
    End Class

    Public Class Example
        Public Property data As Data
        Public Property status_cd As String
        Public Property status_desc As String
        Public Property header As Header
    End Class

然后我尝试执行以下操作:

下面在数据表中返回Null

Dim table as datatable = JsonConvert.DeserializeObject(Of RootObject(Of DataTable))(responsetext).Table

也尝试过: 以下不允许我键入rootofTable.data

    Dim rootOfList = JsonConvert.DeserializeObject(Of RootObject(Of List(Of data)))(responsetext)
Dim table As DataTable = rootOfTable.data

这些都没有将我的值返回到数据表。 数据集保持为空。

这就是我想要的 enter image description here

请帮助解决。

谢谢

1 个答案:

答案 0 :(得分:1)

可以(至少可以想到)有4种不同的方法,这是一个示例,我认为这对您来说就足够了。

您基本上只需设置json的哪一部分进入哪一列。

Imports Newtonsoft.Json.Linq

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim JsonP As JObject = JObject.Parse(TextBox1.Text)
    Dim SetPointer As JToken = JsonP("data")("b2b")(0)("inv")

    For Each item In SetPointer
        Dim NewDR As DataRow = TempDT.NewRow
        NewDR("val") = item("val")
        NewDR("inv_typ") = item("val")
        NewDR("flag") = item("flag")
        NewDR("updby") = item("updby")
        NewDR("pos") = item("pos")
        NewDR("idt") = item("idt")
        NewDR("rchrg") = item("rchrg")
        NewDR("cflag") = item("cflag")
        NewDR("inum") = item("inum")
        NewDR("chksum") = item("chksum")
        NewDR("itms_num") = item("itms")(0)("num")
        NewDR("itms_itc_cs") = item("itms")(0)("itc")("tx_cs")
        TempDT.Rows.Add(NewDR)
    Next
    DataGridView1.DataSource = TempDT
End Sub

Dim TempDT As New DataTable
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    TempDT.Columns.Add("val")
    TempDT.Columns.Add("inv_typ")
    TempDT.Columns.Add("flag")
    TempDT.Columns.Add("updby")
    TempDT.Columns.Add("pos")
    TempDT.Columns.Add("idt")
    TempDT.Columns.Add("rchrg")
    TempDT.Columns.Add("cflag")
    TempDT.Columns.Add("inum")
    TempDT.Columns.Add("chksum")
    TempDT.Columns.Add("itms_num")
    TempDT.Columns.Add("itms_itc_cs")
End Sub
End Class

enter image description here

我并没有做所有的事情,这只是概念的证明。


使用json可视化工具。 https://jsonformatter.curiousconcept.com/,然后在浏览时将其写为{},如果是数组[],则写出数字。

就像您可以完全写出(“ data”)(“ b2b”)(0)(“ inv”)(0)(“ itms”)(0)(“ itc”)(“ tx_cs”)以获得价值。但是导航到更近的地方然后像(“ itms”)那样写一部分会更容易。

由于数组大多数情况下具有动态数量的成员,因此大多数情况下您不能为数组编写固定路径,因此,您必须执行(x)并遍历项目,而不是(0)。 / p>