长解析不正确的JSON响应ASP.NET MVC

编程入门 行业动态 更新时间:2024-10-25 22:29:04
本文介绍了长解析不正确的JSON响应ASP.NET MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我定义了一个类:

public class custom_field { public long custom_field_type_id { get; set; } }

在我的控制器中,我有以下称为方法的Ajax:

In my controller I have the following Ajax called method:

[HttpPost] public JsonResult CustomFieldEdit(long hash) { var respObj = new custom_field(); respObj.custom_field_type_id = -8454757700450211158L; return Json(respObj); }

我的调用CustomFieldEdit的jQuery

My jQuery that calls CustomFieldEdit

var baseUrl = '@Url.Action("CustomFieldEdit")?hash=' + customFieldId; $.ajax({ type: "POST", url: baseUrl, contentType: "application/json", data: JSON.stringify({ hash: customFieldId }), error: function (xhr, status, error) { toastr.error("Error saving, please try again."); }, success: function (data) { console.log(data.custom_field_type_id); //error here! val = -8454757700450211000 } });

因此控制器中的long值是-8454757700450211158,但解析为JSON的值是-8454757700450211000.

So the long value in the controller is -8454757700450211158 but the value parsed to JSON is -8454757700450211000.

我知道我可以通过将custom_field_type_id更改为字符串或使用具有字符串属性的JSON DTO(用于长属性)来解决此问题,但是我想知道另一种解决此问题的方法,例如Newtonsoft JSON序列化程序设置. >

I know I can fix this by changing custom_field_type_id to a string or creating a JSON DTO with a string property for long properties but I would like to know another way for fixing this if possible, like a Newtonsoft JSON serializer setting.

推荐答案

我知道Javascript数字类型被限制为2 ^ 53(请参见例如 cdivilly.wordpress/2012/04/11/json-javascript-large-64-bit-integers/),因此您可能会遇到该问题.为了避免遇到此问题,我倾向于不将序列号序列化给Json,所以我建议切换.

I know Javascript Number type is limited to 2^53 (see e.g. cdivilly.wordpress/2012/04/11/json-javascript-large-64-bit-integers/) so you could be running into that issue. I tend to not serialize longs to Json to avoid running into this issue, so I'd recommend switching.

更多推荐

长解析不正确的JSON响应ASP.NET MVC

本文发布于:2023-11-12 09:37:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581134.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正确   JSON   ASP   NET   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!