Json.NET - 自定义转换器 - 字符串到Int

时间:2014-01-14 23:42:00

标签: c# json.net

我遇到了关于Json.NET和omdbapi的问题。我正在尝试从omdbapi中检索信息,并且一些属性给我带来了麻烦,特别是“imdbVotes”,因为它的编写,例如,“321,364”所以我无法从中获得整数。

我打赌我需要一个自定义转换器,但我担心,此刻,我真的不明白如何为我的特定问题创建一个。

所有其他属性都运行良好(我目前没有使用所有这些属性)。

这是响应,比如说Snatch:http://www.omdbapi.com/?i=&t=snatch

这是我的班级:

public class MovieJSON
{
    [JsonProperty(PropertyName = "Title")]
    public String Title { get; set; }

    [JsonProperty(PropertyName = "Year")]
    public int Year { get; set; }

    [JsonProperty(PropertyName = "Genre")]
    public String Genre { get; set; }

    [JsonProperty(PropertyName = "Director")]
    public String Director { get; set; }

    [JsonProperty(PropertyName = "Actors")]
    public String Actors { get; set; }

    [JsonProperty(PropertyName = "Plot")]
    public String Plot { get; set; }

    [JsonProperty(PropertyName = "Poster")]
    public String Poster { get; set; }

    [JsonProperty(PropertyName = "Metascore")]
    public int Metascore { get; set; }

    [JsonProperty(PropertyName = "imdbRating")]
    public decimal ImdbRating { get; set; }

    [JsonProperty(PropertyName = "imdbVotes")]
    public int ImdbVotes { get; set; }
}

更新#1:

当属性值为“N / A”时,如何处理响应?对于某些电影来说会发生这种情况(即。http://www.omdbapi.com/?i=&t=four+rooms将Metascore设置为N / A)。

更新#2:

另一项相关调查。我正在使用EF6和MySQL,并通过JSON解析创建电影来填充数据库。

这是我的电影课程:

[JsonObject(MemberSerialization.OptIn)]
[Table("movies")]
public class MovieJSON
{
    [Key]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "Title")]
    [Column("title")]
    public String Title { get; set; }

    [JsonProperty(PropertyName = "Year")]
    [Column("year")]
    public int Year { get; set; }

    [JsonProperty(PropertyName = "Genre")]
    [Column("genre")]
    public String Genre { get; set; }

    [JsonProperty(PropertyName = "Director")]
    [Column("director")]
    public String Director { get; set; }

    [JsonProperty(PropertyName = "Actors")]
    [Column("actors")]
    public String Actors { get; set; }

    [JsonProperty(PropertyName = "Plot")]
    [Column("plot")]
    public String Plot { get; set; }

    [JsonProperty(PropertyName = "Poster")]
    [Column("poster")]
    public String Poster { get; set; }

    [JsonProperty(PropertyName = "Metascore")]
    public String Metascore { get; set; }

    [Column("metascore")]
    public int MetascoreInt
    {
        get
        {
            int result;
            if (int.TryParse(Metascore, NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbRating")]
    public String ImdbRating { get; set; }

    [Column("imdb_rating")]
    public Decimal ImdbRatingDecimal
    {
        get
        {
            Decimal result;
            if (Decimal.TryParse(ImdbRating, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbVotes")]
    public String ImdbVotes { get; set; }

    [Column("imdb_votes")]
    public long ImdbVotesLong
    {
        get
        {
            long result;
            String stringToParse = ImdbVotes.Remove(ImdbVotes.IndexOf(','), 1);

            if (long.TryParse(stringToParse, out result))
                return result;
            return 0;
        }
    }

    [JsonProperty(PropertyName = "imdbID")]
    [Column("imdb_id")]
    public String ImdbID { get; set; }

    [JsonProperty(PropertyName = "type")]
    [Column("type")]
    public String Type { get; set; }

    public override string ToString()
    {
        String[] propertiesToIgnore = {"MetascoreInt", "ImdbRatingDecimal", "ImdbVotesLong"};
        var sb = new StringBuilder();

        PropertyInfo[] properties = GetType().GetProperties();

        foreach (PropertyInfo propertyInfo in properties)
        {
            if (propertiesToIgnore.Contains(propertyInfo.Name))
                continue;

            sb.AppendLine(String.Format("{0} : {1} ",
                propertyInfo.Name, propertyInfo.GetValue(this, null)));
        }

        return sb.ToString();
    }
}

这是我的EF6配置上下文类(我忽略了String字段,而是使用Helper,因为数据库配置为接受Metascore的int等等):

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MovieJSON>().Ignore(e => e.Metascore).Ignore(e => e.ImdbRating).Ignore(e => e.ImdbVotes);
        base.OnModelCreating(modelBuilder);
    }

其他图片信息:

插入数据库之前的对象值(正确设置所有值)

Valid XHTML http://imagizer.imageshack.us/v2/800x600q90/689/8x5m.png

数据库中的值:

Valid XHTML http://imagizer.imageshack.us/v2/800x600q90/844/nvc5.png

辅助字段(MetascoreInt,ImdbRatingDecimal,ImdbVotesLong)返回零,我无法弄清楚原因。

任何帮助都会非常感激! :)

一切顺利

1 个答案:

答案 0 :(得分:3)

你可以有两个属性:一个是来自IMDB的字符串属性,另一个是转换字符串一的int属性。要进行转换,您可以使用漂亮的NumberStyles.AllowThousands标志。所以你会有

[JsonProperty(PropertyName = "imdbVotes")]
public string ImdbVotes { get; set; }

public int ImdbVotesInt 
{ 
    get 
    {
        int result;
        if (int.TryParse(ImdbVotes, 
                NumberStyles.AllowThousands,
                CultureInfo.InvariantCulture, 
                out result))                    
            return result;  // parse is successful, use 'result'
        else
            return 0;       // parse is unsuccessful, return whatever default value works for you       
    }
}