0

ねえ、私は見つけようとしている次の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") の値はNothingです。

しかし、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

しかし、それから続けて、エラーUnable to cast object of type 'Newtonsoft.Json.Linq.JProperty' to type 'Newtonsoft.Json.Linq.JObject' を示しています

4

1 に答える 1

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

無効なキャストが発生するのを防ぐ必要があります。

于 2013-08-27T20:51:47.250 に答える