Avatar of Spike UK
Spike UKFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

How do I deserializea string to an object in vb?

I have a string being returned from a HttpWebRequest.
The string returns in the format:
"{""result"":{""version"":2,""name"":""StatusResult"",""database"":""ok"",""licenses"":""ok"",""internal_services"":""ok"",""reporting_service"":""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(Status))
            Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

            res = serviceRequest.DownloadString(New Uri(url))

            Dim string_reader As New StringReader(res)

            stat = xml_serializer.Deserialize(string_reader)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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?
Visual Basic.NETXML* serialization

Avatar of undefined
Last Comment
Spike UK
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

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.

Dim JSONObj = New JavaScriptSerializer().Deserialize(Of Dictionary(Of String, String))("JSON String")

Open in new window


Reference-
https://www.codementor.io/andrewbuchan/how-to-parse-json-into-a-c-object-4ui1o0bx8
Avatar of Kimputer
Kimputer

From NUGET, use this: Newtonsoft.Json;

Some code to help you along the way: https://www.codeproject.com/Articles/1201466/Working-with-JSON-in-Csharp-VB
Avatar of Spike UK
Spike UK
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Rikin Shah - what do I have to reference to Import  System.Web.Scrip?
Avatar of Spike UK
Spike UK
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Kimputer - JsonHelper.ToClass(Of Response(Of Category))(rawJson) is producing an error as it is not defined in the vb project "Simple".
I have reference Netonsoft.Json and updated the version so there is no longer an error.
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Spike UK
Spike UK
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

it_saige  nailed it with a complete solution to my issue.
Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo