首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法将当前JSON数组(例如[1,2,3])反序列化为“Model”类型

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

Stack Overflow用户
提问于 2018-03-06 04:28:54
回答 1查看 227关注 0票数 0

关于我的问题有很多例子,但我在这里找不到我的问题。我读了大部分时间,我发现问题与绑定Json到Model有关。

我有以下Json字符串:已编辑的

代码语言:javascript
复制
{
 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继续

代码语言:javascript
复制
{
answered: 88983,
total: 88983,
tma: "74.0",
tme: "7.0",
total_condos: 71,
byday: {
answerbyday: []
},
condos: []
}

我的模型如下:

代码语言:javascript
复制
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; }
}

在我的控制器上,我调用该方法:

代码语言:javascript
复制
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发错了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-06 05:40:00

通过在线转换器json2csharp.com运行您的JSON,我得到..。

代码语言:javascript
复制
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; }
}

看看微软的文章ASP.NET Web中的JSON和XML序列化,来自Newtonsoft:序列化属性

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49123397

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档