{"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反序列化为我的对象,但是count部分没有被反序列化。我的目标是
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);
任何帮助都将不胜感激!!
答案 0 :(得分:3)
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; }
}
答案 1 :(得分:3)
您的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对象。然后你可以修复变量名等等。
答案 2 :(得分:1)
使用{}
"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"}}}
答案 3 :(得分:1)
这是无效的json - 你的计数对象需要在{braces}。
仅供参考 - 你也可以使用json2csharp.com自动生成反序列化的c-sharp对象,这也会突出显示这样的问题。
答案 4 :(得分:1)
public class Createdat
{
public List<object> counts { get; set; }
public string gap { get; set; }
public string start { get; set; }
public string end { get; set; }
}
这对我有用