简体   繁体   English

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

[英]Issue with Converting JSON to VB.NET datatable

following is me JSON response text. 以下是我的JSON响应文本。 I validated response and there is no error. 我验证了响应,没有错误。 I am trying to convert it to data table but it gives me null or error. 我正在尝试将其转换为数据表,但它给我null或错误。

JSON Response: 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"
}

Following are the classes i defined 以下是我定义的课程

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

and then i am trying to do following: 然后我尝试执行以下操作:

Following return me Null in datatable 以下返回我在数据表中为Null

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

also tried: following does not allow me to type in rootofTable.data 也尝试过:以下不允许我键入rootofTable.data

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

None of these are returning me values to datatable. 这些都没有使我的价值观回到数据表。 Dataset stay null. 数据集保持为空。

This is what i want 这就是我要的 在此处输入图片说明

Please help to resolve. 请帮忙解决。

Thanks 谢谢

There are at least 4 different ways it could be done (that I can think of) here is one example, I think it should be enough for you. 至少有4种不同的方法可以实现(我能想到),这里是一个示例,我认为这对您来说应该足够了。

You basically just set which part of json goes into what column. 您基本上只需设置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

在此处输入图片说明

I didn't do everything, it's simply a demonstration of concept. 我没有做任何事情,这只是概念的演示。


Use json visualizer. 使用json visualizer。 https://jsonformatter.curiousconcept.com/ then as you navigate trough it you write out the name if it's {}, number if it's an array [] . https://jsonformatter.curiousconcept.com/,然后在浏览时,将名称写成{},写出名称,如果是数组[]。

Like you can totally write out ("data")("b2b")(0)("inv")(0)("itms")(0)("itc")("tx_cs") to get value from it. 就像您可以完全写出(“ data”)(“ b2b”)(0)(“ inv”)(0)(“ itms”)(0)(“ itc”)(“ tx_cs”)从中获取价值一样。 But it would be easier to navigate to somewhere closer and then just write a part of it like ("itms"). 但是导航到更近的地方然后像(“ itms”)那样写一部分会更容易。

And you can't write a fixed path for arrays most of the time as they have dynamic amount of members most of the time, so instead of (0) you will have to do (x) and loop over items. 而且,由于数组在大多数情况下具有动态数量的成员,因此您通常无法为数组编写固定路径,因此,您必须要做(x)并遍历项目,而不是(0)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM