Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing nested JSON with Json.net (VB.NET)

NET and im trying to create a JSON output like this

{
"data": [{
"title": "blah",
"youtube_videos": {
"video_identifier": [
             {
         "video_identifier": "id1"
             },
         "video_identifier": "id2"     
           }]

etc.

n "news" with n "videos" associated

And this is my code so far:

Class News
    Public Property title() As String
        Get
            Return _title
        End Get
        Set(value As String)
            _title = value
        End Set
    End Property
    Private _title As String


    Public Property id() As String
        Get
            Return _sId
        End Get
        Set(value As String)
            _sId = value
        End Set
    End Property

    Private _youtube_videos As YoutubeVideos = New YoutubeVideos ()
    Public Property youtube_videos As YoutubeVideos 
        Get
            Return _youtube_videos
        End Get
        Set(ByVal value As YoutubeVideos)
            _youtube_videos = value
        End Set
    End Property
Ënd Class

Public Class YoutubeVideos
    Private _video_identifier As String
    Public Property video_identifier() As String
        Get
            Return _video_identifier
        End Get
        Set(ByVal value As String)
            _video_identifier = value
        End Set
    End Property


End Class

...

Private Function getJSON(ByVal sJSON As String) As String

Dim objNews As New List(Of Noticia)
Dim objVideos As New List(Of YoutubeVideos)

Dim objItem As New News
objItem.title = "blah blah" 

objNews .Add(objItem)

???

Return Newtonsoft.Json.JsonConvert.SerializeObject(New With {Key .data = objNews})

I dont know how to cointinue to add all the videos to each new

Any help would be apreciated. Thanks you.

like image 666
Don Señor Avatar asked Sep 06 '25 16:09

Don Señor


1 Answers

There are two things missing in your solution (ignoring the typos in your solution): - The youtube_videos property needs to be an array or a list - The array can be a simple array of objects (JSON.NET will serialize it for you)

Try this,

Private Function getJSON(sJSON As String) As String
    Dim objNews = New List(Of News)()
    Dim news = New News()
    news.id = ""
    news.title = "blah"
    Dim lst = New List(Of Object)()
    lst.Add(New With {.video_identifier = "id1"})
    lst.Add(New With {.video_identifier = "id2"})
    news.video_identifier = lst.ToArray()
    objNews.Add(news)
    Return Newtonsoft.Json.JsonConvert.SerializeObject(New With {.data = objNews})
End Function

Class News

    Public Property title As String
        Get
            Return _title
        End Get

        Set
            _title = value
        End Set
    End Property

    Private _title As String

    Private _sId As String

    Public Property id As String
        Get
            Return _sId
        End Get

        Set
            _sId = value
        End Set
    End Property

    Private _youtube_videos As Object() = New List(Of Object)().ToArray()

    Public Property video_identifier As Object()
        Get
            Return _youtube_videos
        End Get

        Set
            _youtube_videos = value
        End Set
    End Property
End Class

Public Class YoutubeVideos

    Private _video_identifier As String

    Public Property video_identifier As String
        Get
            Return _video_identifier
        End Get

        Set
            _video_identifier = value
        End Set
    End Property
End Class
like image 108
Don Avatar answered Sep 09 '25 10:09

Don