我正在使用Visual Studio 2010并在Visual Basic中编码。 我有一个JSON文件,我通读它,我可以得到一个项目的名称和价值。 我无法得到的是另一个项目价值内的项目的名称和价值。 我可以获得“Page-1.htm”和“Page-1.htm”括号中的所有内容,但我不能只获得“标题”或“安全”。 我知道我可以获得“安全”,如果我知道“标题”是项目(“标题”)。值,但你可以看到一些项目将只有标题,一些有数字,所以我不能得到这样的信息。
这是JSON
{
"Page-1.htm":{
"title": "Safety",
"001": "1. Purpose",
"002": "2. Definitions"
},
"Page-2.htm":{
"title": "Testing",
"001": "Test first",
"002": "Test Again",
"003": "Final Test"
},
"Page-3.htm":{
"title": "Once Again"
}
}
这是我的VB的开始
Try
Dim reader = New StreamReader(jsonFile.ToString())
Dim rawresp As String = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList()
For Each item As JProperty in results
'This works and gives me nodes in my TreeView
Dim rootName As String = item.Name
Dim root As TreeNode = tvContent.Nodes.Add(rootName)
'This does not work to add child nodes because I can't get the info I need
'"inside value" would be "Safety" or "1. Purpose", or "2. etc" from the JSON
DIm childNode as TreeNode = tvContent.Nodes(0).Nodes.Add("inside value")
Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
答案 0 :(得分:1)
试试这个:
For Each child In item.Children
For Each value In child.Values
root.Nodes.Add(value.ToString)
Next
Next
要同时获取名称和值,您需要查看JProperties
。
For Each child In item.Children
For Each jProp As JProperty In child
root.Nodes.Add(String.Format("{0} = {1}", jProp.Name, jProp.Value))
'or simply
'root.Nodes.Add(jProp.ToString)
Next
Next
答案 1 :(得分:0)
怎么样
For Each item As JProperty in results
Dim rootName As String = item.Name
Dim root As TreeNode = tvContent.Nodes.Add(rootName)
dim sT as string = item.value("title")
if sT isnot nothing then dim childnode as treenode = tvcontent.nodes(0).add(sT)
dim s1 as string = item.value("001")
if s1 isnot nothing then: ' do whatever :endif
dim s2 as string = item.value("002")
if s2 isnot nothing then: ' do whatever : endif
'etc
Next
使用item.value(“ 001”)来指定您想要结果列表中当前'item'的键“ 001”的值
在这里指定一个或多个特定键,您将在所有可能性中循环找到先前的答案