2013-08-27 27 views
0

嘿所有我有以下JSON響應,我想找到:JSON.NET從值陣列啓動

{ 
"threaded_extended": { 
"3570956071": [ 
    { 
    "id": [edited], 
    "network_id": [edited], 
    "sender_type": "user", 
    "url": "[edited]", 
    "sender_id": [edited], 
    "privacy": "public", 
    "body": { 
     "rich": "[edited]", 
     "parsed": "[edited]", 
     "plain": "[edited]" 
    }, 
    "liked_by": { 
     "count": 0, 
     "names": [] 
    }, 
    "thread_id": [edited], 

我試圖找到,但我不能似乎它使用JSON找到。淨。

我的代碼是這樣的:

Dim url As String = "https://www.[edited].json?access_token=" & yAPI.userToken & "&threaded=extended" 
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest) 
    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse) 
    Dim reader As StreamReader = New StreamReader(response.GetResponseStream()) 
    Dim o As JObject = JObject.Parse(reader.ReadToEnd) 

For Each msg3 As JObject In o("threaded_extended")("3570956071") 
'etc etc.... 

,我得到的錯誤:對象引用不設置到對象的實例。

我甚至試過:

For Each msg3 As JObject In o("threaded_extended") 
'etc etc.... 

並且得到錯誤:無法轉換類型 'Newtonsoft.Json.Linq.JProperty' 的對象鍵入 'Newtonsoft.Json.Linq.JObject'。

最後只是這樣做:

For Each msg3 As JObject In o("3570956071") 
'etc etc.... 

給我的錯誤:對象引用不設置到對象的實例。

我在想什麼?

UPDATE

0的值( 「3570956071」)是沒有

但是當你在JSON resonse看,它的存在..

做O( 「threaded_extended」)給我調試中的數字。

調試看起來是這樣的:

"3570956071": [ 
{ 
    "chat_client_sequence": null, 
    "replied_to_id": [edited], 
    "network_id": [edited], 
    "created_at": "2013/08/27 19:26:41 +0000", 
    "privacy": "public", 
    "attachments": [], 
    "sender_id": [edited], 
    "liked_by": { 
    "names": [], 
    "count": 0 
    }, 
    "system_message": false, 
    "group_id": [edited], 
    "thread_id": [edited], 
    'etc etc 

但是從它表明錯誤無法投類型的對象Newtonsoft.Json.Linq.JProperty爲鍵入「Newtonsoft.Json.Linq繼續。 JObject'

+0

這一行是否需要一個NEW語句? 'Dim o As new JObject = JObject.Parse(reader.ReadToEnd)' –

+0

@Mertis我從JSON中收集到的所有其他東西都返回工作,但這個是我所知道的,不是因爲這個原因。 – StealthRT

+0

單步執行代碼。在哪個特定行發生錯誤? –

回答

0

例外Unable to cast object of type 'Newtonsoft.Json.Linq.JProperty' to type 'Newtonsoft.Json.Linq.JObject'是因爲JObject的默認構件,Item(String),返回一個JToken對象,它是一個超類JContainer,其又一個超類JProperty和JObject。如果您將For Each循環更改爲看起來像這樣

For Each msg3 As JToken In o("3570956071") 
    If msg3.GetType() Is GetType(JObject) Then 
     ..etc.. 
    ElseIf msg3.GetType() Is GetType(JProperty) Then 
     ..etc.. 
    End If 
Next 

它應該防止發生無效投射。

+0

這沒有奏效。它甚至不知道如何處理JObject/JProperty變量。 – StealthRT

+0

這樣做**對於每個msg3作爲JToken在o(「threaded_extended」)**是讓它在該行沒有錯誤但它仍然不能通過JObject/JProperty變量的唯一方法。 – StealthRT

+0

錯誤是** JObject'是一種類型,不能用作表達式。** – StealthRT