Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net JSON.NET返回Null或Nothing值_Vb.net_Json.net - Fatal编程技术网

Vb.net JSON.NET返回Null或Nothing值

Vb.net JSON.NET返回Null或Nothing值,vb.net,json.net,Vb.net,Json.net,我目前正在使用存储在datatables中的数据构建JSON。但是,如果数据表的列中不存在数据,则会向我的属性返回一个空(“”)字符串 这会在我的JSON上出现,然后由于所讨论的web服务没有对其进行验证而无法接收 但是,如果我将该值设置为“Nothing”,它不会序列化,因此不会出现在JSON中。我怎么能不返回字符串为“”的所有字符串值 我可以通过编写一个函数来实现这一点,该函数可以测试字符串,并且不返回任何内容,无论我觉得这样做肯定有什么不对 这里有一个例子 Public Class Pol

我目前正在使用存储在datatables中的数据构建JSON。但是,如果数据表的列中不存在数据,则会向我的属性返回一个空(“”)字符串

这会在我的JSON上出现,然后由于所讨论的web服务没有对其进行验证而无法接收

但是,如果我将该值设置为“Nothing”,它不会序列化,因此不会出现在JSON中。我怎么能不返回字符串为“”的所有字符串值

我可以通过编写一个函数来实现这一点,该函数可以测试字符串,并且不返回任何内容,无论我觉得这样做肯定有什么不对

这里有一个例子

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
公共类策略
公共属性策略\u id为字符串=无
公共财产保险公司名称为字符串=无
公共属性策略\u名称为字符串=无
公共属性产品名称为String=Nothing
公共财产出售日期为DateTime=无
公共属性开始日期为DateTime=Nothing
公共属性end_date As DateTime=无
公共属性状态为String=Nothing
新的公共财产车辆清单(车辆)
公共财产人员作为新名单(人员)
末级
私有函数get_JSON(ByVal分支作为字符串,ByVal policyref作为字符串)作为字符串
对于dt_policies.Rows中的每个p作为数据行
Dim oPolicy作为新策略()使用{
.policy_id=p(“B”)和p(“PolRef”),
.保险人名称=p(“保险人名称”),
.policy_name=p(“policy_name”),
.product\U name=p(“product\U name”),
.销售日期=p(“销售日期”),
.开始日期=p(“开始日期”),
.end_date=p(“end_date”),
.状态=p(“状态”),
.vehicles=get_vehicles(p(“B”)、p(“PolRef”),
.persons=get_persons(p(“B”)、p(“PolRef”))
}
Dim json As String=JsonConvert.SerializeObject(oPolicy,NullValueHandling.Ignore)
返回json
下一个
端函数
私有函数将Nothing(ByVal rstring作为字符串)作为字符串返回
如果rstring=“”,则
一无所获
其他的
返回字符串
如果结束
端函数

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

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
问题解决了

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

您考虑过答案吗?@AndrewMorton-问题是数据表返回的不是DBNull就是空字符串…因此这会覆盖“默认值”。DBNull会导致失败,空字符串表示元素已显示。我已经使用了我创建的变通方法,但显然有更好的方法。与其将其称为变通方法,不如将其称为数据规范化。不过,我不相信这是正确的方法。
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)