Items von JSON-Datei (online) in Textbox ausgeben

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von nikexo.

    Items von JSON-Datei (online) in Textbox ausgeben

    Hallo!

    Folgendes Problem
    Ich rufe online eine JSON-Datei und will bestimmte Items aus der Datei in verschiedene Textboxen ausgeben.

    JSON-Datei

    XML-Quellcode

    1. {
    2. "items": [
    3. {
    4. "id": "YE7VzlLtp-4",
    5. "snippet": {
    6. "channelId": "UCSMOQeBJ2RAnuFungnQOxLg",
    7. "title": "Big Buck Bunny",
    8. "description": "Big Buck Bunny tells the story of a giant rabbit with a heart bigger than himself. When one sunny day three rodents rudely harass him, something snaps... and the rabbit ain't no bunny anymore! In the typical cartoon tradition he prepares the nasty rodents a comical revenge.\r\n\r\nLicensed under the Creative Commons Attribution license\r\n\r\nhttp://www.bigbuckbunny.org/",
    9. "tags": [
    10. "Animation",
    11. "CGI",
    12. "Big",
    13. "Buck",
    14. "Bunny",
    15. "Blender",
    16. "3D",
    17. "Creative",
    18. "Commons",
    19. "blender",
    20. "open",
    21. "movie",
    22. "content",
    23. "cartoon",
    24. "animated",
    25. "animation art"
    26. ]
    27. }
    28. }
    29. ]


    Was will ich jetzt genau?
    Ich will zum Beispiel alle Elemente von "tags" in eine Textbox (txtTags) ausgeben (ohne "" und mit Beistrich).
    Das gleiche mit "title" (txtTitle) und mit "description" (txtDesc).

    Also ich kann eigentlich nur die ganze JSON-Datei in einer Textbox via Button ausgeben:

    VB.NET-Quellcode

    1. Dim webClient As New System.Net.WebClient
    2. Dim getdata As String = webClient.DownloadString("https://www.googleapis.com/youtube/v3/videos?id=YE7VzlLtp-4&key=[APIKEY]&fields=items(id,snippet(channelId,title,description,tags),statistics)&part=snippet")
    3. RichTextBox1.Text = (getdata)

    nikexo schrieb:

    Ich will zum Beispiel alle Elemente von "tags" in eine Textbox (txtTags) ausgeben (ohne "" und mit Beistrich).Das gleiche mit "title" (txtTitle) und mit "description" (txtDesc).
    Vielleicht hast du meine Antwort in deinem andern Post einfach nicht gelesen (Schande über dich! :D) Aber hier hast du sie noch einmal:

    Radinator schrieb:

    Verwende das NuGet Paket oder genauer gesagt die Libary Json.NET von Newtonsoft. Bilde dir den Inhalt des Json Strings mit Klassen nach und mach ein Newtonsoft.Json.JsonConvert.DeserializeObject<DeineKlasse>(jsonString). Dann kannst du auch typensicher auf die Daten zugreifen


    Zur Erklärung
    Das Nachbilden des Json Strings mit Klassen ist so gemeint:
    1.) Wenn das JSON sowas wie

    XML-Quellcode

    1. key: [
    2. "value1",
    3. "value2",
    4. "value3"
    5. ]
    enthält, dann deutet das immer auf ein Array hin. Hierzu in der Klasse einfach einen Property anlegen, deren Typ irgendwo in der Vererbungshierarchie das Interface IEnumerable(Of T) implementiert. (z.B.: die Klasse List(Of T), kannst aber - soweit ich weiß - auch nur IEnumerable(Of T) schreiben. Der Parser macht das dann schon richtig.
    2.) Wenn da steht:

    XML-Quellcode

    1. "snippet": {
    2. "channelId": "UCSMOQeBJ2RAnuFungnQOxLg",
    3. "title": "Big Buck Bunny",
    dann bedeutet das, dass du hier eine Property mit der Bezeichnug "snippet" brauchst, die als Typen eine Klasse hat, die weitere Unter-Eigenschaften hat.
    Sowas wie

    VB.NET-Quellcode

    1. Public Class Snippet
    2. Public Property ChannelId As String
    3. Public Property Title As String
    4. '...
    5. End Class

    3.) Hier die Kombination aus beiden:

    XML-Quellcode

    1. "items": [
    2. {
    3. "id": "YE7VzlLtp-4",
    4. "snippet": {
    5. "channelId": "UCSMOQeBJ2RAnuFungnQOxLg",
    6. "title": "Big Buck Bunny",
    meint lediglich, dass hier die Root-Klasse eine Eigenschaft mit dem Namen items hat, wobei der Typ von items wieder eine Auflistung (Irgendwas mit IEnumerable) ist.

    VB.NET-Quellcode

    1. Public Class RootElement
    2. Public Property Items As IEnumerable(Of Item)
    3. End Class
    4. Public Class Item
    5. Public Property Id As Integer
    6. Public Property Snippet As Snippet
    7. '...
    8. End Class
    9. Public Class Snippet
    10. Public Property ChannelId As String
    11. '...
    12. Public Property Tags IEnumerable(Of String)
    13. End Class


    Ich gebe keine Garantie daruf, dass der Code geht. Ich hab den jetzt nur so on the fly im Browser geschrieben

    Ich hoff, ich konnte helfen
    Lg Radinator
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell