Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
覆盖C#-Newtonsoft Json中的Json属性_C#_Json_Json.net - Fatal编程技术网

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

覆盖C#-Newtonsoft Json中的Json属性,c#,json,json.net,C#,Json,Json.net,因此,我正在制作一个小型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":{"

因此,我正在制作一个小型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 }
        },
    };
}
问题是当我想使用不同于158023的号码时。 例如,158025

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

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

由于您不知道密钥,请使用
字典
而不是class
Prices
中的属性
TokenNumber

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);
}
公共部分类价格
{
//删除此属性
//[JsonProperty(“158023”)]
//公共令牌令牌编号{get;set;}
}
公共部分类价格
{
公共静态字典FromJson(string json)=>JsonConvert.DeserializeObject(json,PriceConverter.Settings);
}

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

由于您不知道键,请使用
字典
而不是class
Prices
中的属性
TokenNumber

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);
}
公共部分类价格
{
//删除此属性
//[JsonProperty(“158023”)]
//公共令牌令牌编号{get;set;}
}
公共部分类价格
{
公共静态字典FromJson(string json)=>JsonConvert.DeserializeObject(json,PriceConverter.Settings);
}

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

您可以使用
JsonExtensionData
OnDeserialized
属性:

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; }
}
公共类包装器
{
公共字符串Id{get;set;}
公共项项{get;set;}
[JsonExtensionData]
专用词典_附加数据;
[已序列化]
私有void已序列化(StreamingContext上下文)
{
//获取第一个密钥。如果您有多个密钥,则可能需要
//为您的用例定制此选项
var id=_additionalData.Keys.FirstOrDefault();
如果(id!=null)
{
//分配给我们的Id属性
这个.Id=Id;
//为子对象创建读取器
var itemReader=_additionalData[id].CreateReader();
var serializer=new JsonSerializer();
//将子对象反序列化为我们的Item属性
this.Item=serializer.Deserialize(itemReader);
itemReader.Close();
}
}
}
公共类项目
{
公共字符串名称{get;set;}
}

你可以试试。或者,您可以编写一个
JsonConverter
来实现相同的功能,或者按照阿米尔的建议执行。

您可以使用
JsonExtensionData
OnDeserialized
属性:

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; }
}
公共类包装器
{
公共字符串Id{get;set;}
公共项项{get;set;}
[JsonExtensionData]
专用词典_附加数据;
[已序列化]
私有void已序列化(StreamingContext上下文)
{
//获取第一个密钥。如果您有多个密钥,则可能需要
//为您的用例定制此选项
var id=_additionalData.Keys.FirstOrDefault();
如果(id!=null)
{
//分配给我们的Id属性
这个.Id=Id;
//为子对象创建读取器
var itemReader=_additionalData[id].CreateReader();
var serializer=new JsonSerializer();
//将子对象反序列化为我们的Item属性
this.Item=serializer.Deserialize(itemReader);
itemReader.Close();
}
}
}
公共类项目
{
公共字符串名称{get;set;}
}

你可以试试。或者,您可以编写一个
JsonConverter
来实现同样的功能,或者按照阿米尔的建议去做。

非常感谢。非常好,谢谢。非常好。谢谢你的建议,但我会用阿米尔的建议,因为我能更好地理解它。谢谢,但我会用阿米尔的建议,因为我能更好地理解它