0

I'm trying to catch the error message from a WebRequest that returns a 400 Bad Request message when the user enters bad info. I want to display the message to the screen and according to this, I should be able to deserialize the string containing the JSON and then access the error message like this:

    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

However, I'm getting an error message saying:

Public member 'error' on type 'JObject' not found.

How can I correct this issue?

TheIronCheek
  • 1,077
  • 2
  • 20
  • 50

1 Answers1

2

The JObject class does not have an error property or method. Assuming that your JSON looks something like this:

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

Then you would use:

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

However, you should provide an example of what the response looks like so that I can provide an exact example.

David
  • 5,877
  • 3
  • 23
  • 40