gpt4 book ai didi

json - 使用 C# 中的 JSON.NET 反序列化 Twitter JSON 以获取主题标签

转载 作者:行者123 更新时间:2023-12-01 05:28:50 29 4
gpt4 key购买 nike

也许我在重复这个问题,但我是第一次尝试这个,但我找不到与我的情况完全一样的东西..我正在用 C# 编码并使用 JSON.NET 反序列化一个 json。 json 文件来自 twitter,作为对此 GET 请求的响应:
https://api.twitter.com/1/statuses/home_timeline.json?include_entities=true
json 是来自给定用户的 twitter 时间线的 20 条最新推文(json 对象)的列表,单个 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": "2012 年 9 月 20 日星期四 10:25:39 +0000",
“id”:248729631476314100,
"id_str": "248729631476314112",
"text": "在#MongoDBPune (http://t.co/o5BA7zd6) 和 #MongoDBBangalore (http://t.co/7q3E2MBq) 上询问专家并了解 v.2.2 的新功能",
"source": "Socialrithmic",
“截断”:假,
“in_reply_to_status_id”:空,
“in_reply_to_status_id_str”:空,
“in_reply_to_user_id”:空,
“in_reply_to_user_id_str”:空,
“in_reply_to_screen_name”:空,
“用户”:[
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
],
[
[]
]
],
“地理”:空,
“坐标”:空,
“地方”:空,
“贡献者”:空,
“转推计数”:1,
“实体”:[
[
[
[
[
[]
],
[
[
[],
[]
]
]
],
[
[
[]
],
[
[
[],
[]
]
]
]
]
],
[
[
[
[
[]
],
[
[]
],
[
[]
],
[
[
[],
[]
]
]
],
[
[
[]
],
[
[]
],
[
[]
],
[
[
[],
[]
]
]
]
]
],
[
[]
]
],
“最喜欢的”:假,
“转发”:假,
“可能_敏感”:假
}

我打算从所有 20 个 json 对象中收集所有主题标签..所以基本上我需要在实体内部导航到主题标签到文本。但如上所见,反序列化只是部分的。它不会反序列化任何嵌套的东西。
我该如何解决?请帮忙!

最佳答案

我的妈呀!从 this 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;

关于json - 使用 C# 中的 JSON.NET 反序列化 Twitter JSON 以获取主题标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12511171/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com