首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取JSON输出

读取JSON输出
EN

Stack Overflow用户
提问于 2017-11-09 17:36:09
回答 1查看 90关注 0票数 0

我正在使用VB.net在视觉工作室。在过去的一天里,我一直在艰难地反序列化和读取从我正在使用的API返回的JSON输出。我认为我之所以挣扎,主要是因为我不理解这是什么输出(我相信这是一个JSON数组的集合,而不是一个数组的输出)。以下是我收到的输出:

代码语言:javascript
复制
    [{
    "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

我试过以下几种方法,但都没有用:

代码语言:javascript
复制
        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)

我收到错误:

代码语言:javascript
复制
Newtonsoft.Json.JsonReaderException: 'Additional text encountered after 
finished reading JSON content: ,. Path '', line 1, position 216.'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-09 20:18:44

这里是如何解决我的问题的:这个问题与我反序列化对象的方式有关。我未能认识到如何处理JSON数组与JSON对象和JSON属性

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47208218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档