有很多
关于我的问题的例子,但我无法在这里找到我的问题。我当时阅读了大部分内容,我看到问题与将 Json 绑定到模型有关。
我有以下 Json 字符串:[已编辑]
{
answered: 88983,
total: 88983,
tma: "74.0",
tme: "7.0",
total_condos: 71,
byday: {
answerbyday: [
{
day: "2018-2-1",
total: 3242,
tme: "5.0",
tma: "75.0"
},
{
day: "2018-2-2",
total: 3814,
tme: "8.0",
tma: "74.0"
},
{
day: "2018-2-3",
total: 3157,
tme: "5.0",
tma: "67.0"
}
]
},
condos: [
{
condo: "2000",
name: "2000 - PORTER CUIABA",
total: 1155,
answered: 1155,
tma: "50.0",
tme: "7.0"
},
{
condo: "5010",
name: "5010 - COND PASSAREDO",
total: 1347,
answered: 1347,
tma: "80.0",
tme: "7.0"
},
{
condo: "5020",
name: "5020 - COND OURO PRETO",
total: 241,
answered: 241,
tma: "61.0",
tme: "7.0"
}
]
}
已恢复的 JSON
{
answered: 88983,
total: 88983,
tma: "74.0",
tme: "7.0",
total_condos: 71,
byday: {
answerbyday: []
},
condos: []
}
我的模型如下所示:
public class GroupbyDay
{
public int answered { get; set; }
public int total { get; set; }
public double tma { get; set; }
public double tme { get; set; }
public int total_condos { get; set; }
public byday byday { get; set; }
public List<condos> condos { get; set; }
}
public class byday
{
public List<answerbyday> answerbyday { get; set; }
}
public class answerbyday
{
public string day { get; set; }
public int total { get; set; }
public double tme { get; set; }
public double tma { get; set; }
}
public class condos
{
public string condo { get; set; }
public string name { get; set; }
public int total { get; set; }
public int answered { get; set; }
public double tme { get; set; }
public double tma { get; set; }
}
在我的控制器上,我调用该方法:
string URL1 = "http://" + server + "/report/calls/synthetic/agents?from=" + data1 + "&to=" + data2 + "&groupby=day";
var webRequest1 = WebRequest.Create(URL1);
if (webRequest1 != null)
{
webRequest1.Method = "GET";
webRequest1.Timeout = 300000;
webRequest1.ContentType = "application/json";
using (var s = webRequest1.GetResponse().GetResponseStream())
{
using (var sr = new System.IO.StreamReader(s))
{
var lista = JsonConvert.DeserializeObject<GroupbyDay>(sr.ReadToEnd());
return Json(lista, JsonRequestBehavior.AllowGet);
}
}
}
我在这里错过了什么?我已经查看了所有代码,找不到我做错了什么。
编辑:我发送了错误的 JSON。
通过在线转换器 json2csharp.com 运行您的 JSON,我得到..
public class Answerbyday
{
public string day { get; set; }
public int total { get; set; }
public string tme { get; set; }
public string tma { get; set; }
}
public class Byday
{
public List<Answerbyday> answerbyday { get; set; }
}
public class Condo
{
public string condo { get; set; }
public string name { get; set; }
public int total { get; set; }
public int answered { get; set; }
public string tma { get; set; }
public string tme { get; set; }
}
public class GroupbyDay // RootObject
{
public int answered { get; set; }
public int total { get; set; }
public string tma { get; set; }
public string tme { get; set; }
public int total_condos { get; set; }
public Byday byday { get; set; }
public List<Condo> condos { get; set; }
}
查看Microsoft的文章 JSON 和 XML 序列化 ASP.NET Web API 和 Newtonsoft:序列化属性