如何处理VB.Net中不存在的已解析JSON属性/值?

时间:2018-07-26 08:57:49

标签: json vb.net parsing json.net

我会讲到重点,这对某人来说可能是一个简单的答案。从HubSpot返回的JSON可能包含或可能不包含电话和地址之类的属性,因为它没有在HubSpot中填写。这会在下面的For Each块中导致错误:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=HubSpotGetAllCompanies

如何处理它,例如,如果电话没有属性,则可以输入DBNull值。

谢谢!

Imports System.ComponentModel
Imports System.IO
Imports System.Net
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Module Index

Sub Main()

    Dim counter = 0
    Dim offset As String = String.Empty
    Dim hasmore As Boolean = False
    Dim hapikey = "xxx"
    Dim hubSpot As New Dal.HubSpot.Pull


    'Create Table
    Dim companiesTable As DataTable = New DataTable()
    companiesTable.Columns.Add("PortalID")
    companiesTable.Columns.Add("CompanyID")
    companiesTable.Columns.Add("Company")
    companiesTable.Columns.Add("Website")
    companiesTable.Columns.Add("Address1")
    companiesTable.Columns.Add("City")
    companiesTable.Columns.Add("Country")
    companiesTable.Columns.Add("Postcode")
    companiesTable.Columns.Add("Telephone")
    companiesTable.Columns.Add("Ref")
    companiesTable.Columns.Add("VatCode")

    'Create Values


    'Loop as you can only return so many companies at once (250 is limit I believe)
    Do
        Dim url As String = String.Format("https://api.hubapi.com/companies/v2/companies/paged?hapikey={0}&properties=name&properties=website&properties=address&properties=city&properties=country&properties=zip&properties=phone&limit=10{1}", hapikey, offset)
        Dim httpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
        httpWebRequest.ContentType = "application/json"
        httpWebRequest.Method = "GET"
        Dim httpResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)

        Using streamReader = New StreamReader(httpResponse.GetResponseStream())
            Dim result = streamReader.ReadToEnd()

            Dim jObject As JObject = JObject.Parse(result)
            Dim jhasmore As JToken = jObject("has-more")
            Dim joffset As JToken = jObject("offset")
            Dim jcompanies As JToken = jObject("companies")

            If jhasmore.ToString().ToLower() = "true" Then
                hasmore = True
            Else
                hasmore = False
            End If

            offset = String.Format("&offset={0}", joffset.ToString())

            For Each item In jcompanies.ToList()
                Dim portalId = item("portalId").ToString()
                Dim companyId = item("companyId").ToString()
                Dim company = item("properties")("name")("value").ToString()
                Dim website = If(item("properties")("website")("value").ToString(), DBNull.Value)
                Dim address1 = If(item("properties")("address")("value").ToString(), DBNull.Value)
                Dim city = If(item("properties")("city")("value").ToString(), DBNull.Value)
                Dim country = If(item("properties")("country")("value").ToString(), DBNull.Value)
                Dim postcode = If(item("properties")("zip")("value").ToString(), DBNull.Value)
                Dim telephone = If(item("properties")("phone")("value").ToString(), DBNull.Value)
                Dim ref = DBNull.Value
                Dim vatCode = DBNull.Value

                companiesTable.Rows.Add(portalId, companyId, company, website, address1, city, country, postcode, telephone, ref, vatCode)

            Next

        End Using

        counter += 1
    Loop While hasmore

    hubSpot.GetAllHubSpotCompanies(companiesTable)

End Sub

1 个答案:

答案 0 :(得分:0)

尝试在可能未填充的任何令牌访问器之间使用Null传播运算符?

Dim telephone = If(item("properties")("phone")?("value").ToString(), DBNull.Value)

如果特定令牌是Nothing,它将返回NothingIf并将变量设置为DBNull.Value