Home > Enterprise >  How to read Json array using vb.net?
How to read Json array using vb.net?

Time:04-04

I try to read json array which return from RestResponse using following code , i am used RestClient to call POST method

                Dim clientPI As RestClient = New RestClient("https://sampleurl")
                Dim requestPI = New RestRequest(Method.POST)
                requestPI.AddParameter("name", "Aravind")
                requestPI.AddParameter("username", "aravind")
                requestPI.AddParameter("password", "aravind123")
                requestPI.AddParameter("id", "100")
                Dim responsePI As RestResponse = clientPI.Execute(requestPI)
                Dim StrReturnPI As JValue = responsePI.Content.ToString
                Dim serPI As JObject = JObject.Parse(StrReturnPI)
                Dim dataPI As List(Of JToken) = serPI.Children().ToList

                For Each item As JProperty In dataPI
                        item.CreateReader()
                        Select Case item.Name
                            Case "result"
                                output  = "Document_id:"   vbCrLf
                                For Each comment As JObject In item.Values
                                    Dim u As String = comment("Document_id")
                                    output  = u   vbTab
                                Next

                            Case "Inv_Date"
                                output  = "Inv_Date:"   vbCrLf
                                For Each msgDate As JObject In item.Values
                                    Dim f As String = msgDate ("value")
                                    output  = f   vbTab
                                Next
                           Case "Inv_Number"
                                output  = "Inv_Number:"   vbCrLf
                                For Each msg As JObject In item.Values
                                    Dim f As String = msg("value")
                                    output  = f   vbTab
                                Next

                        End Select
                 Next

Sample Json

    {{
 "result": [
  {
    "Document_id": "598dce483b97c",
    "file_name": "2022-04-04_09_13_14.847228.pdf",
   
    "Inv_Date": {
      "value": "15-Jul-2019",
      
    },
    "Inv_Number": {
      "value": "1920021347",
    }
  }
]],
  "status": "complete"
}}
 

From above code i can read only value from Document_id and file_name but cant read value from Inv_Date and Inv_Number.

Anyone help will be appreciated.

Thanks and regards Aravind

CodePudding user response:

First of all, sorry because I can't check against a proper vb.net ide actually so some typo can occur, but I think this may solution your problem:

Get the values inside the result case as the structure is defined that way

                        Case "result"
                            output  = "Document_id:"   vbCrLf
                            For Each comment As JObject In item.Values
                                Dim u As String = comment("Document_id")
                                output  = u   vbTab
                                Dim invDate As JObject = comment("Inv_Date")
                                Dim invNumber As  JObject = comment("Inv_Number")
                                Dim invDateValue As String = invDate("value")
                                output  = invDateValue   vbTab
                                Dim invNumberValue As String = invNumber("value")
                                output  = invNumberValue   vbTab
                            Next
  • Related