首页 > 解决方案 > .NET JSON 序列化 - 空对象

问题描述

我已经使用 Newtonsoft.JSON 序列化我的 JSON,当它被序列化时,我在 Json 中得到空的“地址”对象 - 这在尝试针对我必须发送到的 Web 服务验证它时会导致问题。

我怎样才能让它忽略“空”对象,这样它们就不会输出到 JSON 中。我尝试过使用 DefaultValue 和 Null Ignore。

Public Class Person
    <DefaultValue("")> <JsonProperty("person_id")>
    Public Property person_id As String = Nothing
    <DefaultValue("")> <JsonProperty("email")>
    Public Property email As String = Nothing
    <DefaultValue("")> <JsonProperty("mobile")>
    Public Property mobile As String = Nothing
    <DefaultValue("")> <JsonProperty("forename")>
    Public Property forename As String = Nothing
    <DefaultValue("")> <JsonProperty("surname")>
    Public Property surname As String = Nothing
    <DefaultValue("")> <JsonProperty("address")>
    Public Property address As New PersonAddress()
    <DefaultValue("")> <JsonProperty("dob")>
    Public Property dob As String = Nothing
    <DefaultValue("")> <JsonProperty("driver_details")>
    Public Property driver_details As New Driver_Details()
    Public Property roles As List(Of String)
    Public Property documents_required As List(Of String)

End Class

Public Class PersonAddress
    <DefaultValue("")> <JsonProperty("line_1")>
    Public Property line_1 As String = Nothing
    <DefaultValue("")> <JsonProperty("town_city")>
    Public Property town_city As String = Nothing
    <DefaultValue("")> <JsonProperty("postal_code")>
    Public Property postal_code As String = Nothing
    <DefaultValue("")> <JsonProperty("country")>
    Public Property country As String = Nothing
End Class

Private Function get_persons(ByVal branch As String, ByVal policyref As String) As List(Of Person)

        Dim persons_dr = dt_driverdetails.Select("[B@] = '" & branch & "' and [PolRef@] = '" & policyref & "'")
        Dim objpeople As New List(Of Person)

        For Each row As DataRow In persons_dr

            'This code exists for testing purposes
            Dim mobile As String = row("mobile").ToString
            'Dim email As String = ReturnNothing(row("mobile").toString)

            If row("driver_no").ToString = "Proposer" And testdebug = True Then
                mobile = "+447700900001"
            Else
                'email = Nothing
            End If

            Dim person As Person = New Person With {
            .person_id = row("person_id").ToString,
            .email = row("email").ToString,
            .mobile = mobile,
            .forename = row("forename").ToString,
            .surname = row("surname").ToString,
            .dob = row("dob").ToString,
            .address = New PersonAddress() With {
                .line_1 = row("address.line_1").ToString,
                .town_city = row("address.town_city").ToString,
                .postal_code = row("address.postal_code").ToString,
                .country = row("address.country").ToString
            },
            .driver_details = New Driver_Details() With {
                .licence_type = row("driver_details.licence_type").ToString,
                .licence_date_obtained = row("driver_details.licence_date_obtained").ToString,
                .licence_country_code = row("driver_details.licence_country_code").ToString,
                    .ncb_details = New NCB_Details() With {
                        .ncb_country = row("driver_details.ncb_details.ncb_country").ToString,
                        .ncb_type = row("driver_details.ncb_details.ncb_type").ToString,
                        .ncb_years = row("driver_details.ncb_details.ncb_years").ToString},
                .convictions = get_convictions(row("B@"), row("PolRef@"), row("driver_no"))},
            .roles = dt_apiroles.AsEnumerable().Where(Function(x) x(2) = row("person_id").ToString).Select(Function(x) x(3).ToString()).ToList, 'ObtainRoles(ReturnNothing(row("person_id").toString)),
            .documents_required = dt_apidocs.AsEnumerable().Where(Function(x) x(2) = row("person_id").ToString).Select(Function(x) x(3).ToString()).ToList 'ObtainDocsRequired(ReturnNothing(row("person_id").toString))
            }

            objpeople.Add(person)
        Next

        Return objpeople
    End Function

对于没有地址记录的人,它返回以下内容,它也出现在 ncb_details 上,但我想如果我能让一个工作我可以让另一个工作。

{
      "person_id": "12345",
      "forename": "Jim",
      "surname": "Test",
      "address": {}, 'HERE
      "dob": "1969-11-24",
      "driver_details": {
        "licence_type": "FULL",
        "licence_date_obtained": "1097-11-18",
        "licence_country_code": "GB",
        "ncb_details": {}, 'HERE
        "convictions": []
      },

我也在使用以下设置进行序列化:

        Dim settings = New JsonSerializerSettings With {
            .NullValueHandling = NullValueHandling.Ignore,
            .DefaultValueHandling = DefaultValueHandling.Ignore,
            .Formatting = Formatting.Indented
        }

        Dim json As String = JsonConvert.SerializeObject(oPolicy, settings)

标签: json.netvb.netjson.net

解决方案


推荐阅读