4

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.

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

Comments

0

The answer from Don Jayamanne already noted the fact that you need to use lists.

You also need to use an appropriate model (structure of classes) to make the JSON that you want. For example, you need to introduce new classes.

And also, you need to use the JsonProperty attribute to control the names used to generate the JSON.

Try this:

Class News
    <JsonProperty(propertyName:="title")>
    Public Property Title As String
    <JsonProperty(propertyName:="youtube_videos")>
    Public Property YoutubeVideos As YoutubeVideos = YoutubeVideos
End Class

Class NewsList
    <JsonProperty(propertyName:="data")>
    Public Items As List(Of News) = New List(Of News)()
End Class


Public Class YoutubeVideos
    <JsonProperty(propertyName:="video_identifier")>
    Public Property Items As List(Of YoutubeVideo) = New List(Of YoutubeVideo)
End Class

Public Class YoutubeVideo
    <JsonProperty(propertyName:="video_identifier")>
    Public Property VideoIdentifier As String
End Class

Sub Main()

    Dim newsItem = New News()
    newsItem.Title = "blah"

    Dim video1 = New YoutubeVideo()
    video1.VideoIdentifier = "id1"
    newsItem.YoutubeVideos.Items.Add(video1)

    Dim video2 = New YoutubeVideo()
    video2.VideoIdentifier = "id2"
    newsItem.YoutubeVideos.Items.Add(video2)

    Dim newsList = New NewsList()
    newsList.Items.Add(newsItem)
    Dim result = JsonConvert.SerializeObject(newsList)

End Sub

1 Comment

I will check this. Thanks you for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.