我正在使用Visual Studio 2010并在Visual Basic中进行编码。我有一个JSON文件,并且阅读了该文件,然后可以获得项目的名称和值。我无法得到的是另一个项目的值内的一个项目的名称和值。我可以获得“ Page-1.htm”以及括号“ Page-1.htm”中的所有内容,但是我不能仅获得“ title”或“ Safety”。我知道如果我知道item(“ title”)。Value在那里有“标题”,那么我可以得到“安全”,但是如您所见,有些项目只有标题,而有些却有数字,所以我无法通过这种方式获取信息。
这是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
试试这个:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句