在VB.NET中获取json值

时间:2017-11-30 17:02:53

标签: json vb.net key

如何在json文件中获取密钥的名称?我有一个json,我在VB.NET中解析,其中一个'字段'有一个动态名称(它改变)。我该怎么做才能获得密钥名称?

例如:

...

"one":{  
                        "two":{  
                           "example":[  
                              {  
                                 "aaa":"test",
                                 "bbb":"test",
                                 "ccc":"test"
                              },

...

我正确地获取了所有值(测试,测试,测试......),而且其中一个','两个'总是具有相同的名称。但关键的例子'根据json文件信息更改名称。我怎样才能识别关键文本?

3 个答案:

答案 0 :(得分:1)

我写了一段代码,将JSON转换为XDocument:https://github.com/dday9/.NET-JSON-Transformer

如果您要使用该代码,那么您可以获得代表您的“两个”对象的节点,然后获取第一个子节点。通过这样做,您实际上是通过索引而不是名称来获取数组。

以下是我的意思的快速示例:

Dim literal As String = "{""two"":{""example"":[{""aaa"":""test"",""bbb"":""test"",""ccc"":""test""}]}}"
Dim xJSON As XDocument = JSON.Parse(literal)
Dim object_two As XElement = xJSON.Descendants("two").FirstOrDefault()
If object_two IsNot Nothing Then
    Dim first_descendent As XElement = object_two.Descendants().Skip(1).FirstOrDefault()

    If first_descendent IsNot Nothing Then
        Console.WriteLine(first_descendent)
    End If
End If

小提琴:Live Demo

答案 1 :(得分:0)

我看到这已经解决了,但我想为未来的读者提出另一种解决方案。 JavaScriptSerializer可以返回嵌套的字典集合(Of String,Object)。我发现在编码时更容易在调试中探索结果。下面的代码显示了如何导航集合的示例。

  Dim deserializer As New System.Web.Script.Serialization.JavaScriptSerializer

  Dim text As String = "{""two"":{""example"":[{""aaa"":""test"",""bbb"":""test"",""ccc"":""test""}]}}"
  Dim dict As Dictionary(Of String, Object) = deserializer.DeserializeObject(text)

  Dim keys As Dictionary(Of String, Object).KeyCollection
  keys = dict("two")("example")(0).Keys

  Dim aaaName As String = keys(0)
  Dim aaaValue As String = dict("two")("example")(0)(aaaName)

答案 2 :(得分:0)

这部分将允许从未知的 JSON 结构中获取数据,而无需定义类。

样品

Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer
serializer = New JavaScriptSerializer()
' {"elements":[{"handle~":{"emailAddress":"myself@example.com"},"handle":"urn:li:emailAddress:348955221"}]}
dim json as string
Dim obj As System.Collections.Generic.IDictionary(Of String, Object)
obj = serializer.Deserialize(Of System.Collections.Generic.IDictionary(Of String, Object))(json)
dim email as string=string.empty
email = If(GetJsonValue(obj, {"elements", "handle~", "emailAddress"}.ToList()), email)

功能,非常自我描述:

''' <summary>decode json data </summary>
Public Function GetJsonValue(ByVal obj As Object,
                          ByVal key As List(Of String)) As String
   GetJsonValue = Nothing
    '   If the object is an array, assume any element can contain the key
    If obj.GetType Is GetType(Object()) Then 
        For Each newObj As Object In CType(obj, Object())
            Dim tmp As String = GetJsonValue(newObj, key)
            If Not String.IsNullOrEmpty(tmp) Then Return tmp
        Next
    Else
        Dim objEle As System.Collections.Generic.IDictionary(Of String, Object)
        Dim keyName As String
        Dim objKey As String
        '
        keyName = key(0)
        objEle = CType(obj, System.Collections.Generic.IDictionary(Of String, Object))
        objKey = objEle.Keys.ToArray()(0)

        If objEle.ContainsKey(keyName) Then
            Dim temp As Object = objEle.Item(keyName)
            If key.Count > 1 Then
                ' if the element is array, we need to get the array element and move to the next
                key.RemoveAt(0)
                Return GetJsonValue(temp, key)
            Else
                Return temp.ToString()
            End If
        End If
    End If

结束函数