反序列化JSON字符串以在C#中使用不同的属性名称键入

时间:2017-09-21 19:46:42

标签: c# json

我有一个JSON对象,它是一个看起来像这样的字符串数组。它位于一个名为“researchTerms”的变量中:

[{"1":"ifn","2":7,"3":1.81818181818182},{"1":"macrophages","2":5,"3":1.2987012987013},{"1":"n =","2":5,"3":1.2987012987013},{"1":"p <","2":5,"3":1.2987012987013},{"1":"technique","2":5,"3":1.2987012987013},{"1":"cells","2":4,"3":1.03896103896104}]

如何在C#中使用Newtonsoft将其反序列化为一个ResearchTerms数组,其中属性“1”为Term,属性“2”为Count,属性“3”为Score?:

public class ResearchTerm
{
    public string Term { get; set; }
    public int Count { get; set; }
    public long Score { get; set; }
} 

1 个答案:

答案 0 :(得分:2)

如果您使用的是Json.Net,则可以使用属性 JsonProperty

public class ResearchTerm
{
    [JsonProperty("1")]
    public string Term { get; set; }
    [JsonProperty("2")]
    public int Count { get; set; }
    [JsonProperty("3")]
    public decimal Score { get; set; }
}