使用Json.net(VB.NET)序列化嵌套JSON

时间:2015-10-19 11:30:02

标签: json vb.net serialization json.net

NET和我试图像这样创建一个JSON输出

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

与“视频”关联的

n“新闻”

到目前为止,这是我的代码:

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})

我不知道如何将所有视频添加到每个新的

任何帮助都会被贬低。谢谢你。

2 个答案:

答案 0 :(得分:3)

您的解决方案中缺少两件事(忽略解决方案中的拼写错误): - youtube_videos属性需要是数组或列表 - 数组可以是一个简单的对象数组(JSON.NET会为你序列化它)

试试这个,

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

答案 1 :(得分:0)

Don Jayamanne的回答已经注意到您需要使用列表。

您还需要使用适当的模型(类的结构)来生成所需的JSON。例如,您需要引入新类。

此外,您需要使用JsonProperty属性来控制用于生成JSON的名称。

试试这个:

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