2013-08-26 64 views
0

嘿我公司通过与下面的JSON响应试图循环:JSON.net通过解析数据

Dim url As String = "https://www.[site here].com/api/v1/messages.json?access_token=" & yAPI.userToken 
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) 

reader.Close() 
response.Close() 

Dim mtemp As String = DirectCast(o("messages")(0)("body")("rich").ToString(), String) 

我能得到的数据只是罚款mtemp,但只得到了第一套,而不是循环通过JSON的所有其余部分。

响应的样品是:

{ 
    json data here... 
}, 
"threaded_extended": {}, 
    "messages": [ 
    { 
     "network_id": [edited here], 
     "chat_client_sequence": null, 
     "privacy": "public", 
     "body": { 
     "urls": [ 
      "[edited here]" 
     ], 
     "rich": "[edited here]", 
     "plain": "[edited here]", 
     "parsed": "[edited here]" 
     }, 
     "sender_id": [edited here], 
     "content_excerpt": "[edited here]", 
     "client_url": "[edited here]", 
     "client_type": "Web", 
     "web_url": "[edited here]", 
     "created_at": "2013/08/26 19:31:50 +0000", 
     "language": null, 
     etc etc................... 
    }, 
    { 
     "network_id": [edited here], 
     "chat_client_sequence": null, 
     "privacy": "public", 
     "body": { 
     "rich": "[edited here]", 
     "plain": "[edited here]", 
     "parsed": "[edited here]" 
     }, 
     "sender_id": [edited here], 
     "content_excerpt": "[edited here]", 
     "client_url": "[edited here]", 
     "web_url": "[edited here]", 
     "client_type": "Web", 
     "created_at": "2013/08/26 19:25:00 +0000", 
     "language": null, 
     etc etc.... 
    }, 

我如何可以继续循环,直到它达到我期待的最后一刻?

回答

2

o("messages")JArray,所以你可以枚举它。在你的代码中,你只需要第一个项目。如果你想循环,你需要一个循环结构。

For Each msg As JObject In o("messages") 
    ' Do something with each msg 
    Debug.WriteLine(msg("body")("rich").ToString()) 
Next 
+1

非常感谢,Brian! – StealthRT

+0

非常感谢。 –