Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在VB.NET中获取json值_Json_Vb.net_Key - Fatal编程技术网

在VB.NET中获取json值

在VB.NET中获取json值,json,vb.net,key,Json,Vb.net,Key,如何在json文件中获取密钥的名称?我在VB.NET中解析一个json,其中一个“字段”有一个动态名称(它会更改)。我该怎么做才能得到密钥名 例如: 我正确地得到了所有的值(test、test、test…),并且键“one”、“two”始终具有相同的名称。但是键“example”会根据json文件信息更改名称。如何识别关键文本?我在这里编写了一段代码,将JSON转换为XDocument: 如果要使用该代码,则可以获取表示“两个”对象的节点,然后获取中的第一个子节点。通过这样做,您实际上是通过

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

例如:


我正确地得到了所有的值(test、test、test…),并且键“one”、“two”始终具有相同的名称。但是键“example”会根据json文件信息更改名称。如何识别关键文本?

我在这里编写了一段代码,将JSON转换为XDocument:

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

下面是我的意思的一个快速示例:

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

Fiddle:

我看到这个问题已经解决了,但我想为未来的读者推荐另一个解决方案。JavaScriptSerializer可以返回嵌套字典集合(字符串、对象)。我发现在编写代码时更容易在调试中探索结果。下面的代码显示了如何导航集合的示例

  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)

这一部分将允许从未知的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
“”“解码json数据
公共函数GetJsonValue(ByVal obj作为对象,
ByVal键作为列表(字符串的)作为字符串
GetJsonValue=Nothing
'如果对象是数组,则假定任何元素都可以包含键
如果obj.GetType是GetType(Object()),则
对于CType中的每个newObj As对象(obj,Object())
Dim tmp As String=GetJsonValue(newObj,key)
如果不是String.IsNullOrEmpty(tmp),则返回tmp
下一个
其他的
Dim对象作为System.Collections.Generic.IDictionary(字符串、对象)
Dim键名作为字符串
Dim objKey作为字符串
'
keyName=key(0)
objEle=CType(obj,System.Collections.Generic.IDictionary(字符串,对象))
objKey=objEle.Keys.ToArray()(0)
如果objEle.ContainsKey(键名),则
作为对象的Dim temp=对象项(键名)
如果key.Count>1,则
'如果元素是数组,我们需要获取数组元素并移动到下一个
键。移除(0)
返回GetJsonValue(临时值,键)
其他的
返回温度ToString()
如果结束
如果结束
如果结束

结束功能

通常情况下,变化是由您的请求引起的,因此您可能可以根据该请求采取行动。否则,解析它,看看它是什么它看起来很好!我是VB.NET中的乞丐。如何在主vb文件中调用模块函数?我已经用母版页创建了一个aspx文件(就像我项目中的所有其他文件一样),在后端(.vb文件),我想使用模块函数(JSON.Parse(literal)…)。模块保存在一个文件夹中,我的文件保存在同一个文件夹中。@Developer1903-老实说,我不确定。我从未在VB.NET中开发过任何web应用程序,只有Windows窗体应用程序。如果它与win form应用程序中的相同,那么您需要做的就是:在解决方案资源管理器中右键单击项目,单击添加现有项,然后选择文件。
''' <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