如何将下面列出的C#代码转换为VB

时间:2016-05-17 13:20:24

标签: json.net c#-to-vb.net

我正在使用Json.net来序列化一个对象。特定对象是eventInstance.Properties,它是Windows事件日志的属性。

我正在接受

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 

对于C#,此处显示了一个示例

string json = JsonConvert.SerializeObject(joe, Formatting.Indented, new JsonSerializerSettings
 {
     ReferenceLoopHandling = ReferenceLoopHandling.Ignore
 });

我的代码行在下面,但我不知道如何使它在VB中工作或者甚至可能

command.Parameters.AddWithValue("@f18", JsonConvert.SerializeObject(eventInstance.Properties(), New JsonSerializerSettings() {ReferenceLoopHandling = ReferenceLoopHandling.Ignore}))

我收到错误消息,指出' ReferenceLoopHandling'是枚举类型,不能用作表达式

感谢您的帮助

1 个答案:

答案 0 :(得分:-1)

您可以使用以下代码:

    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