从json输出中键值对的值捕获字符串列表VB .NET

时间:2021-12-20 19:28:22

I have a valid POST-request response converted to json via

我有一个有效的POST请求响应转换为json via

Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, object))(responseFromServer)

While parsing the dictionary, two level inside I find a 'list' of strings as below

解析字典时,我在里面找到两个字符串的“列表”,如下所示

Dim levelOne = jsonResulttodict.item ("level_one")
Dim levelTwo = levelOne.item ("level_two")

On mouse-hover, levelTwo shows up as:

在鼠标悬停时,levelTwo显示为:

{["value-3", "value-2", "value-1"]}

and Console.Writeline(levelTwo.ToString()) results in:

和Console.Writeline(levelTwo.ToString())导致:

[ "value-3", "value-2", "value-1" ]

It sounds silly but I'm simply unable to capture this into a variable (a list of strings).

这听起来很愚蠢,但我根本无法将其捕获到变量(字符串列表)中。

I've already tried these, but neither works:

我已经尝试了这些,但都不起作用:

Dim mylist As List(Of String) = New List(Of String)({levelOne.item("level_two")})

and

For Each pair As KeyValuePair(Of String, List(Of String)) In levelTwo
            Console.WriteLine(pair.Key)
            Console.Write("  ")
            Console.WriteLine(pair.Value)
        Next

1 个解决方案

#1


0  

On further inspection, I printed out the type of the variable.

在进一步检查时,我打印出变量的类型。

Console.WriteLine(levelTwo.GetType())

I got it as Newtonsoft.Json.Linq.JArray. Which now makes everything seem so trivial.
I'm now able to iterate over the JArray as follows

我得到了它作为Newtonsoft.Json.Linq.JArray。现在,这一切似乎都变得如此微不足道。我现在可以按如下方式迭代JArray

for each value as String In levelTwo
    Console.WriteLine(value.ToString())
Next

Prints as follows:

打印如下:

value-3
value-2
value-1

#1


0  

On further inspection, I printed out the type of the variable.

在进一步检查时,我打印出变量的类型。

Console.WriteLine(levelTwo.GetType())

I got it as Newtonsoft.Json.Linq.JArray. Which now makes everything seem so trivial.
I'm now able to iterate over the JArray as follows

我得到了它作为Newtonsoft.Json.Linq.JArray。现在,这一切似乎都变得如此微不足道。我现在可以按如下方式迭代JArray

for each value as String In levelTwo
    Console.WriteLine(value.ToString())
Next

Prints as follows:

打印如下:

value-3
value-2
value-1