在C#中覆盖Json属性 - Newtonsoft Json

时间:2018-05-27 04:24:37

标签: c# json json.net

所以我正在制作一个小型的JSON Parser。

这是我要解析的JSON:

{"158023":{"prices":{"xbox":{"LCPrice":"225,000","LCPrice2":"232,000","LCPrice3":"235,000","LCPrice4":"235,000","LCPrice5":"239,000","updated":"15 secs ago","MinPrice":"27,000","MaxPrice":"500,000","PRP":"41"},"ps":{"LCPrice":"228,000","LCPrice2":"231,000","LCPrice3":"232,000","LCPrice4":"233,000","LCPrice5":"235,000","updated":"9 mins ago","MinPrice":"30,000","MaxPrice":"550,000","PRP":"38"},"pc":{"LCPrice":"305,000","LCPrice2":"305,000","LCPrice3":"315,000","LCPrice4":"333,000","LCPrice5":"347,000","updated":"1 hour ago","MinPrice":"37,000","MaxPrice":"700,000","PRP":"40"}}}}

我有以下类来表示Json对象。

public partial class Prices
{
    [JsonProperty("158023")]
    public Token TokenNumber { get; set; }
}

public partial class Token
{
    [JsonProperty("prices")]
    public PricesClass Prices { get; set; }
}

public partial class PricesClass
{
    [JsonProperty("xbox")]
    public Pc Xbox { get; set; }

    [JsonProperty("ps")]
    public Pc Ps { get; set; }

    [JsonProperty("pc")]
    public Pc Pc { get; set; }
}

public partial class Pc
{
    [JsonProperty("LCPrice")]
    public string LcPrice { get; set; }

    [JsonProperty("LCPrice2")]
    public string LcPrice2 { get; set; }

    [JsonProperty("LCPrice3")]
    public string LcPrice3 { get; set; }

    [JsonProperty("LCPrice4")]
    public string LcPrice4 { get; set; }

    [JsonProperty("LCPrice5")]
    public string LcPrice5 { get; set; }

    [JsonProperty("updated")]
    public string Updated { get; set; }

    [JsonProperty("MinPrice")]
    public string MinPrice { get; set; }

    [JsonProperty("MaxPrice")]
    public string MaxPrice { get; set; }

    [JsonProperty("PRP")]
    public string Prp { get; set; }
}

public partial class Prices
{
    public static Prices FromJson(string json) => JsonConvert.DeserializeObject<Prices>(json, PriceConverter.Settings);
}

internal static class PriceConverter
{
    public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
    {
        MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
        DateParseHandling = DateParseHandling.None,
        Converters = {
            new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
        },
    };
}

通过执行以下操作,我可以轻松地解析JSON:

 Prices prices = Prices.FromJson(myJson);

问题是当我想使用不同于158023的数字时。 例如,158025。

Price类上的JsonProperty已设置为“158023”,我不知道如何重命名它。

TLDR: 我有一个JSON对象,我希望在反序列化之前重命名JsonProperty文本。

2 个答案:

答案 0 :(得分:2)

由于您不知道密钥,因此请使用Dictionary<string, Token>而不是类TokenNumber中的属性Prices

public partial class Prices
{
    // Remove this property
    // [JsonProperty("158023")]
    // public Token TokenNumber { get; set; }
}

public partial class Prices
{
    public static Dictionary<string, Token> FromJson(string json) => JsonConvert.DeserializeObject<Dictionary<string, Token>>(json, PriceConverter.Settings);
}

现在结果将是一个字典,其中键是作为字符串的标记值,值是Token对象。

答案 1 :(得分:0)

您可以使用JsonExtensionDataOnDeserialized属性:

public class Wrapper
{
    public string Id { get; set; }

    public Item Item { get; set; }

    [JsonExtensionData]
    private IDictionary<string, JToken> _additionalData;

    [OnDeserialized]
    private void OnDeserialized(StreamingContext context)
    {
        // Get the first key. If you have more than one, you may need
        // to customize this for your use case
        var id = _additionalData.Keys.FirstOrDefault();
        if (id != null)
        {
            // Assign to our Id property
            this.Id = id;

            // Create a reader for the subobject
            var itemReader = _additionalData[id].CreateReader();
            var serializer = new JsonSerializer();

            // Deserialize the subobject into our Item property
            this.Item = serializer.Deserialize<Item>(itemReader);
            itemReader.Close();
        }
    }
}

public class Item
{
    public string Name { get; set; }
}

你可以尝试here。或者你可以写一个JsonConverter来实现同样的目的,或者做Amir所建议的。