无法将当前JSON数组(例如[1,2,3])反序列化为“模型”类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法将当前JSON数组(例如[1,2,3])反序列化为“模型”类型相关的知识,希望对你有一定的参考价值。

有很多关于我的问题的例子,但我无法在这里找到我的问题。我读了大部分时间,我看到问题与Bind Json与Model有关。

我有以下Json字符串:[EDITED]

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

看看微软的文章JSON and XML Serialization in ASP.NET Web API,以及来自Newtonsoft的文章:Serialization Attributes

以上是关于无法将当前JSON数组(例如[1,2,3])反序列化为“模型”类型的主要内容,如果未能解决你的问题,请参考以下文章

无法将当前 JSON 数组(例如 [1,2,3])反序列化为具有复杂和嵌套对象的类型

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“ConsoleAppTest01.Location”,因为该类型需要 JSON 对象

无法将当前 JSON 数组(例如 [1,2,3])反序列化为“System.Collections.Generic.Dictionary”类型

无法将当前JSON数组(例如[1,2,3])反序列化为“模型”类型

无法将当前 JSON 对象(例如 "name":"value")反序列化为类型“Value[]”,因为该类型需要 JSON 数组(例如 [1,2,3])

无法将 JSON 数组(例如 [1,2,3])反序列化为类型 ' ',因为类型需要 JSON 对象(例如 "name":"value")才能正确反序列化