嘿,我有下面的json响应,我正试图找到:
{
"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],
我正试图找到3570956071,但我似乎无法使用JSON.net找到它。
我的代码是:
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....
出现错误:对象引用未设置为对象的实例
我错过了什么?
更新
o("3570956071")的值为无。
但正如您在json响应中看到的,它就在那里。。
执行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"
异常Unable to cast object of type 'Newtonsoft.Json.Linq.JProperty' to type 'Newtonsoft.Json.Linq.JObject'
是因为JObject
的默认成员Item(String)
返回一个JToken对象,它是JContainer的超类,而JContaine又是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
它应该防止无效强制转换的发生。