Reputation: 3498
{"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; }
but still it fails.
My deserialize code is
objObject = JsonConvert.DeserializeObject<FacetCounts>(json);
Any help would be appreciated !!
Upvotes: 2
Views: 671
Reputation: 309
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 e.g. JSon2CSharp in order to create you JSon objects in C#. Then you can fix variable names, etc.
Upvotes: 3
Reputation: 3498
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
Upvotes: 1
Reputation: 16981
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; }
}
Upvotes: 3
Reputation: 1185
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.
Upvotes: 1
Reputation: 9551
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"}}}
Upvotes: 1