簡體   English   中英

在C#中覆蓋Json屬性-Newtonsoft Json

[英]Overwrite a Json Property in C# - Newtonsoft Json

因此,我正在制作一個小的JSON解析器。

這是我要解析的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文本。

既然你不知道密鑰,使用Dictionary<string, Token>而不是財產TokenNumberPrices

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對象。

您可以使用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; }
}

您可以在這里嘗試。 另外,您可以編寫一個JsonConverter來實現相同的目的,或者執行Amir的建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM