1

I'm trying to use JavaScriptSerializer to parse some json string to create objects. I'm thinking about implementing something like this but no luck on my end...

Dim object As New SomeObject
Dim js As New JavaScriptSerializer
someObject = js.Deserialize(Of Somebject)(jsonstr)
object1.var1 = someObject("somekey")

where jsonstr is data in json format. I can't use any third party library like newtonsoft.json etc.

edit: I made some progress, given my class e.g. has property "name" and my json has key "name" as well it will pass on the string to objects property with whatever value the json file has.

Dim object As SomeObject = js.Deserialize(Of SomeObject)(jsonstr)
Dim name As String = object.name //object now holds value for name

but if my json is like this. How can I get name in the following?

  "value" : {
  "validationFactors" : [
     {
        "name" : "remote_address",
        "value" : "127.0.0.1"
     }
  ]

}

If I call Deserialize to get name for e.g I get MissingMethodException Do I need to change my dummy object and if so how? Thanks

5
  • 1
    What does no luck on my end mean? is there an error message? Does all or part of it not deserialize? no luck is a feeble problem description Commented Jul 21, 2017 at 18:06
  • 1
    You could use System.Web.Script.Serialization.JavaScriptSerializer which is in the .NET framework, but even Microsoft suggests using newtonsoft on that MSDN page. It's fairly simple to include it in your project with NuGet. Why is there such a restriction I can't use any third party library? Commented Jul 21, 2017 at 18:16
  • @Plutonix please see edit, I realize my first post was not very detailed. my apologies Commented Jul 25, 2017 at 15:15
  • 1
    Visual Studio will create the classes for you. Edit Menu -> Paste Special -> Paste Json As Classes Commented Jul 25, 2017 at 15:25
  • @Plutonix thank you so much. It didn't do it perfectly at first because some objects were lists but after declaring them as list properties in the vb file everything works great! Commented Jul 25, 2017 at 21:01

1 Answer 1

1

Here's an example that works:

Sub Main
    Dim js As New JavaScriptSerializer()
    Dim someObject As SomeObject = js.Deserialize(Of SomeObject)("{""A"":""Hello"",""B"":42}")
    Console.WriteLine("A:={0}, B:={1}", someObject.A, someObject.B)
End Sub

Class SomeObject
    Public Property A As String
    Public Property B As Integer
End Class

Output:

A:=Hello, B:=42

However, it looks like you are trying to access someObject like a dictionary, and you could do that with:

Dim someObject As Dictionary(Of String, Object) = js.Deserialize(Of Dictionary(Of String, Object))("{""A"":""Hello"",""B"":42}")
Console.WriteLine("A:={0}, B:={1}", someObject("A"), someObject("B"))

Same output as before.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.