首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用JSON.NET在C#中反序列化twitter以获取哈希标签

用JSON.NET在C#中反序列化twitter以获取哈希标签
EN

Stack Overflow用户
提问于 2012-09-20 10:55:04
回答 1查看 4.8K关注 0票数 4

也许我是在重复这个问题,但我是第一次尝试这个问题,我找不到任何与我的情况完全相同的东西。

我正在用C#编写代码,并使用JSON.NET来反序列化json。作为对GET请求的响应,json文件来自twitter:

entities=true

json是来自给定用户的twitter时间线的20个最新tweet (json对象)的列表,单个json对象的格式如下所示:

代码语言:javascript
复制
{
"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

}

我使用这段代码获取(反序列化)列表:

代码语言:javascript
复制
List<JsonObject> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JsonObject>>(response);

当'response‘持有我的json.When时,我插入了一个断点并观察到'list’中的json对象,我发现如下:

{ "created_at":“清华9月20日10:25:39 +0000 2012年”,"id":248729631476314100,"id_str":"248729631476314112","text":“询问专家并学习在#MongoDBPune (http://t.co/o5BA7zd6)和#MongoDBBangalore (http://t.co/7q3E2MBq)第2.2节中的新内容”,“源代码”:“社会主义”,“截断”: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":空,“坐标”:空,“位置”:空,“贡献者”:空,"retweet_count":1,“实体”:[ ],[],[] ],[] ],[[]],]、[] ]、[]、[]、[]、[] ]、[ [] ]、[ [] ]、[ [] ]、[] ]、[ []、[ [] ]、“最爱”:false、“重发”:false、"possibly_sensitive":false }

我打算从所有的20个json objects..So收集所有的哈希标签,本质上我需要在实体中导航到哈希标记到文本。但如上文所示,反序列化只是部分的。它没有反序列化任何嵌套的东西。

我该怎么解决这个问题?请帮帮我!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-24 10:42:59

天哪!是从this stackoverflow question那里得到的。谢谢@chill182 182!

你是怎么做到的:

代码语言:javascript
复制
 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 :)

代码语言:javascript
复制
using Newtonsoft.Json.Linq;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12511171

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档