JSON parsen / Treeview oder DataTable erstellen

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von Morrison.

    JSON parsen / Treeview oder DataTable erstellen

    Hallo!

    Ich bekomme über ein WebRequest einen String im JSON-Format zurück!

    Ich habe mir jetzt schon eine Klasse mit Properties erstellt um auf die einzelnen Werte zuzugreifen.

    Wie kann ich mir jetzt aus dem String ein Treeview erstellen, oder gar ein DataTable?

    Der JSON-String hat mehrere Bereiche, hier mal die Klasse:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Artist
    2. Public Property id As String
    3. Public Property name As String
    4. Public Property joinphrase As String
    5. End Class
    6. Public Class Track
    7. Public Property position As Integer
    8. Public Property artists As Artist()
    9. Public Property id As String
    10. Public Property title As String
    11. End Class
    12. Public Class Medium
    13. Public Property position As Integer
    14. Public Property tracks As Track()
    15. Public Property track_count As Integer
    16. Public Property format As String
    17. Public Property title As String
    18. End Class
    19. Public Class Release
    20. Public Property mediums As Medium()
    21. Public Property id As String
    22. End Class
    23. Public Class Releasegroup
    24. Public Property id As String
    25. Public Property releases As Release()
    26. End Class
    27. Public Class Recording
    28. Public Property sources As Integer
    29. Public Property releasegroups As Releasegroup()
    30. Public Property id As String
    31. End Class
    32. Public Class Result
    33. Public Property recordings As Recording()
    34. Public Property score As Double
    35. Public Property id As String
    36. End Class
    37. Public Class c_JSON_ROOT
    38. Public Property status As String
    39. Public Property results As Result()
    40. End Class



    Newtonsoft.JSON hab ich installiert!
    Hallo @Morrison

    Ich mach das immer mit

    VB.NET-Quellcode

    1. .FromJson
    2. .ToJson


    Also zum Beispiel

    VB.NET-Quellcode

    1. Dim sDict As String = dict.ToJson()
    2. dict = s.FromJson(Of Dictionary(Of String, List(Of String)))()


    geht eventuell auch für das Dataset oder Treeview

    Freundliche Grüsse


    exc-jdbi
    Hab jetzt eine Funktion von C# konvertiert die eigentlich schon gut funktioniert!

    Werde sie noch weiter testen, wenn jemand Verbesserungen oder ähnliches hat, gerne her damit!

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Shared Function Json2Tree(obj As JObject) As TreeNode
    2. 'create the parent node
    3. Dim parent As New TreeNode()
    4. 'loop through the obj. all token should be pair<key, value>
    5. For Each token In obj
    6. 'change the display Content of the parent
    7. parent.Text = token.Key.ToString()
    8. 'create the child node
    9. Dim child As New TreeNode()
    10. child.Text = token.Key.ToString()
    11. 'check if the value is of type obj recall the method
    12. If token.Value.Type.ToString() = "Object" Then
    13. ' child.Text = token.Key.ToString();
    14. 'create a new JObject using the the Token.value
    15. Dim o As JObject = DirectCast(token.Value, JObject)
    16. 'recall the method
    17. child = Json2Tree(o)
    18. 'add the child to the parentNode
    19. parent.Nodes.Add(child)
    20. 'if type is of array
    21. ElseIf token.Value.Type.ToString() = "Array" Then
    22. Dim ix As Integer = -1
    23. ' child.Text = token.Key.ToString();
    24. 'loop though the array
    25. For Each itm In token.Value
    26. 'check if value is an Array of objects
    27. If itm.Type.ToString() = "Object" Then
    28. Dim objTN As New TreeNode()
    29. 'child.Text = token.Key.ToString();
    30. 'call back the method
    31. ix += 1
    32. Dim o As JObject = DirectCast(itm, JObject)
    33. objTN = Json2Tree(o)
    34. objTN.Text = token.Key.ToString() + "[" + ix.ToString + "]"
    35. 'parent.Nodes.Add(child);
    36. child.Nodes.Add(objTN)
    37. 'regular array string, int, etc
    38. ElseIf itm.Type.ToString() = "Array" Then
    39. ix += 1
    40. Dim dataArray As New TreeNode()
    41. For Each _Data In itm
    42. dataArray.Text = token.Key.ToString() + "[" + ix.ToString + "]"
    43. dataArray.Nodes.Add(_Data.ToString())
    44. Next
    45. child.Nodes.Add(dataArray)
    46. Else
    47. child.Nodes.Add(itm.ToString())
    48. End If
    49. Next
    50. parent.Nodes.Add(child)
    51. Else
    52. 'if token.Value is not nested
    53. ' child.Text = token.Key.ToString();
    54. 'change the value into N/A if value == null or an empty string
    55. If token.Value.ToString() = "" Then
    56. child.Nodes.Add("N/A")
    57. Else
    58. child.Nodes.Add(token.Value.ToString())
    59. End If
    60. parent.Nodes.Add(child)
    61. End If
    62. Next
    63. Return parent
    64. End Function


    ​Ach ja, aufgerufen wird das ganze dann so:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Dim obj As JObject = JObject.Parse(responseFromServer)
    2. Form1.tvw_display.Nodes.Clear()
    3. Dim parent As TreeNode = Json2Tree(obj)
    4. parent.Text = "Root Object"
    5. Form1.tvw_display.Nodes.Add(parent)

    Hier noch der JSON-Response:

    Spoiler anzeigen

    XML-Quellcode

    1. ​{
    2. "status":"ok",
    3. "results":[
    4. {
    5. "recordings":[
    6. {
    7. "sources":7,
    8. "releasegroups":[
    9. {
    10. "id":"86bb879a-7df3-3269-a7fb-c6695350d51f",
    11. "releases":[
    12. {
    13. "mediums":[
    14. {
    15. "position":1,
    16. "tracks":[
    17. {
    18. "position":5,
    19. "artists":[
    20. {
    21. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    22. "name":"Den Harrow"
    23. }
    24. ],
    25. "id":"d7abb208-8b51-3f66-9851-ba8d6fd3b768",
    26. "title":"Catch the Fox"
    27. }
    28. ],
    29. "track_count":19,
    30. "format":"CD"
    31. }
    32. ],
    33. "id":"6303dcd2-811f-42f6-b52a-4de9052cf0e3"
    34. }
    35. ]
    36. }
    37. ],
    38. "id":"2c760729-c0bf-4ae9-90a4-6714cb030432"
    39. },
    40. {
    41. "sources":2,
    42. "releasegroups":[
    43. {
    44. "id":"bf20f861-6586-3025-a96b-b861c79bcbc5",
    45. "releases":[
    46. {
    47. "mediums":[
    48. {
    49. "position":1,
    50. "tracks":[
    51. {
    52. "position":9,
    53. "artists":[
    54. {
    55. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    56. "name":"Den Harrow"
    57. }
    58. ],
    59. "id":"8a167748-3fae-3a42-891f-01a360a870b9",
    60. "title":"Catch the Fox"
    61. }
    62. ],
    63. "track_count":13,
    64. "format":"CD"
    65. }
    66. ],
    67. "id":"dcd7e9f4-c1a9-479d-b369-7505284464c4"
    68. }
    69. ]
    70. }
    71. ],
    72. "id":"4f1a0951-cf6a-46d6-8b04-33514399d0dc"
    73. },
    74. {
    75. "sources":3,
    76. "releasegroups":[
    77. {
    78. "id":"d1489a3d-8f75-3dd8-a0ec-a79bcbd3a3d4",
    79. "releases":[
    80. {
    81. "mediums":[
    82. {
    83. "position":1,
    84. "tracks":[
    85. {
    86. "position":3,
    87. "artists":[
    88. {
    89. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    90. "name":"Den Harrow"
    91. }
    92. ],
    93. "id":"cba1fc30-72da-3bfb-8119-f677d700d6de",
    94. "title":"Catch the Fox"
    95. }
    96. ],
    97. "track_count":12,
    98. "format":"CD"
    99. }
    100. ],
    101. "id":"a89d9ac2-7ec4-4ec9-bbcc-40d0dff78703"
    102. }
    103. ]
    104. }
    105. ],
    106. "id":"636b1f9f-4452-40ce-975b-45dab86afbcc"
    107. },
    108. {
    109. "sources":3,
    110. "releasegroups":[
    111. {
    112. "id":"91821cbf-600f-37e6-88f1-f417ef25e69c",
    113. "releases":[
    114. {
    115. "mediums":[
    116. {
    117. "position":1,
    118. "tracks":[
    119. {
    120. "position":8,
    121. "artists":[
    122. {
    123. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    124. "name":"Den Harrow"
    125. }
    126. ],
    127. "id":"705d1d78-94e3-3ea8-819e-b93e943b457e",
    128. "title":"Catch the Fox"
    129. }
    130. ],
    131. "track_count":17
    132. }
    133. ],
    134. "id":"06bc042c-fdf8-4830-a72d-af3b231aa58b"
    135. }
    136. ]
    137. }
    138. ],
    139. "id":"94059c84-fee9-4688-ad6a-b6e50877dadc"
    140. },
    141. {
    142. "sources":17,
    143. "releasegroups":[
    144. {
    145. "id":"39482788-bb66-3adc-a648-be1dea99d9f4",
    146. "releases":[
    147. {
    148. "mediums":[
    149. {
    150. "position":1,
    151. "tracks":[
    152. {
    153. "position":9,
    154. "artists":[
    155. {
    156. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    157. "name":"Den Harrow"
    158. }
    159. ],
    160. "id":"9a0b834f-9a54-3043-afcb-666a781d3188",
    161. "title":"Catch the Fox (Caccia Alla Volpe)"
    162. }
    163. ],
    164. "track_count":13,
    165. "format":"CD"
    166. }
    167. ],
    168. "id":"396c28e3-f860-4d27-b172-025f3f27e32d"
    169. }
    170. ]
    171. }
    172. ],
    173. "id":"9f48c983-6c78-4818-aa4e-c1c6f4eacd80"
    174. },
    175. {
    176. "sources":1,
    177. "releasegroups":[
    178. {
    179. "id":"e66792c2-c1f8-338f-9a19-d5aa9e09b965",
    180. "releases":[
    181. {
    182. "mediums":[
    183. {
    184. "position":1,
    185. "tracks":[
    186. {
    187. "position":2,
    188. "artists":[
    189. {
    190. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    191. "name":"Den Harrow"
    192. }
    193. ],
    194. "id":"9635acb3-51ca-307d-ab75-b24606e960c4",
    195. "title":"Catch the Fox"
    196. }
    197. ],
    198. "track_count":12,
    199. "format":"CD"
    200. }
    201. ],
    202. "id":"84ada4b0-15da-452a-b71e-0f1f6f012737"
    203. },
    204. {
    205. "mediums":[
    206. {
    207. "position":1,
    208. "tracks":[
    209. {
    210. "position":2,
    211. "artists":[
    212. {
    213. "id":"107e04ef-f7c0-4f0c-984e-9ff244ab5566",
    214. "name":"Den Harrow"
    215. }
    216. ],
    217. "id":"5ce55c26-86b6-4a98-a831-31364f003a53",
    218. "title":"Catch the Fox"
    219. }
    220. ],
    221. "track_count":14,
    222. "format":"CD"
    223. }
    224. ],
    225. "id":"25562f7f-d5bf-4292-92d2-1f91fe1d9afb"
    226. }
    227. ]
    228. }
    229. ],
    230. "id":"ea3abbbf-58bc-4cef-a07a-623f42e2ea93"
    231. }
    232. ],
    233. "score":0.938225,
    234. "id":"6dbf49b2-7dc7-44d5-8625-b44d8cc5b8b8"
    235. }
    236. ]
    237. }