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

时间:2013-08-20 09:23:25

标签: c# json twitter

使用下面的url我试图拉动特定屏幕名称具有正常工作的关注者。当我尝试将代码反序列化为一个ojbect时,我会在下面找到任何想法的错误消息。我也把代码用于Json类型..我想获得位置归档。我已经发布用户本身就是一个对象。所以我可以得到一个例子,它可以让我对初始对象进行脱盐,然后将对象放入其中。

网址= "https://api.twitter.com/1.1/followers/list.json?&screen_name="will insert here "

反序列化为对象代码

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; }

}

错误消息

  

{“无法反序列化当前的JSON对象(例如   {\“name \”:\“value \”})进入类型   'System.Collections.Generic.List`1 [OAuthTwitterWrapper.JsonTypes.FollowerUsers]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。\ r \ n要修复此错误,请将JSON更改为JSON数组   (例如[1,2,3])或更改反序列化类型以使其正常   .NET类型(例如,不是整数的基本类型,不是集合   类似于数组或List的类型,可以从JSON反序列化   宾语。 JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。\ r \ n“'用户',第1行,位置   9。“}

Json String Examplt

{
    "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"
}

3 个答案:

答案 0 :(得分:6)

  

我需要的唯一字段是用户表中的位置

您不需要任何课程来从您的json获取一些字段。只需使用dynamic

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

答案 1 :(得分:1)

Follower[] result = JsonConvert.DeserializeObject<Follower[]>(FollowerData);

如果您希望它是List<Follower>,那么您可以致电.ToList()(您需要using System.Linq;)。


using System.Linq;

List<Follower> result = JsonConvert.DeserializeObject<Follower[]>(FollowerData).ToList();

或者你可以将数组传递给列表(没有linq):

List<Follower> result = new List<Follower>(JsonConvert.DeserializeObject<Follower[]>(FollowerData));

答案 2 :(得分:0)

根据您的JSON,您要检索的数据是一个JSON数组(用户数据在右括号之间的项目 - '“用户”:[] part')

因此,要正确解析它,必须创建另一个包含Follower对象数组的类,并向此数组添加JsonProperty("users")属性。然后,您可以正确地将JSON反序列化到该类。

澄清:

容器类:

public class FollowerContainer
{
   [JsonProperty("users")]
   public Follower[] Followers { get; set; }
}

反序列化:

var result = JsonConvert.DeserializeObject(json, typeof (FollowerCollection));

您可以使用result.Followers来访问您的数据。