0

I am using VB.Net and calling salesforce API. It returns very ugly JSON which I am not able to deserialize. I have a following code using JSON.Net

Dim objDescription As Object = JsonConvert.DeserializeObject(Of Object)(result)

objDescription contains many properties, one on=f them in fields. But when I write something like objDescription.fields it gives me error.

  objDescription.fields   Public member 'fields' on type 'JObject' not found. Object

I am not very sure but I think it C# allow to convert any JSON to dynamic object. How can I use it in VB.Net?

3
  • objDescription is of Object type and Object types do not have "fields" property. Maybe you can create it as a strongly type object of your target class Commented Apr 21, 2018 at 6:12
  • @F0r3v3r-A-N00b I really want to do it but I think there has to a way because this JSon has very lengthy list of properties, array etc...... Commented Apr 21, 2018 at 6:16
  • Then don't convert it into an object and retain it as JSON object and access the properties like object.GetValue("name of property") and cast it to whatever data type that you expect Commented Apr 21, 2018 at 6:43

1 Answer 1

3

You can turn Option Strict Off and use ExpandoObject which is recognized by JSON.NET. In order to leverage the dynamic features you can use a variable of type object.

Option Strict Off

Sub Main    
    Dim jsonData As Object = JsonConvert.DeserializeObject(Of System.Dynamic.ExpandoObject)("{""Id"":25}")
    Dim test As Integer = jsonData.Id
    Console.WriteLine(test)
End Sub

If you would like to use JObject because you need some of its features, you can index the JObject instead.

Sub Main
    Dim jsonData As Object = JsonConvert.DeserializeObject(Of Object)("{""Id"":25}")
    Dim test = jsonData("Id")
    Console.WriteLine(test)
End Sub
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.