如何使用 vb.net 读取 Json 数组?
How to read Json array using vb.net?
我尝试使用以下代码从 RestResponse 中读取 json 数组 return,我使用 RestClient 调用 POST 方法
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
样本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"
}}
从上面的代码我只能读取 Document_id 和 file_name 的值,但不能读取 Inv_Date 和 Inv_Number 的值。
任何人的帮助将不胜感激。
感谢和问候
亚拉文
首先,抱歉,因为我无法检查正确的 vb.net ide 实际上可能会出现一些拼写错误,但我认为这可能会解决您的问题:
获取值 inside result 案例,因为结构是这样定义的
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
我尝试使用以下代码从 RestResponse 中读取 json 数组 return,我使用 RestClient 调用 POST 方法
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
样本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"
}}
从上面的代码我只能读取 Document_id 和 file_name 的值,但不能读取 Inv_Date 和 Inv_Number 的值。
任何人的帮助将不胜感激。
感谢和问候 亚拉文
首先,抱歉,因为我无法检查正确的 vb.net ide 实际上可能会出现一些拼写错误,但我认为这可能会解决您的问题:
获取值 inside result 案例,因为结构是这样定义的
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