json - .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)
解决方案
推荐阅读
- android - 无法序列化 DslActivationStatus 状态...Android Studio
- python-3.x - 按季度平均分组并获取在平均计算中使用的值 -pandas
- git - 仅将具有历史记录的 TFS 存储库的一部分迁移到 Git azure devops
- javascript - 如何在 HTML5 中打开文件夹
- python - 你能解释一下 keras 模型中 tensorflow 加载和 hdf5 加载之间的区别吗
- postgresql - 在 Postgres 中将复合数组 UNNEST 成行和列
- c++ - 函数内的 C++2a 递归 lambda
- python - 从控制台运行 python 脚本时出现 SSLError
- stm32 - STM32 内存布局和寻址
- swift5 - 从 UIDocumentPickerViewController / .stopAccessingSecurityScopedResource() 保存图像的问题