Json mit mehrdimensionalen Arrays parsen

  • VB.NET
  • .NET (FX) 3.0–3.5

Es gibt 7 Antworten in diesem Thema. Der letzte Beitrag () ist von simpelSoft.

    Json mit mehrdimensionalen Arrays parsen

    Ich beschäftige mich jetzt schon seit mehreren Tagen mit der Json API von Newtonsoft und Ich werde allmählich verrückt.

    Ich möchte dieses mehrdimensionale Array parsen

    Quellcode

    1. {
    2. "19346232": [
    3. {
    4. "name": "Master Yi's Pyromancers",
    5. "tier": "GOLD",
    6. "queue": "RANKED_SOLO_5x5",
    7. "entries": [
    8. {
    9. "playerOrTeamId": "19346232",
    10. "playerOrTeamName": "Wamborambo",
    11. "division": "V",
    12. "leaguePoints": 58,
    13. "wins": 109,
    14. "losses": 104,
    15. "isHotStreak": false,
    16. "isVeteran": false,
    17. "isFreshBlood": false,
    18. "isInactive": false
    19. }
    20. ]
    21. },
    22. {
    23. "name": "Elise's Masterminds",
    24. "tier": "BRONZE",
    25. "queue": "RANKED_TEAM_3x3",
    26. "entries": [
    27. {
    28. "playerOrTeamId": "TEAM-4f457680-9f2b-11e4-8954-c81f66db8bc5",
    29. "playerOrTeamName": "KidGalahad",
    30. "division": "IV",
    31. "leaguePoints": 63,
    32. "wins": 6,
    33. "losses": 8,
    34. "isHotStreak": false,
    35. "isVeteran": false,
    36. "isFreshBlood": false,
    37. "isInactive": false
    38. }
    39. ]
    40. },
    41. {
    42. "name": "Elise's Horde",
    43. "tier": "BRONZE",
    44. "queue": "RANKED_TEAM_5x5",
    45. "entries": [
    46. {
    47. "playerOrTeamId": "TEAM-4f457680-9f2b-11e4-8954-c81f66db8bc5",
    48. "playerOrTeamName": "KidGalahad",
    49. "division": "I",
    50. "leaguePoints": 28,
    51. "wins": 6,
    52. "losses": 4,
    53. "isHotStreak": false,
    54. "isVeteran": false,
    55. "isFreshBlood": false,
    56. "isInactive": false
    57. }
    58. ]
    59. }
    60. ]
    61. }


    Ich hatte zuerst das Problem das "19346232" dynamisch ist. Durch etwas einlesen und Beispielen konnte ich das umsetzen.

    VB.NET-Quellcode

    1. Dim jsonSource As String = reader.ReadToEnd
    2. Dim dynamicObj = Newtonsoft.Json.JsonConvert.DeserializeObject(Of JObject)(jsonSource)
    3. For Each prop As JProperty In dynamicObj.Properties()
    4. For Each subObj As JObject In prop.Values(0)
    5. liganameSolo = subObj("name") '5vs5 Division Name
    6. rangSolo = subObj("tier")
    7. Next
    8. Next

    Mein Problem ist es jetzt das Array "entries" anzusprechen.

    Ich hab das ganze mal soweit versucht aber Ich komme leider nicht mehr weiter.
    Fehlermeldung
    Accessed JArray values with invalid key value: "wins". Array position index expected.

    VB.NET-Quellcode

    1. Dim premiers = dynamicObj.Properties().First().Value()
    2. For Each subObj As JObject In premiers
    3. winsSolo = subObj("entries")("wins")
    4. Next

    Jag doch mal Dein JSON durch diesen online-Klassengenerator: jsonutils.com
    Dort kannst Du Dir die erstelle Klasse in C# oder VB.NET ansehen und auch im Viewer testen.
    Sehr nützlich, wenn man die Strukturen eines JSON braucht.

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Entry
    2. Public Property playerOrTeamId As String
    3. Public Property playerOrTeamName As String
    4. Public Property division As String
    5. Public Property leaguePoints As Integer
    6. Public Property wins As Integer
    7. Public Property losses As Integer
    8. Public Property isHotStreak As Boolean
    9. Public Property isVeteran As Boolean
    10. Public Property isFreshBlood As Boolean
    11. Public Property isInactive As Boolean
    12. End Class
    13. Public Class 19346232
    14. Public Property name As String
    15. Public Property tier As String
    16. Public Property queue As String
    17. Public Property entries As Entry()
    18. End Class
    19. Public Class Example
    20. Public Property 19346232 As 19346232()
    21. End Class

    das problem ist meine Public Class "19346232" ist dynamisch. die kann also auch anders heißen.
    Ich weiß leider nicht wie ich das umgehen kann. ansonsten könnte man das sehr leicht aufrufen mit den propertys.(so hatte ich das am Anfang mit leichteren Jsons gemacht.

    simpelSoft schrieb:

    Sehr nützlich, wenn man
    numerische Ausdrücke als Klassennamen verwendet.
    Mach da einen Unterstrich davor, da versteht es sogar der Compiler.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    simpelSoft schrieb:

    Erzähl das jsonutils.com
    Warum?
    json2csharp.com/ liefert dies:
    Spoiler anzeigen

    C#-Quellcode

    1. public class Entry
    2. {
    3. public string playerOrTeamId { get; set; }
    4. public string playerOrTeamName { get; set; }
    5. public string division { get; set; }
    6. public int leaguePoints { get; set; }
    7. public int wins { get; set; }
    8. public int losses { get; set; }
    9. public bool isHotStreak { get; set; }
    10. public bool isVeteran { get; set; }
    11. public bool isFreshBlood { get; set; }
    12. public bool isInactive { get; set; }
    13. }
    14. public class __invalid_type__19346232
    15. {
    16. public string name { get; set; }
    17. public string tier { get; set; }
    18. public string queue { get; set; }
    19. public List<Entry> entries { get; set; }
    20. }
    21. public class RootObject
    22. {
    23. public List<__invalid_type__19346232> __invalid_name__19346232 { get; set; }
    24. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!