1

I want to get the keys present in a json deserialized object

The json looks like :

{"key1":1,"key2":2,"key3":3}

I'm using JavaScriptSerializer :

Dim jsonStr As String = "{""key1"":1,""key2"":2,""key3"":3}"

Dim j As Object = New JavaScriptSerializer().Deserialize(Of Object)(jsonStr)

Dim jQty As Integer = j.Count 'Count key/value pairs (Return 3)

Now I want to get the list of existing keys in j. I tried :

Dim keys As List(Of String) = j.Properties().Select(Function(p) p.Name).ToList()

But it gave me "System.MissingMemberException: 'Public member 'Properties' on type 'Dictionary(Of String,Object)' not found.'"

Tr Fntn
  • 15
  • 1
  • 6

1 Answers1

0

By default, it is deserializing into a Dictionary(Of String, Object) object, as the error message says. Therefore, you just need to loop through the list of dictionary entries:

For Each entry As KeyValuePair(Of String, Object) In j
    Console.WriteLine("Key = " & entry.Key)
    Console.WriteLine("Value = " & entry.Value)
Next

Or, if you just need the key names:

j.Select(Function(entry) entry.Key)
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105