【问题标题】:Public member 'error' on type 'JObject' not found未找到类型“JObject”的公共成员“错误”
【发布时间】:2019-07-09 16:43:52
【问题描述】:

我试图从WebRequest 中捕获错误消息,当用户输入错误信息时,它会返回 400 Bad Request 消息。我想将消息显示到屏幕上,根据this,我应该能够反序列化包含 JSON 的字符串,然后访问如下错误消息:

    Try
        'My web request is here
    Catch ex As WebException            
        Using resp As HttpWebResponse = ex.Response
            Using data As Stream = resp.GetResponseStream()
                Using reader = New StreamReader(data)

                    Dim bodyContent As String = reader.ReadToEnd()
                    Dim bodyObj = JsonConvert.DeserializeObject(bodyContent)

                    lblMyLabel.Text = bodyObj.error.message
                End Using
            End Using
        End Using
    End Try

但是,我收到一条错误消息:

未找到类型“JObject”的公共成员“错误”。

我该如何解决这个问题?

【问题讨论】:

    标签: json vb.net deserialization


    【解决方案1】:

    JObject class 没有 error 属性或方法。假设您的 JSON 看起来像这样:

    {
      "error": {
        "message": "...",
        "status": "...",
        "...": "..."
      },
      "...": "..."
    }
    

    然后你会使用:

    lblMyLabel.Text = bodyObj("error")("message").ToString()
    

    但是,您应该提供响应的示例,以便我提供确切的示例。

    【讨论】:

      猜你喜欢
      • 2021-03-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 2018-09-12
      • 2014-08-02
      相关资源
      最近更新 更多