我想获取json反序列化对象中的键
json看起来像:
{"key1":1,"key2":2,"key3":3}
我正在使用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)
现在我想获取j中现有密钥的列表。 我试过了:
Dim keys As List(Of String) = j.Properties().Select(Function(p) p.Name).ToList()
但它给了我" System.MissingMemberException:'公共成员'属性' on type' Dictionary(Of String,Object)'没找到。'"
答案 0 :(得分:0)
默认情况下,它会反序列化为Dictionary(Of String, Object)
对象,如错误消息所示。因此,您只需循环遍历字典条目列表:
For Each entry As KeyValuePair(Of String, Object) In j
Console.WriteLine("Key = " & entry.Key)
Console.WriteLine("Value = " & entry.Value)
Next
或者,如果您只需要密钥名称:
j.Select(Function(entry) entry.Key)