使用 JSON.net 进行 JSON 解析
我正在访问 facebook api 并使用 json.net 库 (newtonsoft.json.net) 我声明一个 Jobject 来解析内容并查找特定元素并获取它们的值。前几个一切正常,但后来我得到了这个无法解释的空异常错误“(对象引用未设置到对象的实例)
现在我查看了声明,但不知道如何更改它。任何帮助表示赞赏:
Dim jobj as JObject = JObject.Parse(responseData)
message = jobj("message").tostring
错误发生在上面的最后一行。我检查消息是否为空,然后查找下一个所需的字段,如下
catch exception..
dim jobj2 as JObject = JObject.parse(responseData)
description = jobj2("description").tostring
JSON 响应数据:
{
"id": "5281959998_126883980715630",
"from": {
"name": "The New York Times",
"category": "Company",
"id": "5281959998"
},
"picture": "http://external.ak.fbcdn.net /safe_image.php?d=e207958ca7563bff0cdccf9631dfe488&w=
90&h=90&url=http\u00253A\u00252F\u00252Fgraphics8.nytimes.com\u00252Fimages\u00252F2011\u00252F02\u00252F04\u00252Fbusiness\u00252FMadoff\u00252FMadoff-thumbStandard.jpg",
"link": "http://nyti.ms/hirbn0",
"name": "JPMorgan Said to Have Doubted Madoff Long Before His Scheme Was Revealed",
"caption": "nyti.ms",
"description": "Newly unsealed court documents show that bank
executives were suspicious of Bernard Madoff\u2019s accounts
and steered clients away from him but did not alert regulators.",
"icon": "http://static.ak.fbcdn.net/rsrc.php/yD/r/aS8ecmYRys0.gif",
"type": "link",
"created_time": "2011-02-04T16:09:03+0000",
"updated_time": "2011-02-06T20:09:51+0000",
"likes": {
"data": [
{
"name": "Siege Ind.",
"category": "Product/service",
"id": "152646224787462"
},
{
"name": "Lindsey Souter",
"id": "100000466998283"
},
这是一个示例,其中“消息”没有出现在前几行中,但稍后出现。我所做的就是寻找消息和描述的位置,然后先去获取它,如果我收到错误或字段不返回任何内容,我会尝试通过正则表达式进行解析,即使这样也无法正常工作。
I am accessing the facebook api and using the json.net library (newtonsoft.json.net)
I declare a Jobject to parse the content and look for the specific elements and get their values. Everything works fine for the first few but then I get this unexplained nullexception error " (Object reference not set to an instance of an object)
Now I took a look at the declaration but cannot see how to change it. Any help appreciated:
Dim jobj as JObject = JObject.Parse(responseData)
message = jobj("message").tostring
The error occurs at the last line above.I check to see if message is null and then look for the next desired field as follows
catch exception..
dim jobj2 as JObject = JObject.parse(responseData)
description = jobj2("description").tostring
JSON responsedata:
{
"id": "5281959998_126883980715630",
"from": {
"name": "The New York Times",
"category": "Company",
"id": "5281959998"
},
"picture": "http://external.ak.fbcdn.net /safe_image.php?d=e207958ca7563bff0cdccf9631dfe488&w=
90&h=90&url=http\u00253A\u00252F\u00252Fgraphics8.nytimes.com\u00252Fimages\u00252F2011\u00252F02\u00252F04\u00252Fbusiness\u00252FMadoff\u00252FMadoff-thumbStandard.jpg",
"link": "http://nyti.ms/hirbn0",
"name": "JPMorgan Said to Have Doubted Madoff Long Before His Scheme Was Revealed",
"caption": "nyti.ms",
"description": "Newly unsealed court documents show that bank
executives were suspicious of Bernard Madoff\u2019s accounts
and steered clients away from him but did not alert regulators.",
"icon": "http://static.ak.fbcdn.net/rsrc.php/yD/r/aS8ecmYRys0.gif",
"type": "link",
"created_time": "2011-02-04T16:09:03+0000",
"updated_time": "2011-02-06T20:09:51+0000",
"likes": {
"data": [
{
"name": "Siege Ind.",
"category": "Product/service",
"id": "152646224787462"
},
{
"name": "Lindsey Souter",
"id": "100000466998283"
},
This is one example where "message" does not appear in the first few lines but appears later. So what I do is look for position of message and description and which ever is first go and get that and if I get an error or the fields do not return anything, I try and parse by regex and even that is not working right.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,大概
jobj("message")
返回了Nothing
,如果jobj
没有message
就会发生这种情况代码>属性。如果您认为这是不正确的,请发布一段简短但完整的 JSON 来帮助我们调查。(是否有任何原因导致您在第二行声明
message
并为其分配一个值,而只是在第三行覆盖该值?)Well, presumably
jobj("message")
has returnedNothing
, which will happen ifjobj
doesn't have amessage
property. If you think this is incorrect, please post a short but complete piece of JSON to help us investigate.(Is there any reason why you're declaring
message
and assigning it a value on the second line, only to overwrite that value on the third line?)