c# - 将 json twitter 字符串反序列化为对象

标签 c# json twitter

我试图使用下面的 url 拉取特定屏幕名称所具有的关注者,这些关注者工作正常。当我尝试将代码反序列化为一个 ojbect 时,我在任何想法下面收到错误消息,为什么会这样。我还放置了 Json 类型的代码。我想获取 Location 文件。我已经发布了用户本身就是一个对象。那么我能否得到一个示例,让我先对初始对象进行脱盐处理,然后再对其中的对象进行脱盐处理。

URL = "https://api.twitter.com/1.1/followers/list.json?&screen_name="将插入此处"

反序列化为目标代码

var result = JsonConvert.DeserializeObject<List>(FollowerData)

Json类型代码

public class Follower
{

[JsonProperty("created_at")]
public string CreatedAt { get; set; }

[JsonProperty("id")]
public string Id { get; set; }

[JsonProperty("id_str")]
public string IdStr { get; set; }

[JsonProperty("name")]
public string Name { get; set; }

[JsonProperty("screen_name")]
public string ScreenName { get; set; }

[JsonProperty("location")]
public bool Location { get; set; }

[JsonProperty("description")]
public string Description { get; set; }

}

错误信息

{"Cannot deserialize the current JSON object (e.g. {\"name\":\"value\"}) into type 'System.Collections.Generic.List`1[OAuthTwitterWrapper.JsonTypes.FollowerUsers]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.\r\nTo fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.\r\nPath 'users', line 1, position 9."}

Json 字符串示例

{
    "users": [
        {
            "id": 219566993,
            "id_str": "219566993",
            "name": "belenastorgano",
            "screen_name": "anna_belenn_",
            "location": "CapitalFederal, Argentina",
            "description": "Mesientonomade, todav\\u00edanotengounlugarfijodondevivir.-",
            "url": null,
            "entities": {
                "description": {
                    "urls": []
                }
            },
            "protected": true,
            "followers_count": 44,
            "friends_count": 64,
            "listed_count": 0,
            "created_at": "ThuNov2506: 28: 12+00002010",
            "favourites_count": 1,
            "utc_offset": -10800,
            "time_zone": "BuenosAires",
            "geo_enabled": true,
            "verified": false,
            "statuses_count": 207,
            "lang": "es",
            "contributors_enabled": false,
            "is_translator": false,
            "profile_background_color": "599E92",
            "profile_background_image_url": "http: \\/\\/a0.twimg.com\\/images\\/themes\\/theme18\\/bg.gif",
            "profile_background_image_url_https": "https: \\/\\/si0.twimg.com\\/images\\/themes\\/theme18\\/bg.gif",
            "profile_background_tile": false,
            "profile_image_url": "http: \\/\\/a0.twimg.com\\/profile_images\\/378800000326157070\\/e91b8fd8e12eda0a7fa350dcd286c56a_normal.jpeg",
            "profile_image_url_https": "https: \\/\\/si0.twimg.com\\/profile_images\\/378800000326157070\\/e91b8fd8e12eda0a7fa350dcd286c56a_normal.jpeg",
            "profile_link_color": "E05365",
            "profile_sidebar_border_color": "EEEEEE",
            "profile_sidebar_fill_color": "F6F6F6",
            "profile_text_color": "333333",
            "profile_use_background_image": true,
            "default_profile": false,
            "default_profile_image": false,
            "following": null,
            "follow_request_sent": null,
            "notifications": null
        }
    ],
    "next_cursor": 1443863551966642400,
    "next_cursor_str": "1443863551966642309",
    "previous_cursor": 0,
    "previous_cursor_str": "0"
}

最佳答案

the only field i need is the location in the user table

你不需要任何类来从你的 json 中获取一些字段。只需使用 dynamic

dynamic dynObj = JsonConvert.DeserializeObject(json); 
Console.WriteLine(dynObj.users[0].location);

关于c# - 将 json twitter 字符串反序列化为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18331446/

相关文章:

javascript - 在 Protractor 测试中使用外部测试数据

r - 用ggmap绘制热图?

c# - 如何获取两个字符之间但具有选定数字的所有文本?

c# - 标准 Dispose 模式的代码编织助手?

c# - 如何为调用基对象中的基方法的方法编写单元测试

java - Json深度序列化

c# - 将 JSON 数组转换为 C# 对象集合

c# - 浏览器的后退按钮在 ASP.NET 应用程序中随机禁用

facebook - 在 Coldfusion 页面上聚合并显示 facebook 和 twitter 状态更新

ios - 无法使用 Twitter+OAuth 集成 Twitter 登录?