JSON.NET解析Twitter Archive问题

时间:2015-12-12 10:27:02

标签: c# json twitter

我正在尝试使用JSON.NET解析包含所有推文的JSON 基本上我只需要“text”和“id”的值。 这是我解析它的代码:

string file = ReadJSON(filename);
var parsedFile = JsonConvert.DeserializeObject(file, typeof(ATweet));

字符串文件,基本上是来自任何给定.js文件的所有行,其中包含该月的twitter存档。排除第一行,因为第一行总是使JSON解析失败。 这些是我(尝试)将这些值读入的类:

[JsonObject]
class ATweet
{
    [JsonProperty(PropertyName = "source")]
    public string Source { get; set; }
    [JsonProperty(PropertyName = "entities")]
    public ATweetEntities Entities { get; set; }
    [JsonProperty(PropertyName = "geo")]
    public string Location { get; set; }
    [JsonProperty(PropertyName = "id_str")]
    public string IdStr { get; set; }
    [JsonProperty(PropertyName = "text")]
    public string Text { get; set; }
    [JsonProperty(PropertyName = "id")]
    public long Id { get; set; }
    [JsonProperty(PropertyName = "created_at")]
    public string CreationTime { get; set; }
    [JsonProperty(PropertyName = "user")]
    public ATwitterUser Author { get; set; }

}

[JsonObject]
class ATweetEntities
{
    [JsonProperty(PropertyName = "user_mentions")]
    public string[] Mentions { get; set; }
    [JsonProperty(PropertyName = "media")]
    public string[] Media { get; set; }
    [JsonProperty(PropertyName = "hashtags")]
    public string[] HashTags { get; set; }
    [JsonProperty(PropertyName = "urls")]
    public string[] Urls { get; set; }
}

class ATwitterUser
{
    [JsonProperty(PropertyName = "name")]
    public string UserName { get; set; }
    [JsonProperty(PropertyName = "screen_name")]
    public string ScreenName { get; set; }
    [JsonProperty(PropertyName = "protected")]
    public bool IsProtected { get; set; }
    [JsonProperty(PropertyName = "id_str")]
    public string IdStr { get; set; }
    [JsonProperty(PropertyName = "profile_image_url_https")]
    public string ProfileImageUrl { get; set; }
    [JsonProperty(PropertyName = "id")]
    public long Id { get; set; }
    [JsonProperty(PropertyName = "verified")]
    public bool IsVerified { get; set; }
}

但是当我尝试运行它时,我在JsonConvert.DeserializeObject行得到了这个异常:

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ThisNamespace.ATweet' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

1 个答案:

答案 0 :(得分:0)

文件中的每个元素都必须是这样的名称值对。

screen_name : value
profile_image_url_https : value