Skip to content

Instantly share code, notes, and snippets.

@teramako
Created February 27, 2012 07:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teramako/1922351 to your computer and use it in GitHub Desktop.
Save teramako/1922351 to your computer and use it in GitHub Desktop.
キーをソートしてJSON化のサンプル
var o = {
b: "B",
c: "C",
a: "A",
};
print(JSON.stringify(o));
// {"b":"B","c":"C","a":"A"}
Object.prototype.toJSON = function toSortedJSON () {
var o = {}, keys = Object.keys(this).sort();
keys.forEach(function(key){ o[key] = this[key]; }, this);
return o;
};
print(JSON.stringify(o));
// {"a":"A","b":"B","c":"C"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment