åèªèº«ãåã®å¨è¾ºãJSONããã使ãã¾ããã§ããç´°ããç¹ã§ãã£ãããã¹ãããããã¦ãã¾ã(è¦ç¬)ããã®ã¨ã³ããªã¼ã§ãJSONã使ãä¸ã§æ³¨æãã¹ããã¨ï¼ééããããç¹ããã¹ã¦åæãã¾ãã
å 容 å ¼ãã§ãã¯ãªã¹ãï¼
- ä»æ§åå ¸ããèªãã°å®ç§ï¼ã®ã¯ãã ãï¼
- æ°å¤ã®åã«ã¼ããä»ãã¦ã¯ãããªã
- 16é²æ°è¡¨è¨ãç¦æ¢ã ã
- æ°å¤ã®åã«ãã©ã¹ãä»ãã¦ã¯ãããªã
- å°æ°ç¹ããã¯ãã¾ãæ°å¤ã¯ãã¡
- ç¨èªæ³ãéããï¼ããããã£ã¨ã¡ã³ãã¼
- ã¡ã³ãã¼åã«ã¯å¸¸ã«æååã使ã
- 空æåå""ãã¡ã³ãã¼åã«ä½¿ãã
- é åè¦ç´ ã¯ããããªã¨ä¸¦ã¹ãã
- æååãå²ãã«ã¯äºéå¼ç¨ç¬¦ã ã
- æååå ã®ã¨ã¹ã±ã¼ããå¾®å¦ã«éã
- ä»æ§ã«ãªãã¨ã¹ã±ã¼ãã¯æ§æã¨ã©ã¼
- undefinedãNaNãããã¾ãã
- ã©ããã¼ãªãã¸ã§ã¯ãã¯ä½¿ããªãã®ãå
- åã·ã¹ãã ã¨typeofã«é¢ãã注æ
- æå¾ã«
ä»æ§åå ¸ããèªãã°å®ç§ï¼ã®ã¯ãã ãï¼
JSONã¯ãå°ããã¦ç°¡åãªä»æ§ã§ãã次ã®Webãã¼ã¸ï¼å°å·ãã¦2ãã¼ã¸ãããï¼ã«ãã¹ã¦ãæ¸ãã¦ããã¾ãã
ããã«ã¡ããã¨ç®ãéãã°ãééããªãã¦èµ·ããªãã¯ããªãã§ãããã¼ã
ã§ãå®éã«ã¯åéãï¼ééããããããã®ã§ãå½ã¨ã³ããªã¼ã§ç¢ºèªãã¦ãã ãããªã
æ°å¤ã®åã«ã¼ããä»ãã¦ã¯ãããªã
ç¾äºåä¸ã表ãã®ã«ã0123ã¨æ¸ãã¦ã¯ããã¾ãããJSONæ§æã§ã¯ãå é ã®ä½åãªã¼ãã¯è¨±ããã¦ãã¾ãããå é ã¼ãã使ãã®ã¯ã0, 0.123, 0.1e12 ã®ãããªã¨ãã ãã§ãã
JavaScriptã§ã¯ã0123ã¨æ¸ãã¦ããã¾ãã¾ãããããããã¯ç¾äºåä¸ãããªãã§ããããã£ã¦ã¿ã¾ããããï¼å®è¡ä¾ã¯Rhinoï¼
js> print(0123)
83
js>
å é ã«0ãä»ããã¨8é²æ°ã«ãªãã¾ãããã®è¨æ³ã¯Cã®æ代ï¼ãã£ã¨åï¼ï¼ããã®ä¼çµ±ã§ãã
#includemain()
{
printf("0123=%d 123=%04d\n", 0123, 123);
}
ãããå®è¡ããã¨ã表示ã¯æ¬¡ã®ããã«ãªãã¾ãã
0123=83 123=0123
æ¡æ°ãæããããã®ã¼ãããã£ã³ã°ã¯ãJSONæ§æã¨ãã¦ã¯å ¨ç¶ãã¡ã¼ï¼*1 æå³ããå¤ã¨ã¯ã¾ã£ããéã£ãå¤ã¨ãã¦è§£éããããã
ãã®ä¼çµ±çãª8é²è¡¨è¨æ³ã¯ééãã®ãã¨ã ãããã¨ãã次第ã§ãJavaScriptã§ãå»æ¢ãäºå®ããã¦ãã¾ããï¼http://developer.mozilla.org/ja/docs/Core_JavaScript_1.5_Guide:Literals ããï¼
8 é²æ´æ°ãªãã©ã«ã¯å»æ¢äºå®ã§ãããECMA-262 第 3 çããé¤ããã¦ãã¾ããJavaScript 1.5 ã§ã¯ä¾ç¶ã¨ãã¦å¾æ¹äºæã®ããã«ããããµãã¼ããã¦ãã¾ãã
16é²æ°è¡¨è¨ãç¦æ¢ã ã
8é²æ°ã¯ã¤ãã¤ãããããã»ãããããã©ã0x1A ã¨ãã®16é²æ°ãªãããããããã¡ã§ããJSONã§ã¯åé²ã ãã§ãã
æ°å¤ã®åã«ãã©ã¹ãä»ãã¦ã¯ãããªã
JavaScriptã§ã¯ï¼ã»ã¨ãã©ã®ããã°ã©ãã³ã°è¨èªã§ï¼ãæ°å¤ã®åã«ã¯ãã©ã¹ï¼+ï¼ããã¤ãã¹(-)ãä»ãããã¾ããã§ããªãããJSONã§ã¯å é ã®ãã©ã¹ãä»ãããã¾ããããã¤ãã¹ã ãã§ãã
ãããç´ãããããã¨ã«ãæµ®åå°æ°ç¹ã®ææ°é¨ã«ã¯ãã©ã¹ãä»ãã¦ãããã®ã§ãã+123ã¯ãã¡ã0.1e+12ã¯OKã§ãã
å°æ°ç¹ããã¯ãã¾ãæ°å¤ã¯ãã¡
JavaScriptã§ã¯ã0.3333333333333333ã.3333333333333333ã¨æ¸ãã¦ããã¾ãã¾ããã
js> 1/3 == .3333333333333333
true
js>
ããããJSONã§ã¯è¨±ããã¾ãããå¿ ã0ãä»ãã¦ãã ããï¼1åã ãï¼ï¼ãåæ§ã«ã.1e12ãèªãããã¾ããã
ç¨èªæ³ãéããï¼ããããã£ã¨ã¡ã³ãã¼
JavaScriptã§ã¯ããªãã¸ã§ã¯ãã«å«ã¾ããååã»å¤ãã¢ï¼name/value pairï¼ãããããã£ã¨å¼ã³ã¾ãããJSONã§ã¯ã¡ã³ãã¼ã§ããããããã£åã«å¯¾å¿ããJSONç¨èªã¯ç¹ã«ãªãããã§ãããJSONã®å ´åã¯ååï¼ã¡ã³ãã¼åï¼ã¨ããããã¯ãã¼æååã¨ããã»ããé©åã§ãããã
ããã¯ãªããã¨ããã¨ã⦠次ã®é ç®ãèªãã§ãã ããã
ã¡ã³ãã¼åã«ã¯å¸¸ã«æååã使ã
JSONãªãã¸ã§ã¯ãã®ã¡ã³ãã¼ã¯ååã»å¤ãã¢ã§ãããååãæååãªãã©ã«ã§ããã¤ã¾ããäºéå¼ç¨ç¬¦ã§å²ãå¿ è¦ãããã¾ãã
{x: 12, y: -3, color: "red"} ã¯ãã¡ã§ã{"x": 12, "y": -3, "color": "red"} ã¨ãªãã¾ãããã¼ãæååã«éå®ãããããã·ã¥ãããã¨èããã¨ããã¨æãã¾ãã
空æåå""ãã¡ã³ãã¼åã«ä½¿ãã
空æåå""ãæååã®ä¸ç¨®ã§ããJSONã®ãªãã¸ã§ã¯ãã§ã¯ãä»»æã®æååãã¡ã³ãã¼åï¼ãã¼æååï¼ã«ä½¿ããã®ã§ã{"": "hello"} ã¯åæ³çãã¼ã¿ã§ãã
JavaScript㧠var x = {"": "hello"}; ã¨ããã¨ãããããè¨æ³ã§ã¯ååï¼ãã¼ï¼ã""ã§ããã¡ã³ãã¼ã«ã¢ã¯ã»ã¹ã§ããªãã®ã§ãx[""] ã¨ããå¿ è¦ãããã¾ãã
é åè¦ç´ ã¯ããããªã¨ä¸¦ã¹ãã
JSONé åã®æ§æã¯ãåºæ¬çã«ã¯JavaScriptã¨åãã§ããã§ããã次ã¯ãã¡ã§ãã
[, "Hello", , "World", ,]
å½ããåã ããã£ã¦ï¼ ããã£ãJavaScriptã ã¨ä¸ã®æ¸ãæ¹ã許ããã¡ãããã§ããã
js> var a = [, "Hello", , "World", ,]
js> a.length
5
js> a[0]
js> typeof a[0]
undefined
js> a[1]
Hello
js> typeof a[2]
undefined
js>
ã¢ããããããã£ã±ãJavaScriptã£ã¦ç°å¸¸ã«ããããããJSONæ§æã¯ããã¾ã§ããããªãã£ã¦ãã¨ã§ãã
ã¡ãªã¿ã«ãJavaScriptã®ãªãã¸ã§ã¯ããªãã©ã«æ§æã¯ãé åã»ã©ã«ãããã¯ãªãã®ã§ãããæå¾ã®ã³ã³ãã¯è¨±ããã¾ãï¼ãã£ãã便å©ï¼ã{"x": 12, "y": -3, "color": "red",} ã¯JavaScriptã§ã¯OKãJSONã§ã¯ãã¡ã§ãã ï¼[追è¨]IEã®JavaScriptã§ã¯ãæå¾ã®ã³ã³ãã¯è¨±ãããªãããã§ãã[/追è¨]ï¼
æååãå²ãã«ã¯äºéå¼ç¨ç¬¦ã ã
JavaScriptã§ã¯ã"hello" ã 'hello' ã許ããã¾ãããJSONã§ã¯äºéå¼ç¨ç¬¦ã"ãã ãã使ç¨å¯è½ã§ãã
æååå ã®ã¨ã¹ã±ã¼ããå¾®å¦ã«éã
æååãªãã©ã«å ã§ã¯ãããã¯ã¹ã©ãã·ã¥ã«ããã¨ã¹ã±ã¼ãã使ãã¾ããããJavaScriptã¨JSONã§ã¯å¾®å¦ã«éãã¾ããJavaScriptã¨JSONã§å ±éãªã®ã¯æ¬¡ã®8種ã§ãã
- \b å¾é
- \f æ¹ãã¼ã¸
- \n æ¹è¡
- \r 復帰
- \t ã¿ã
- \" äºéå¼ç¨ç¬¦
- \\ ããã¯ã¹ã©ãã·ã¥ (\)
- \uXXXX 4æ¡ã®16é²æ° XXXX ã§æå®ãããUnicodeæå
JavaScriptã«ãã \vï¼åç´ã¿ãï¼ã¨ \'ï¼åä¸å¼ç¨ç¬¦ï¼ã¯ä½¿ãã¾ãããã©ãã \v ãªãã¦ä½¿ããããæååãå²ãã®ã¯äºéå¼ç¨ç¬¦ã ããªã®ã§ \' ãä¸è¦ã§ãã
3æ¡ã®8é²æ°ã2æ¡ã®16é²æ°ã«ããæåçªå·æå®ã使ãã¾ãããJSONã§ã¯ã\u ã«ã¦ãã³ã¼ãæåçªå·ã使ããã£ã¦ãã¨ã§ããã
ãã¦ãJavaScriptã«ã¯ãªãã®ã«JSONã«ã¯ããã¨ã¹ã±ã¼ã表ç¾ã¯ï¼
- \/ ã¹ã©ãã·ã¥ï¼ã½ãªãã¹ï¼
ã§ããå¥ã«ã¹ã©ãã·ã¥ãçã§å ¥ãã¦ããã¾ããªããããªã®ã§ãåã«ã¯æå³ä¸æã§ãããªãã ãï¼ ããã
ä»æ§ã«ãªãã¨ã¹ã±ã¼ãã¯æ§æã¨ã©ã¼
ããã¯ã¹ã©ãã·ã¥ã«ç¶ããæåããä»æ§ã§æ±ºããããã¨ã¹ã±ã¼ã表ç¾ã§ãªãã¦ããJavaScriptå¦çç³»ã¯æå¥ãè¨ãã¾ããã
js> print("\h\e\l\l\o");
hello
js>
å³å¯ãªJSONãã¼ã¶ã¼ã§ã¯æ§æã¨ã©ã¼ã«ãªãã§ããããJavaScripã§ãå°æ¥çã«ã¯æ§æã¨ã©ã¼ã«ãªãããã§ããï¼http://developer.mozilla.org/ja/docs/Core_JavaScript_1.5_Guide:Literals ããï¼
表 2.1 ã«è¼ã£ã¦ããªã[æªå±±æ³¨ï¼ä»æ§å¤ã®]æåã®ç´åã«ããã¯ã¹ã©ãã·ã¥ãä»ãã¦ãç¡è¦ããã¾ããããã®ä½¿ç¨æ³ã¯å»æ¢äºå®ã§ããã使ç¨ãé¿ããã¹ãã§ãã
undefinedãNaNãããã¾ãã
undefinedãNaNã¯ãJavaScriptãªãã©ã«ã§ã¯ãªãã¦ããããããå¤ãè¨å®ããã大åå¤æ°ã§ãããããã£ã¦ãJSONã§ã¯ãããªãã®ããã¾ããã
undefinedãNaNã使ãããç¶æ³ã§ã¯ããªã«ã代æ¿è¡¨ç¾ãèããå¿ è¦ãããã¾ããnull, "", 0, -1, false, "undefined", "NaN" ãªã©ã使ããã¨ã«ãªãã§ãããã
ã©ããã¼ãªãã¸ã§ã¯ãã¯ä½¿ããªãã®ãå
var flag = new Boolean(true); ã¨ãã¦ã»ããããfalgã®å¤ã¯ãããªããã£ããªãã¼ã«å¤ã§ã¯ãªãã¦ãªãã¸ã§ã¯ãã«ãªãã¾ãã
js> var flag = new Boolean(true);
js> typeof flag
object
js>
JSONã§ãªãã¸ã§ã¯ãã¨è¨ãã°ãå¤ã»ååãã¢ã®é åºãªãéåã§ããå³å¯ã«è¨ãã°ããã¼ã«å¤ãæ°å¤ãæååãªã©ã®ã©ããã¼ãªãã¸ã§ã¯ãããã®ã¾ã¾JSONã«ã¨ã³ã³ã¼ãã£ã³ã°ãããã¨ã¯ä¸å¯è½ã§ããå®éä¸ã¯ãvalueOfã¡ã½ããã§åãåºããå¤ãã¨ã³ã³ã¼ãã£ã³ã°ããã°æ¸ãããããã便å©ããã§ãããã©ããã¼ãªãã¸ã§ã¯ãã¨ãã¦ã®æ©è½ã»ç¹å¾´ã¯å¤±ããã¾ãã
åã·ã¹ãã ã¨typeofã«é¢ãã注æ
JSONã®åã·ã¹ãã ã¯åç´ã§ãããã«ã¾ã¨ã¾ã£ã¦ãã¾ãã
- ããªããã£ããªåï¼ null, boolean, number, string
- æ§é çï¼è¤åçï¼ãªåï¼ array, object
ãã¼ã¿è¡¨ç¾ã®æåã®ä¸æåãè¦ãã ãã§ãåãå¤æã§ãã¾ãã
- n ï¼ null
- tã¾ãã¯f ï¼ boolean
- æ°åã¾ãã¯- ï¼ number
- " ï¼ string
- [ ï¼ array
- { ï¼ object
JSONã«ãã£ããããtypeofããããªãããã㯠null, boolean, number, string, array, object ã®ãããããè¿ãã¹ãã§ããããããããJavaScriptã®typeofã¯ã¡ãã£ã¨ãããã§ãã¾ãã
- typeof null 㯠object
- typeof é å 㯠object ï¼arrayãè¿ãå®è£ ãåå¨ããããã§ããï¼
æå¾ã«
JavaScriptã§ã¯ãjsonDataã«JSONå½¢å¼ã§è¨è¿°ãããããã¹ããã¼ã¿ãå ¥ã£ã¦ããå ´åãeval('(' + jsonData + ')') ã¨ããã°ã¨ãããããã³ã¼ããã§ãã¾ãããã®å®ç´ãªï¼ããã¦å±éºãªï¼æ¹æ³ã使ã£ã¦ããã¨ãjsonDataãJSONä»æ§ã«éåãã¦ãã¦ãè¦éãã¡ããã¾ããã§ãããããªã«ãããã¦ã¨ã³ã«ã²ã³ãªãã¼ã¶ã¼ã°ããã§ã¯ãªãã®ã§ãéæ³æ§æã¯äºæ ã«ã¤ãªããããã¾ããã
ãã£ã±ããæ¬æ¥ã®JSONä»æ§ã«ã¡ããã¨å¾ã£ãæ§æã使ããããã®ã§ãã
*1:åãããã¯è¦éãã¦ãããªã¼ãã¼ãããã£ã³ã°ã便å©ããã ãã©ãã¼ã¡ãï¼é¢ä¿è