将 JSON 反序列化为 C# 对象

Deserialize JSON to c# object

{"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);

如有任何帮助,我们将不胜感激!!

{}

包围计数中的每个项目
"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 - 您的计数对象需要在 {braces} 中。

仅供参考 - 您还可以使用 json2csharp.com 自动生成要反序列化的 c-sharp 对象,这也会突出显示此类问题。

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; }
}

来自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; }
}

这对我有用

您的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 个对象。然后就可以修改变量名等