Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

c# - German culture - get double number from JSON with a comma

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).

In IIS configuration the culture is "Invariant culture" and since the computer is German - the localize is "de-DE".

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".

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"
        }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...