问题描述
我有一个供内部使用的 MVC Web-API 应用程序.我有一些带有表单和数字字段的页面.我需要在德国计算机上安装它,用户将只是德国人.在德国,他们写3,5"而不是3.5"(带逗号).
I have an MVC Web-API application for inner use. I have some pages with forms and numeric fields. I need to install this on a German computer, and the users will be only Germans. In Germany they write "3,5" instead of "3.5" (with a comma).
在 IIS 配置中,文化是不变文化",并且由于计算机是德语 - 本地化是de-DE".
In IIS configuration the culture is "Invariant culture" and since the computer is German - the localize is "de-DE".
当用户在字段中写入3,5"时 - 我可以在 firebug 中看到3,5"是以 JSON 格式发送的,但服务器将其获取为35".
When the users write "3,5" in the field - I can see in firebug that "3,5" is what is sent in JSON, but the server gets it as "35".
我可以在服务器端处理它吗?(我不想更改 json,因为我需要在每个字段和页面中都这样做)
Can I handle it on server-side? (I don't want to change the json because I'll need to do it in every field and page)
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public class ItemsController : ApiController, IDisposable
{
[Authorize(Roles = "Admin")]
[HttpPost]
public HttpResponseMessage UpdateItem(ItemViewModel itemVM)
{
// JSON data sent data.NumProp1 = "3,5"
// itemVM.NumProp1 contains "35" instead of "3.5"
}
}
推荐答案
您不应该本地化您的 JSON - 请参阅 http://www.json.org 用于规范(仅显示点作为分隔符)和 JSON 序列化时如何本地化? 类似的问题.
You should not localize your JSON - see http://www.json.org for the spec (which only shows the dot as a separator) and How to localize when JSON-serializing? for a similar question.
我不建议您尝试阅读您自定义的 JSON - 现在听起来可能很快就赢了,但最终您根本没有使用 JSON.
I wouldn't recommend trying to read your customized JSON - it may sound like a quick win right now, but in the end you simply aren't using JSON.
这篇关于德国文化 - 用逗号从 JSON 中获取双数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!