JSON password formatting formats?

Am I right in assuming that I need to NUMBER convert Json-encoded date strings to date objects in my client code?

Coming from C #, I took for granted that this happens automatically, but I think it was .NET.

Is there a built-in mechanism for getting native javascript types from a Json string (for dates, int, etc.)?

Thank.

+5
source share
5 answers

The JSON specification does not define the date data type. It is up to you.

See section A.8: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf

+5
source

JSON . ( .NET) .

+1

ISO Asp.net

- , jQuery, jQuery, ISO Asp.net $.parseJSON().

.

+1

JSON.parse() replacer param.

JSON.stringify(value[, replacer[, space]])

. MDN

, json.

function replacer(key, value) {
  return key == "date1" || key == "date2" ? new Date(value) : value;
}

var obj = {
    date1:"5/21/2012 4:49:17 PM",
    date2:new Date()
}
var jsonString = JSON.stringify(obj);
console.log(jsonString);
var jsonObj = JSON.parse(jsonString, replacer);
console.log(jsonObj);
0

All Articles