I have a string being returned from a HttpWebRequest.
The string returns in the format:
"{""result"":{""version"":
2,""name""
:""StatusR
esult"",""
database""
:""ok"",""
licenses""
:""ok"",""
internal_s
ervices"":
""ok"",""r
eporting_s
ervice"":"
"ok""}}"
I want to save this to an object by deserializing it.
I have created an object:
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~
Imports System.Xml.Serialization
<Serializable()>
Public Class Status
Public version As Integer
Public name As String
Public database As String
Public licenses As String
Public internal_services As String
Public reporting_service As String
' Empty constructor required for serialization.
Public Sub New()
End Sub
Public Sub New(ByVal new_version As Integer,
ByVal new_name As String,
ByVal new_database As String,
ByVal new_licenses As String,
ByVal new_internal_services As String,
ByVal new_reporting_service As String)
version = new_version
name = new_name
database = new_database
licenses = new_licenses
internal_services = new_internal_services
reporting_service = new_reporting_service
End Sub
End Class
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~
but receive an error when running the following:
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~~~~
url = String.Format("
http://127.0.0.1:8600/status/")
res = String.Empty
serviceRequest = New WebClient()
Dim stat As New Status(
version,
name,
database,
licenses,
internal_services,
reporting_service)
Dim xml_serializer As New XmlSerializer(GetType(Stat
us))
Dim request As HttpWebRequest = CType(WebRequest.Create(ur
l), HttpWebRequest)
res = serviceRequest.DownloadStr
ing(New Uri(url))
Dim string_reader As New StringReader(res)
stat = xml_serializer.Deserialize
(string_re
ader)
~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~
~~~~~~~~~~
~~
Error= "There is an error in XML document (1, 1)."
I'm presuming it's because of the xml tag "Result" but how do I deal with it?
The result you get from HttpWebRequest will be JSON and you need to convert it to object to use it in VB. You need to use JavaScriptSerializer class, which is part of the System.Web.Script namespace.
Open in new window
Reference-
https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8