2011-02-15 134 views
0

我正在訪問facebook api並使用json.net庫(newtonsoft.json.net) 我聲明瞭一個Jobject來解析內容並查找特定元素並獲取它們的值。一切工作正常的前幾個,但然後我得到這個無法解釋的nullexception錯誤「(對象引用未設置爲對象的實例)使用JSON.net進行JSON解析

現在我看看聲明,但看不到如何改變它。理解:

Dim jobj as JObject = JObject.Parse(responseData) 
message = jobj("message").tostring 

發生該錯誤在最後一行above.I檢查是否消息是空,然後尋找下一個期望的字段如下

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" 
    }, 

這是一個例子,其中「消息」沒有出現在前幾行但稍後出現。所以我所做的就是尋找消息和描述的位置,以及哪個是第一個去獲取,如果我得到一個錯誤或者字段沒有返回任何東西,我嘗試用正則表達式解析,甚至是不正確的。

回答

2

那麼,大概jobj("message")已經返回Nothing,這將發生如果jobj沒有message屬性。如果您認爲這是不正確的,請張貼一小段但完整的JSON來幫助我們進行調查。

(是否有任何理由,你爲什麼要宣佈message和第二行分配一個值,只在第三行覆蓋該值?)

+0

我檢查爲,當我看着響應數據我看到現場消息... – vbNewbie 2011-02-15 15:48:03