JSON.NET返回Null或Nothing值



我目前正在使用存储在数据表中的数据构建JSON。然而,当数据表的列中不存在数据时,它会向我的属性返回一个空("(字符串。

然后,这在我的JSON上出现,然后由于所讨论的web服务没有验证它,它无法接收。

然而,如果我将该值设置为"0";什么都没有";它不序列化,因此不会出现在JSON中。我如何才能使Nothing返回到我的所有字符串值&";。

我可以通过编写一个函数来实现这一点,该函数测试字符串并返回Nothing,尽管我觉得必须这样做一定有问题。

以下是的示例

Public Class Policy
<JsonProperty("policy_id", NullValueHandling:=NullValueHandling.Ignore)>
Public Property policy_id As String = Nothing
<JsonProperty("insurer_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property insurer_name As String = Nothing
<JsonProperty("policy_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property policy_name As String = Nothing
<JsonProperty("product_name", NullValueHandling:=NullValueHandling.Ignore)>
Public Property product_name As String = Nothing
<JsonProperty("sale_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property sale_date As DateTime = Nothing
<JsonProperty("start_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property start_date As DateTime = Nothing
<JsonProperty("end_date", NullValueHandling:=NullValueHandling.Ignore)>
Public Property end_date As DateTime = Nothing
<JsonProperty("status", NullValueHandling:=NullValueHandling.Ignore)>
Public Property status As String = Nothing
<JsonProperty("vehicles", NullValueHandling:=NullValueHandling.Ignore)>
Public Property vehicles As New List(Of Vehicle)
<JsonProperty("people", NullValueHandling:=NullValueHandling.Ignore)>
Public Property persons As New List(Of Person)
End Class
Private Function get_JSON(ByVal branch As String, ByVal policyref As String) As String
For Each p As DataRow In dt_policies.Rows

Dim oPolicy As New Policy() With {
.policy_id = p("B@") & p("PolRef@"),
.insurer_name = p("insurer_name"),
.policy_name = p("policy_name"),
.product_name = p("product_name"),
.sale_date = p("sale_date"),
.start_date = p("start_date"),
.end_date = p("end_date"),
.status = p("status"),
.vehicles = get_vehicles(p("B@"), p("PolRef@")),
.persons = get_persons(p("B@"), p("PolRef@"))
}
Dim json As String = JsonConvert.SerializeObject(oPolicy, NullValueHandling.Ignore)
Return json
Next
End Function
Private Function ReturnNothing(ByVal rstring As String) As String
If rstring = "" Then
Return Nothing
Else
Return rstring
End If
End Function

您可以制作一个扩展方法,将提取和规范化数据结合到一个步骤中,这样您就不必考虑它:

Imports System.Runtime.CompilerServices
Module DataTableExtensions
<Extension>
Function GetVal(row As DataRow, columnName As String) As Object
Dim val As Object = row(columnName)
If TypeOf val Is DBNull OrElse (TypeOf val Is String AndAlso val = "") Then
Return Nothing
End If
Return val
End Function
End Module

然后在您的代码中,只要在使用行索引器的任何地方使用扩展方法:

Dim oPolicy As New Policy() With 
{
.policy_id = p.GetVal("B@") & p.GetVal("PolRef@"),
.insurer_name = p.GetVal("insurer_name"),
.policy_name = p.GetVal("policy_name"),
.product_name = p.GetVal("product_name"),
.sale_date = p.GetVal("sale_date"),
.start_date = p.GetVal("start_date"),
.end_date = p.GetVal("end_date"),
.status = p.GetVal("status"),
.vehicles = get_vehicles(p.GetVal("B@"), p.GetVal("PolRef@")),
.persons = get_persons(p.GetVal("B@"), p.GetVal("PolRef@"))
}

问题已解决。

Public Class Policy
<DefaultValue("")> <JsonProperty("policy_id")>
Public Property policy_id As String = Nothing
<DefaultValue("")> <JsonProperty("insurer_name")>
Public Property insurer_name As String = Nothing
<DefaultValue("")> <JsonProperty("policy_name")>
Public Property policy_name As String = Nothing
<DefaultValue("")> <JsonProperty("product_name")>
Public Property product_name As String = Nothing
<DefaultValue("")> <JsonProperty("sale_date")>
Public Property sale_date As DateTime = Nothing
<DefaultValue("")> <JsonProperty("start_date")>
Public Property start_date As DateTime = Nothing
<DefaultValue("")> <JsonProperty("end_date")>
Public Property end_date As DateTime = Nothing
<DefaultValue("")> <JsonProperty("status")>
Public Property status As String = Nothing
<DefaultValue("")> <JsonProperty("vehicles")>
Public Property vehicles As New List(Of Vehicle)
<DefaultValue("")> <JsonProperty("people")>
Public Property persons As New List(Of Person)
End Class

为每个属性设置一个默认值,然后更改序列化程序的设置。

Dim settings = New JsonSerializerSettings With {
.NullValueHandling = NullValueHandling.Ignore,
.DefaultValueHandling = DefaultValueHandling.Ignore,
.Formatting = Formatting.Indented
}
Dim json As String = JsonConvert.SerializeObject(oPolicy, settings)

最新更新