使用 Json.net (VB.NET) 序列化嵌套的 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33213265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Serializing nested JSON with Json.net (VB.NET)
提问by Don Se?or
NET and im trying to create a JSON output like this
NET 并且我正在尝试创建这样的 JSON 输出
{
"data": [{
"title": "blah",
"youtube_videos": {
"video_identifier": [
{
"video_identifier": "id1"
},
"video_identifier": "id2"
}]
etc.
等等。
n "news" with n "videos" associated
n 个“新闻”与 n 个“视频”相关联
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.
任何帮助将不胜感激。谢谢。
回答by Don
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)
您的解决方案中缺少两件事(忽略解决方案中的拼写错误): - youtube_videos 属性需要是一个数组或列表 - 该数组可以是一个简单的对象数组(JSON.NET 将为您序列化它)
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
回答by Yacoub Massad
The answer from Don Jayamanne already noted the fact that you need to use lists.
Don Jayamanne 的回答已经指出您需要使用列表这一事实。
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.
您还需要使用适当的模型(类结构)来制作您想要的 JSON。例如,您需要引入新的类。
And also, you need to use the JsonPropertyattribute to control the names used to generate the JSON.
而且,您需要使用该JsonProperty属性来控制用于生成 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

