简体   繁体   中英

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

I am trying to deserialize the above json into my object but the counts part is not getting deserialized. My object is

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

I also tried removing the public Dictionary<string, int> count { get; set; } public Dictionary<string, int> count { get; set; } public Dictionary<string, int> count { get; set; } but still it fails. My deserialize code is

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

Any help would be appreciated !!

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

Your JSon is invalid. The error is the lack of braces around the complete JSon string {}

Here is a working JSon string:

{
"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"
            }
        }
    }
}

I used JSon Lint for validating your JSon, and fixed error by error.
I am not sure if your C# code is correct to Deserialize your JSon, so I would recommend you to use eg JSon2CSharp in order to create you JSon objects in C#. Then you can fix variable names, etc.

Surround each item in counts with {}

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

That isn't valid json - Your count objects need to be in {braces}.

FYI - You can also use json2csharp.com to auto-generate the c-sharp object to deserialize with, which would also highlight issues like this.

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

This worked for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM