【问题标题】:Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Model'无法将当前 JSON 数组(例如 [1,2,3])反序列化为“模型”类型
【发布时间】:2018-03-06 04:28:54
【问题描述】:

有很多关于我的问题的例子,但我无法在这里找到我的问题。我阅读了大部分内容,发现问题与将 Json 绑定到模型有关。

我有以下 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 错误。

【问题讨论】:

  • 好吧,您的“byday”属性作为对象打开并作为数组关闭。不知道是不是错别字,但先检查那里
  • 忽略第一个 { for byday 上的错字,JSON 似乎将它作为一个数组,但在您的模型声明中,您有一种类型的 byday,它具有 answerbyday 的属性,它是一个集合.这与只有一个集合(数组)的 JSON(即对象内的集合)不匹配
  • 要绑定到您的模型,它需要是byday: { answerbyday: [ { day: "2018-2-1", ... }, { day: "2018-2-2", ... } ] }
  • 对不起,我第一次发送了错误的 JSON,我编辑更正了。谢谢

标签: c# asp.net-mvc json.net


【解决方案1】:

通过在线转换器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

【讨论】:

  • 嘿亚当,我的问题是关于对象顺序的?
  • @RogerioAzevedo 实际上,通过序列化,您需要正确地获取依赖项(如您所说的“对象顺序”)。同样,这里是区分大小写的东西,这就是为什么您要查看数据注释对序列化的意义。用关于序列化的好文章更新了答案..
  • 我尝试改变我的模型,但得到同样的错误:Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Sistema.Model.GroupbyDay ' 因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。
  • 请用最新型号更新您的答案。并更新您发布的 JSON。关于“JSON RESUMED”的部分没有意义。您是否将 JSON 粘贴到 json2csharp.com
  • 我使用了 json2csharp.com 并进行了更改。现在可以正常工作了谢谢!
猜你喜欢
  • 2013-07-19
  • 2019-03-20
  • 2018-12-30
  • 2020-12-05
  • 2022-01-02
  • 2014-03-20
相关资源
最近更新 更多