【问题标题】:JSON.NET parsing Twitter Archive issueJSON.NET 解析 Twitter 存档问题
【发布时间】:2016-03-18 06:22:05
【问题描述】:

我正在尝试使用 JSON.NET 解析包含您所有推文的 JSON 基本上我需要的只是“文本”和“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.

【问题讨论】:

    标签: c# json twitter


    【解决方案1】:

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

    screen_name : value
    profile_image_url_https : value
    

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2014-01-20
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      相关资源
      最近更新 更多