Json反序列化动态在.NET Core 3.1中不起作用



动态对象的反序列化,该对象在.NET Core 2.2中运行良好,但在.NET Core 3.1中不起作用。

我使用的是Newtonsoft.Jsonnuget软件包v12.0.3版本。这是一个ASP.NET核心MVC项目。

public IActionResult Apply([FromBody]dynamic postData)
{
string serverPath = _hostingEnvironment.WebRootPath;
List<ModelData> list = new List<ModelData>();
foreach (var item in JsonConvert.DeserializeObject(postData))
{
list.Add(new ModelData
{
Key = item.Key,
Value = item.Value
});
}
}

我需要帮助修复

输入数据:

ValueKind = String : "[{'Key':'Table','Value':'admins'},{'Key':'MethodValue','Value':'Update'},{'Key':'Page','Value':'1'},{'Key':'Id','Value':'1'},{'Key':'ImgPath','Value':''},{'Key':'Id','Value':'1'},{'Key':'Login','Value':'admin'},{'Key':'Password','Value':'12345678999'}]"

这就是我使用调试器看到的

ModelData输出

public struct ModelData
{
public string Key { get; set; }
public string Value { get; set; }
}

来自客户端页面的代码

function SendFormData(fileName) {
var jsonArr = "[{'Key':'Table','Value':'@ViewBag.Table'},{'Key':'MethodValue','Value':'@ViewBag.MethodValue'}," +
"{'Key':'Page','Value':'@ViewBag.Page'},{'Key':'Id','Value':'@ViewBag.Id'},";
jsonArr += "{'Key':'ImgPath','Value':'" + fileName + "'},";
var other_data = $("#ApplyForm").serializeArray();
$.each(other_data, function (_key, input) {
if (input.name === "Status") {
return true;
}
jsonArr += "{'Key':'" + input.name + "','Value':'" + input.value + "'},";
});
jsonArr = jsonArr.slice(0, -1);
jsonArr += "]";
$.ajax({
url: "/Admin/Apply",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(jsonArr),
async: false,
success: function (returnUrl) {
AjaxLoad(returnUrl);
},
error: function () {
console.log("@Translations.UpsErrorMessage");
}
});
}

客户端似乎无法发送有效的postData(没有唯一的(,并且不需要字符串化任何字符串变量,除非它是如下数组:

function SendFormData(fileName) {
var jsonArr = [{'Key':'Table',''Value':'@ViewBag.Table'},
{'Key':'MethodValue','Value':'@ViewBag.MethodValue'},
{'Key':'Page','Value':'@ViewBag.Page'},
{'Key':'ImgPath','Value':fileName}],
other_data = $("#ApplyForm").serializeArray();
$.each(other_data, function (_key, input) {
if (input.name === "Status") {
return true;
}
jsonArr.push({Key: input.name, Value: input.value});
});
$.ajax({
url: "/Admin/Apply",
type: "POST",
contentType: 'application/json; charset=utf-8',
data: {postData:JSON.stringify(jsonArr)},
async: false,
success: function (returnUrl) {
AjaxLoad(returnUrl);
},
error: function () {
console.log("@Translations.UpsErrorMessage");
}
});
}

相关内容

  • 没有找到相关文章

最新更新