{"id":3783,"date":"2014-10-08T11:34:57","date_gmt":"2014-10-08T02:34:57","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=3783"},"modified":"2014-10-08T11:42:54","modified_gmt":"2014-10-08T02:42:54","slug":"post-3783","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/javascript\/3783","title":{"rendered":"JSON.stringify\u306e\u7b2c\uff12\u5f15\u6570\u3092\u4f7f\u3063\u3066\u51fa\u529b\u7d50\u679c(JSON)\u3092\u6574\u5f62\u3059\u308b"},"content":{"rendered":"<blockquote><p><a href=\"https:\/\/developer.mozilla.org\/ja\/docs\/JavaScript\/Reference\/Global_Objects\/JSON\/stringify\">JSON.stringify() <\/a>\u30e1\u30bd\u30c3\u30c9\u306f\u5024\u3092 JSON \u306b\u5909\u63db\u3057\u307e\u3059\u3002\u7f6e\u304d\u63db\u3048\u95a2\u6570\u3092\u6307\u5b9a\u3057\u3066\u5024\u3092\u7f6e\u304d\u63db\u3048\u305f\u308a\u3001\u7f6e\u304d\u63db\u3048\u914d\u5217\u3092\u6307\u5b9a\u3057\u3066\u6307\u5b9a\u3055\u308c\u305f\u30d7\u30ed\u30d1\u30c6\u30a3\u306e\u307f\u3092\u542b\u3080\u3088\u3046\u306b\u3057\u305f\u308a\u3057\u307e\u3059\u3002<\/p><\/blockquote>\n<p>JavaScript\u3067\u5024\u3092JSON\u306b\u5909\u63db\u3059\u308b\u306e\u306b\u3068\u3066\u3082\u4fbf\u5229\u306a\u30e1\u30bd\u30c3\u30c9\u3067\u3059\u304c\u3001\u7d30\u304b\u3044\u4f7f\u3044\u65b9\u3092\u3057\u3089\u306a\u304b\u3063\u305f\u306e\u3067\u8abf\u3079\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<h3>\u30a4\u30f3\u30c7\u30f3\u30c8\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>stringify\u30e1\u30bd\u30c3\u30c9\u306f\u4e09\u3064\u306e\u5f15\u6570\u3092\u53d7\u3051\u53d6\u308a\u307e\u3059\u304c\u30013\u756a\u76ee\u306e\u5f15\u6570\u306b\u7a7a\u767d\u6587\u5b57\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u3067\u51fa\u529b\u7d50\u679c\u306e\u30a4\u30f3\u30c7\u30f3\u30c8\u3092\u4ed8\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u308f\u308a\u3068\u3088\u304f\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js decode:true \" >var a = {\"number\":100, \"string\":\"hoge\", \"date\":new Date, \"array\":[10,20,30], \"object\":{\"hellow\": \"world\"}, \"function\":function(){ alert(\"test\") } };\r\n\r\nJSON.stringify(a);\r\n-&gt; '{\"number\":100,\"string\":\"hoge\",\"date\":\"2014-10-08T02:04:02.416Z\",\"array\":[10,20,30],\"object\":{\"hellow\":\"world\"}}'\r\n\r\n\/\/\u30bf\u30d6\u3067\u30a4\u30f3\u30c7\u30f3\u30c8\r\nJSON.stringify(a, null , \"\\t\");\r\n-&gt;\r\n'{\r\n\t\"number\": 100,\r\n\t\"string\": \"hoge\",\r\n\t\"date\": \"2014-10-08T02:04:02.416Z\",\r\n\t\"array\": [\r\n\t\t10,\r\n\t\t20,\r\n\t\t30\r\n\t],\r\n\t\"object\": {\r\n\t\t\"hellow\": \"world\"\r\n\t}\r\n}'<\/pre>\n<p>\u3061\u306a\u307f\u306bstringify\u30e1\u30bd\u30c3\u30c9\u3067\u306ffunction\u306f\u51fa\u529b\u3055\u308c\u307e\u305b\u3093\u3002<\/p>\n<h3>replacer\u3092\u4f7f\u3046<\/h3>\n<p>\u4eca\u56de\u306e\u30e1\u30a4\u30f3\u3002\u307b\u3068\u3093\u3069\u4f7f\u3063\u305f\u3053\u3068\u304c\u3042\u308a\u307e\u305b\u3093\u3002<br \/>\nstringify\u30e1\u30bd\u30c3\u30c9\u306e2\u756a\u76ee\u306e\u5f15\u6570\u306b\u306freplacer\u3068\u547c\u3070\u308c\u308b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u3092\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002replacer\u306b\u306f\u30ad\u30fc\u3068\u5024\u304c\u5f15\u6570\u3068\u3057\u3066\u53d7\u3051\u6e21\u3055\u308c\u308b\u306e\u3067\u3001\u305d\u308c\u3089\u3092\u4f7f\u3063\u3066\u51fa\u529b\u3055\u308c\u308bJSON\u3092\u6574\u5f62\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<br \/>\n\u4e0b\u8a18\u306f\u3001date\u306e\u5024\u3092<a href=\"http:\/\/momentjs.com\/\">moment.js<\/a>\u3092\u4f7f\u3063\u3066\u6574\u5f62\u3057JSON\u3068\u3057\u3066\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:5,15 decode:true \" >var a = {\"number\":100, \"string\":\"hoge\", \"date\":new Date, \"array\":[10,20,30], \"object\":{\"hellow\": \"world\"}, \"function\":function(){ alert(\"test\") } };\r\n\r\nvar res = JSON.stringify(a, function(key, val){\r\n\tif (key === \"date\") {\r\n\t\treturn moment(new Date(val)).format('YYYY\/MM\/DD h:mm:ss');\r\n\t}\r\n\treturn val;\t\r\n}, \"\\t\");\r\n\r\nconsole.log(res);\r\n-&gt;\r\n'{\r\n\t\"number\": 100,\r\n\t\"string\": \"hoge\",\r\n\t\"date\": \"2014\/10\/08 11:14:21\", \/\/\u6574\u5f62\u3055\u308c\u305f\u5024\r\n\t\"array\": [\r\n\t\t10,\r\n\t\t20,\r\n\t\t30\r\n\t],\r\n\t\"object\": {\r\n\t\t\"hellow\": \"world\"\r\n\t}\r\n}'<\/pre>\n<p>\u2026\u2026\u3076\u3063\u3061\u3083\u3051\u3001\u3042\u307e\u308a\u4f7f\u3044\u3069\u3053\u308d\u304c\u306a\u3044\u6c17\u304c\u3057\u307e\u3059\u3002<br \/>\n\u3061\u306a\u307f\u306breplacer\u306e\u4e2d\u3067null\u3084undefined\u3092\u8fd4\u3059\u3068stringify\u30e1\u30bd\u30c3\u30c9\u306e\u623b\u308a\u5024\u306fundefined\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u3001\u5024\u3092\u524a\u3063\u305f\u308a\u524a\u9664\u3057\u305f\u308a\u3059\u308b\u306e\u306b\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002<\/p>\n<p>\u500b\u4eba\u7684\u306b\u3053\u3046\u3044\u3046\u7528\u9014\u4e0d\u660e\u306a\u6a5f\u80fd\u304c\u5927\u597d\u304d\u306a\u306e\u3067\u3001\u306a\u3093\u304b\u9762\u767d\u3044\u4f7f\u3044\u65b9\u304c\u3067\u304d\u306a\u3044\u304b\u7814\u7a76\u4e2d\u3067\u3059\u3002<\/p>\n<h3>\u4f59\u8ac7<\/h3>\n<p>stringify\u30e1\u30bd\u30c3\u30c9\u306f\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5185\u306bfunction\u304c\u3042\u3063\u3066\u3082JSON\u3068\u3057\u3066\u51fa\u529b\u3057\u307e\u305b\u3093\u304c\u3001\u4e0a\u8a18replacer\u3092\u4f7f\u3063\u3066\u6587\u5b57\u5217\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3067\u51fa\u529b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"lang:js mark:24 decode:true \" >var a = {\"number\":100, \"string\":\"hoge\", \"date\":new Date, \"array\":[10,20,30], \"object\":{\"hellow\": \"world\"}, \"function\":function(){ alert(\"test\") } };\r\n\r\nvar res = JSON.stringify(a, function(key, val){\r\n\tif (typeof val === \"function\") {\r\n\t\treturn val.toString();\r\n\t}\r\n  return val;\t\r\n}, \"\\t\");\r\n\t\r\nconsole.log(res);\r\n-&gt;\r\n\\\"{\r\n\t\"number\": 100,\r\n\t\"string\": \"hoge\",\r\n\t\"date\": \"2014-10-08T02:24:40.354Z\",\r\n\t\"array\": [\r\n\t\t10,\r\n\t\t20,\r\n\t\t30\r\n\t],\r\n\t\"object\": {\r\n\t\t\"hellow\": \"world\"\r\n\t},\r\n\t\"function\": \"function (){ alert(\\\"test\\\") }\"\r\n}\\\" <\/pre>\n<p>\u3067\u3082\u3001\u95a2\u6570\u3092json\u3067\u6e21\u3059\u306e\u306f\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u7684\u306b\u554f\u984c\u304c\u3042\u308b\u306e\u3067\u3084\u3081\u307e\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON.stringify() \u30e1\u30bd\u30c3\u30c9\u306f\u5024\u3092 &hellip;<\/p>\n","protected":false},"author":1,"featured_media":3784,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-3783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=3783"}],"version-history":[{"count":0,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/3783\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/3784"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=3783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=3783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=3783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}