【发布时间】:2014-11-19 19:14:01
【问题描述】:
大家好,我在代码中随机出现以下错误:
对象引用未设置为对象的实例。
我知道我为什么会得到它。它没有找到我正在寻找的正确属性,因此它给出了错误。有些可能具有该属性,而有些则如此错误所示,可能没有。
我可以做些什么来首先检查以确保它具有该属性?目前我只有一个 Try/catch 方法,所以如果它确实找到了不存在的东西,它可以继续运行。
For Each Row In json("data")
Try
thePostID = DirectCast(Row("id").ToString(), String)
thePostType = DirectCast(Row("type").ToString(), String)
thePosterID = DirectCast(Row("from")("id").ToString(), String)
thePosterName = DirectCast(Row("from")("name").ToString(), String)
Catch ex As NullReferenceException
msgbox("Did not find that particular property!")
End Try
Next
更新
{
"data": [
{
"id": "102zzz533zz_10z52zz9zzzz94z3",
"from": {
"id": "102zzzzz95zzz7",
"name": "Jim zzzzz"
},
"likes": {
"data": [
{
"id": "85zzzzz35zzzz0",
"name": "Anna zzzzz"
},
{
"id": "10zzzz93z31zzzzz",
"name": "Vanessa zzzz zzzz"
},
{
"id": "1zzz44zzz48731z6",
"name": "Leta zzzzzz"
}
],
"paging": {
"cursors": {
"after": "MTAyMdfasdfwrtMTkyNg=",
"before": "ODUasdfasrU5Mwerw"
}
}
}
etc...
上面的这个 JSON 与所有其他 JSON 遵循相同的 data 路径。
使用下面的@Andrews 代码:
thePostLikes = NullSafeSelect(Row, "likes.data.id")
If thePostLikes <> "NA" Then
For Each Row2 In json("likes")("data")
thePostLikesID += NullSafeSelect(Row2, "id") & ","
thePostLikesName += NullSafeSelect(Row2, "name") & ","
Next
End If
thePostLikes 的值始终是Nothing
【问题讨论】:
标签: json vb.net json.net nullreferenceexception