首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在VB.NET中获取json值

在VB.NET中获取json值
EN

Stack Overflow用户
提问于 2017-11-30 17:02:52
回答 3查看 1.9K关注 0票数 0

如何在json文件中获得密钥的名称?我在VB.NET中解析一个json,其中一个“字段”有一个动态名称(它会发生变化)。我能做些什么才能弄到钥匙的名字?

例如:

..。

代码语言:javascript
运行
复制
"one":{  
                        "two":{  
                           "example":[  
                              {  
                                 "aaa":"test",
                                 "bbb":"test",
                                 "ccc":"test"
                              },

..。

我得到了所有的值(测试,测试,测试.)而键‘1’,‘2’,总是有相同的名字。但是键‘示例’根据json文件信息更改名称。我怎样才能识别关键的文本?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-12-01 04:17:34

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

代码语言:javascript
运行
复制
  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)
票数 0
EN

Stack Overflow用户

发布于 2017-11-30 17:31:10

我在这里编写了一段将JSON转换为XDocument的代码:https://github.com/dday9/.NET-JSON-Transformer

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

下面是我的意思的一个简单例子:

代码语言:javascript
运行
复制
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

小提琴:现场演示

票数 1
EN

Stack Overflow用户

发布于 2021-03-05 16:59:27

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

示例

代码语言:javascript
运行
复制
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)

功能,非常自我描述:

代码语言:javascript
运行
复制
''' <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

端函数

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47578817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档