我在Visual Studios中使用 VB.net 。在过去的一天里,我一直在努力反序列化并读取从我正在使用的API返回的JSON输出。我相信我正在努力,主要是因为不了解这是什么输出(我相信它是一个JSON数组的集合,而不是只有一个数组的输出)。 这是我收到的输出:
[{
"Issue": {
"ID": 80,
"Name": "Cold",
"Accuracy": 90,
"Icd": "J00",
"IcdName": "Acute nasopharyngitis [common cold]",
"ProfName": "Common cold",
"Ranking": 1
},
"Specialisation": [{
"ID": 15,
"Name": "General practice",
"SpecialistID": 3
}]
}, {
"Issue": {
"ID": 11,
"Name": "Flu",
"Accuracy": 65.390625,
"Icd": "J10;J11",
"IcdName": "Influenza due to other identified influenza virus;Influenza, virus not identified",
"ProfName": "Influenza",
"Ranking": 2
},
"Specialisation": [{
"ID": 15,
"Name": "General practice",
"SpecialistID": 3
}, {
"ID": 19,
"Name": "Internal medicine",
"SpecialistID": 4
}]
}, {
"Issue": {
"ID": 83,
"Name": "Inflammation of the brain covering membranes",
"Accuracy": 45.4921875,
"Icd": "G00;G01;G02;G03",
"IcdName": "Bacterial meningitis, not elsewhere classified;Meningitis in bacterial diseases classified elsewhere;Meningitis in other infectious and parasitic diseases classified elsewhere;Meningitis due to other and unspecified causes",
"ProfName": "Meningitis",
"Ranking": 3
},
"Specialisation": [{
"ID": 15,
"Name": "General practice",
"SpecialistID": 3
}, {
"ID": 23,
"Name": "Infectiology",
"SpecialistID": 28
}, {
"ID": 19,
"Name": "Internal medicine",
"SpecialistID": 4
}, {
"ID": 27,
"Name": "Neurology",
"SpecialistID": 45
}]
}]
如果有人可以帮助我理解我在这里的结构,以及如何干净地将其解析为可查询的信息。我将非常感激。 我已经尝试过跟随这里和其他网站上的许多类似问题,但是我没有实现任何代码并且得到与错误定义数组或对象有关的错误。 我也在使用Newtonsoft.json
我尝试过以下无效:
For Each item As JProperty In data
item.CreateReader()
Select Case item.Name
Case "Issue"
output += "Issue:" + vbCrLf
For Each comment As JObject In item.Values
Dim u As String = comment("ID")
Dim d As String = comment("Name")
Dim c As String = comment("Accuracy")
output += u + vbTab + d + vbTab + c + vbCrLf
Next
Case "Specialisation"
output += "Specialisation:" + vbCrLf
For Each msg As JObject In item.Values
Dim f As String = msg("ID")
Dim t As String = msg("Name")
Dim d As String = msg("SpecialistID")
output += f + vbTab + t + vbTab + d + vbTab
Next
End Select
Next
MsgBox(output)
我收到错误:
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after
finished reading JSON content: ,. Path '', line 1, position 216.'
答案 0 :(得分:0)
以下是我解决问题的方法: 这个问题与我反序列化对象的方式有关。我没有认识到如何处理JSON数组与JSON对象和JSON属性
Public Function GetIllness()
response = <Insert API Query with header>.asJsonAsync(Of String)()
Dim client = JsonConvert.DeserializeObject(Of RootClass)(response.Result.Body)
MsgBox(client.DescriptionShort)
MsgBox(client.TreatmentDescription)
End Function
<Newtonsoft.Json.JsonObjectAttribute(MemberSerialization:=Newtonsoft.Json.MemberSerialization.OptIn)>
Partial Public Class RootClass
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Issue As Issue
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Specialisation() As Specialisation
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Description As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public DescriptionShort As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public MedicalCondition As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Name As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public PossibleSymptoms As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public ProfName As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Synonyms As Object
<Newtonsoft.Json.JsonPropertyAttribute()>
Public TreatmentDescription As String
End Class
'Type created for JSON at <<root>>
<Newtonsoft.Json.JsonObjectAttribute(MemberSerialization:=Newtonsoft.Json.MemberSerialization.OptIn)>
Partial Public Class Issue
<Newtonsoft.Json.JsonPropertyAttribute()>
Public ID As Integer
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Name As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Accuracy As Double
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Icd As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public IcdName As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public ProfName As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Ranking As Integer
End Class
'Type created for JSON at <<root>> --> Specialisation
<Newtonsoft.Json.JsonObjectAttribute(MemberSerialization:=Newtonsoft.Json.MemberSerialization.OptIn)>
Partial Public Class Specialisation
<Newtonsoft.Json.JsonPropertyAttribute()>
Public ID As Integer
<Newtonsoft.Json.JsonPropertyAttribute()>
Public Name As String
<Newtonsoft.Json.JsonPropertyAttribute()>
Public SpecialistID As Integer
End Class