4

多分私はこの質問を繰り返していますが、私はこれを初めて試していて、私の状況とまったく同じものを見つけることができません..

私は C# でコーディングし、JSON.NET を使用して json を逆シリアル化しています。json ファイルは、この GET 要求への応答として Twitter から取得されます:
https://api.twitter.com/1/statuses/home_timeline.json?include_entities=true

特定のユーザーの twitter タイムラインと単一の json オブジェクトの形式は次のようになります。

{
"created_at": "Thu Sep 20 10:25:39 +0000 2012",
"id": 248729631476314100,
"id_str": "248729631476314112",
"text": "Ask the experts and learn what's new with v.2.2 at  at #MongoDBPune (http://t.co/o5BA7zd6) and #MongoDBBangalore (http://t.co/7q3E2MBq)",
"source": "<a href=\"http://www.socialrithmic.com\" rel=\"nofollow\">Socialrithmic</a>",
"truncated": false,
"in_reply_to_status_id": null,
"in_reply_to_status_id_str": null,
"in_reply_to_user_id": null,
"in_reply_to_user_id_str": null,
"in_reply_to_screen_name": null,
"user": {
  "id": 18080585,
  "id_str": "18080585",
  "name": "mongodb",
  "screen_name": "MongoDB",
  "location": "",
  "url": "http://mongodb.org/",
  "description": "High-performance, open source, schema-free document-oriented  database   | www.facebook.com/MongoDB",
  "protected": false,
  "followers_count": 24999,
  "friends_count": 16,
  "listed_count": 1582,
  "created_at": "Fri Dec 12 17:21:18 +0000 2008",
  "favourites_count": 6,
  "utc_offset": -18000,
  "time_zone": "Eastern Time (US & Canada)",
  "geo_enabled": true,
  "verified": false,
  "statuses_count": 1669,
  "lang": "en",
  "contributors_enabled": false,
  "is_translator": false,
  "profile_background_color": "529747",
  "profile_background_image_url": "http://a0.twimg.com/profile_background_images/619655093/767dji3m3k684nza7w9z.png",
  "profile_background_image_url_https": "https://si0.twimg.com/profile_background_images/619655093/767dji3m3k684nza7w9z.png",
  "profile_background_tile": false,
  "profile_image_url": "http://a0.twimg.com/profile_images/2452709870/wo2h8r0qy8d5lsxe5lgd_normal.png",
  "profile_image_url_https": "https://si0.twimg.com/profile_images/2452709870/wo2h8r0qy8d5lsxe5lgd_normal.png",
  "profile_link_color": "43B02A",
  "profile_sidebar_border_color": "829D5E",
  "profile_sidebar_fill_color": "99CC33",
  "profile_text_color": "3E4415",
  "profile_use_background_image": true,
  "default_profile": false,
  "default_profile_image": false,
  "following": true,
  "follow_request_sent": null,
  "notifications": null
},
"geo": null,
"coordinates": null,
"place": null,
"contributors": null,
"retweet_count": 1,
"entities": {
  "hashtags": [
    {
      "text": "MongoDBPune",
      "indices": [
        55,
        67
      ]
    },
    {
      "text": "MongoDBBangalore",
      "indices": [
        95,
        112
      ]
    }
  ],
  "urls": [
    {
      "url": "http://t.co/o5BA7zd6",
      "expanded_url": "http://soc.ai/ZV",
      "display_url": "soc.ai/ZV",
      "indices": [
        69,
        89
      ]
    },
    {
      "url": "http://t.co/7q3E2MBq",
      "expanded_url": "http://soc.ai/ZW",
      "display_url": "soc.ai/ZW",
      "indices": [
        114,
        134
      ]
    }
  ],
  "user_mentions": []
},
"favorited": false,
"retweeted": false,
"possibly_sensitive": false

}

このコードを使用して (逆シリアル化された) リストを取得します。

List<JsonObject> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonObject>>(response);

ここで、「応答」は私のjsonを保持します。ブレークポイントを挿入し、「リスト」内のjsonオブジェクトを観察すると、次のことがわかりました:

{ "created_at": "Thu Sep 20 10:25:39 +0000 2012", "id": 248729631476314100, "id_str": "248729631476314112", "text": "専門家に質問して、v.2.2 の新機能を#MongoDBPune (http://t.co/o5BA7zd6) および #MongoDBBangalore (http://t.co/7q3E2MBq) で", "source": "Socialrithmic", "truncated": false, "in_reply_to_status_id": null, "in_reply_to_status_id_str": null, "in_reply_to_user_id": null, "in_reply_to_user_id_str": null, "in_reply_to_screen_name": null, "user": [ [ [] ], [ [] ], [ [] ], [ [] ], [ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、 [ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、[ [] ]、 [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ], [ [] ] , "geo": null, "coordinates" : null, "place": null, "contributors": null, "retweet_count": 1, "entities":[ [ [ [ [ [] ], [ [ [], [] ] ] , [ [ [] ], [ [ [], [] ] ] ] ], [ [ [ [ [] ], [ [] ]、[ [] ]、[ [ []、[] ] ]、[ [ [] ]、[ [] ]、[ [] ]、[ [ []、[] ] ] ]、[ [] ] ]、「お気に入り」: false, "リツイート": false, "possibly_sensitive": false }

私は 20 のすべての json オブジェクトからすべてのハッシュタグを収集するつもりです..したがって、本質的に、エンティティ内をハッシュタグからテキストに移動する必要があります。しかし、上で見たように、逆シリアル化は部分的なものにすぎません。ネストされたものを逆シリアル化していません。

これを修正するにはどうすればよいですか? 助けてください!

4

1 に答える 1

1

良い天国!このstackoverflow questionから入手しました。ありがとう@chill182!

方法は次のとおりです。

 ArrayList hashtags = new ArrayList();

            JArray jsonDat = JArray.Parse(response); 
            for(int x = 0; x < jsonDat.Count(); x++) 
            {
                JObject entity = JObject.Parse(jsonDat[x]["entities"].ToString());
                JArray arrHashtags = JArray.Parse(entity["hashtags"].ToString());
                for(int i=0; i < arrHashtags.Count(); i++)
                {
                    JObject hashtagstuff = JObject.Parse(arrHashtags[i].ToString());
                    hashtags.Add(hashtagstuff["text"].ToString());
                }
            }

名前空間にLinqを含めることを忘れないでください:)

using Newtonsoft.Json.Linq;
于 2012-09-24T10:42:59.890 に答える