json - 如何处理 VB.Net 中不存在的已解析 JSON 属性/值?
问题描述
我会直截了当,这对某人来说可能是一个简单的答案。从 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
解决方案
?
尝试在任何可能未填充的令牌访问器之间使用空传播运算符:
Dim telephone = If(item("properties")("phone")?("value").ToString(), DBNull.Value)
如果一个特定的标记是Nothing
,它将返回Nothing
并将If
变量设置为DBNull.Value
推荐阅读
- kubernetes - Kubernetes 中 Prometheus 的动态目标?
- c# - 设置组合框自动完成的宽度
- html - div的动画背景颜色不起作用
- python - 为什么外键显示为下拉框?
- python - 为我的 kmeans 聚类数据创建散点图时遇到问题
- windows - 向 IE 代理设置添加例外以允许连接到 Exchange Online
- spring-boot - 红帽中间件 Camel 解析 JDBC 输出
- javascript - Javascript 获取对 servicedesk plus 的 API 调用失败
- filtering - 调用 getGUI 后,ag-grid 自定义下拉过滤器不会刷新过滤器的 GUI
- ajax - 我在ajax中有什么遗漏吗