将JSON反序列化为c#对象

本文关键字:对象 反序列化 JSON | 更新日期: 2023-09-27 17:50:27

{"facet_counts":{
  "facet_queries":{},
  "facet_fields":{},
  "facet_dates":{},
  "facet_ranges":{
    "createdat":{
      "counts":[
        "2015-05-17T00:00:00Z",155,
        "2015-05-18T00:00:00Z",162,
        "2015-05-19T00:00:00Z",200,
        "2015-05-20T00:00:00Z",218,
        "2015-05-21T00:00:00Z",181,
        "2015-05-22T00:00:00Z",137],
      "gap":"+1DAY",
      "start":"2015-05-17T00:00:00Z",
      "end":"2015-05-23T00:00:00Z"}}}}

我试图将上面的json反序列化到我的对象中,但计数部分没有被反序列化。对象是

public class FacetCounts
{
    public class Facet_Ranges
    {
        public class CreatedAt
        {
            public List<Counts> counts { get; set; }
            public class Counts
            {
                public Dictionary<string, int> count { get; set; }
            }
        }
        public CreatedAt createdat { get; set; }
    }
    public Facet_Ranges facet_ranges { get; set; }
}

我也试着删除public Dictionary<string, int> count { get; set; },但仍然失败。我的反序列化代码是

objObject = JsonConvert.DeserializeObject<FacetCounts>(json);

任何帮助将不胜感激!!

将JSON反序列化为c#对象

public class FacetCounts
{
    public class Facet_Ranges
    {
        public class CreatedAt
        {
            public List<string> counts 
            { 
              get
              {
                return Counts
                  .SelectMany(pair => new[]{pair.Key, pair.Value.ToString()})
                  .ToList();
              }
              set
              {
                var pairs = new Dictionary<string, int>(); 
                for (var i = 0; i < value.Length / 2; ++i)
                {
                  pairs[value[2*i]] = int.Parse(value[2*i+1]);
                }
                this.Counts = pairs;
              }
            }
            [JsonIgnore]
            public Dictionary<string, int> Counts {get;set;}
        }
        public CreatedAt createdat { get; set; }
    }
    public Facet_Ranges facet_ranges { get; set; }
}

您的JSon无效。错误是在完整的JSon字符串{}

周围缺少大括号

这是一个工作的JSon字符串:

{
"facet_counts": {
    "facet_queries": {},
    "facet_fields": {},
    "facet_dates": {},
    "facet_ranges": {
        "createdat": {
            "counts": [
                "2015-05-17T00:00:00Z",
                155,
                "2015-05-18T00:00:00Z",
                162,
                "2015-05-19T00:00:00Z",
                200,
                "2015-05-20T00:00:00Z",
                218,
                "2015-05-21T00:00:00Z",
                181,
                "2015-05-22T00:00:00Z",
                137
            ],
            "gap": "+1DAY",
            "start": "2015-05-17T00:00:00Z",
            "end": "2015-05-23T00:00:00Z"
            }
        }
    }
}

我使用JSon Lint来验证你的JSon,并通过错误修复错误。
我不确定你的c#代码是否正确地反序列化你的JSon,所以我建议你使用例如JSon2CSharp,以便在c#中创建JSon对象。然后你可以修改变量名,等等。

{}包围计数项

"facet_counts":{
    "facet_queries":{},
    "facet_fields":{},
    "facet_dates":{},
    "facet_ranges":{
        "createdat":{
            "counts":[
                {"2015-05-17T00:00:00Z",155},
                {"2015-05-18T00:00:00Z",162},
                {"2015-05-19T00:00:00Z",200},
                {"2015-05-20T00:00:00Z",218},
                {"2015-05-21T00:00:00Z",181},
                {"2015-05-22T00:00:00Z",137}],
        "gap":"+1DAY",
        "start":"2015-05-17T00:00:00Z",
        "end":"2015-05-23T00:00:00Z"}}}

这是无效的json -你的计数对象需要在{大括号}。

供参考-你也可以使用json2csharp.com来自动生成c-sharp对象来反序列化,这也会突出这样的问题

From JSON2CSHARP

public class Createdat
{
    public List<object> counts { get; set; }
    public string gap { get; set; }
    public string start { get; set; }
    public string end { get; set; }
}