我试图将json反序列化为List<struct>
- 但我的对象始终为null。源JSON中的数据类型似乎是正确的,我的代码不会抛出任何错误,所以我完全不知道为什么它不起作用。
我正在学习的课程:
public class SystemStatusRules
{
public struct EmailItem
{
public int EmailSubject_Id;
public bool IsSubject;
public string Email_String;
public int StatusCode;
}
public List<EmailItem> EmailItems {get; set;}
}
我的JSON:
{"EmailItems":[
{"EmailSubject_Id":16,"IsSubject":true,"Email_String":"Cube Build Successful","StatusCode":0},
{"EmailSubject_Id":17,"IsSubject":true,"Email_String":"Cube Build Failure","StatusCode":0}
]}
最后,我的反序列化代码:
[HttpPost]
public bool SetAllStatusRules(string ruleList )
{
SystemStatusRules subjectRules =
JsonConvert.DeserializeObject<SystemStatusRules>(ruleList);
}
万一你需要它,我的序列化代码:
objArray.push({ EmailSubject_Id: SubjectId, IsSubject: IsSubject,
Email_String: EmailSubject, StatusCode: status });
objArray = { EmailItems: objArray };
var rulesJSON = JSON.stringify(objArray);
$.ajax({
url: "Management/SetAllStatusRules",
type: 'POST',
data: 'ruleList='+rulesJSON,
success: function (result) {
MyResultFunction(result);
}
});
修改
这与我投射到一个物体有关。以下代码具有正确填充的subjectRules变量。但是ssr为空。
object subjectRules = JsonConvert.DeserializeObject<SystemStatusRules>(StatusRules);
SystemStatusRules ssr = (SystemStatusRules)subjectRules;
我不知道出了什么问题,但是反序列化为var允许我使用带有数据的对象作为解决方法。
答案 0 :(得分:3)
这是a working fiddle for you。 修改:您不需要[FromBody]
。它只是有效。您的代码显然没有任何问题。
<html lang="en">
<head></head>
<body>
<div class="container"></div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var container = $(".container");
var objArray = [];
objArray.push({
EmailSubject_Id: "0",
IsSubject: "false",
Email_String: "My Email",
StatusCode: "0" });
objArray = { EmailItems: objArray };
var rulesJSON = JSON.stringify(objArray);
$.ajax({
url: '@Url.RouteUrl(new{ action="SetAllStatusRules", controller="Home"})',
type: 'POST',
data: 'ruleList='+rulesJSON
})
.done(function (d) { container.html(d); } );
});
</script>
</body>
</html>
using System.Web.Mvc;
using Newtonsoft.Json;
namespace HelloWorldMvcApp
{
public class HomeController : Controller
{
[System.Web.Mvc.HttpGetAttribute]
public ActionResult Index()
{
return View();
}
[System.Web.Mvc.HttpPost]
public string SetAllStatusRules(string ruleList)
{
SystemStatusRules subjectRules =
JsonConvert.DeserializeObject<SystemStatusRules>(ruleList);
return subjectRules.EmailItems[0].Email_String;
}
}
}
using System.Collections.Generic;
namespace HelloWorldMvcApp
{
public class SystemStatusRules
{
public struct EmailItem
{
public int EmailSubject_Id;
public bool IsSubject;
public string Email_String;
public int StatusCode;
}
public List<EmailItem> EmailItems { get; set; }
}
}
它似乎与您的JavaScript POST及其提供给服务器的有效负载有关。那是因为以下工作(see fiddle):
public class Program
{
public static void Main()
{
var json = @"
{'EmailItems':[
{'EmailSubject_Id':16,'IsSubject':true,'Email_String':'Cube Build Successful','StatusCode':0},
{'EmailSubject_Id':17,'IsSubject':true,'Email_String':'Cube Build Failure','StatusCode':0}
]}";
var p = new Program();
p.SetAllStatusRules(json);
}
public bool SetAllStatusRules(string ruleList)
{
SystemStatusRules subjectRules = JsonConvert.DeserializeObject<SystemStatusRules>(ruleList);
Console.WriteLine(subjectRules.EmailItems.Count.ToString());
Console.WriteLine(subjectRules.EmailItems[0].Email_String);
return true;
}
}
public class SystemStatusRules
{
public struct EmailItem
{
public int EmailSubject_Id;
public bool IsSubject;
public string Email_String;
public int StatusCode;
}
public List<EmailItem> EmailItems { get; set; }
}