3

using the url below i am trying to pull the followers that a particular screen name has which is working fine. when i try deserialize the code into a ojbect i get the error message below any ideas why this would be. I have also put the code for the for the Json type.. I would like to get the Location filed. i have released that the users is an object itself. so can i get an example that will let me Desalinize the initial object anf then the object within.

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

Deserialize into objec code

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

Json type code

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

}

Error Message

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

3 回答 3

6

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

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

dynamic dynObj = JsonConvert.DeserializeObject(json); 
Console.WriteLine(dynObj.users[0].location);
于 2013-08-20T11:15:48.107 回答
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));
于 2013-08-20T09:25:56.950 回答
0

根据您的 JSON,您要检索的数据位于 JSON 数组中(带有用户数据的项目位于右括号之间 - '"users" : [ ] part ')

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

澄清:

容器类:

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

反序列化:

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

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

于 2013-08-20T11:36:02.967 回答