¾Õ¼ ¿¹Á¦¸¦ ÅëÇؼ º¸¾ÒÁö¸¸, JSON ¹®ÀÚ¿Àº ÀÚ¹Ù½ºÅ©¸³Æ® ¹è¿°ú ¾ÆÁÖ Èí»ç ÇÕ´Ï´Ù. (°ÅÀÇ ¶È°°´Ù°í º¸¼Åµµ µÉ°Í °°½À´Ï´Ù.)
¹è¿µµ ±×·¸Áö¸¸, 1Â÷¿ø, 2Â÷¿ø, 3Â÷¿ø,,,, ó·³ ¹è¿ÀÌ Á¸ÀçÇÏ´Â °Íó·³, JSON ¹®ÀÚ¿µµ ´ÙÂ÷¿øÀ» Áö¿øÇØ ÁÝ´Ï´Ù.
À̹ø¿¡´Â ±× Ç¥Çö ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æº¸µµ·Ï ÇÏ°Ú½À´Ï´Ù.
¸ÕÀú °¡Àå ±âº»ÀÌ µÇ´Â °ÍÀº 1Â÷¿ø ÀÔ´Ï´Ù.
var JSONObject = {"name":"À¯Àç¼®","street":"¼¿ïƯº°½Ã", "age":41, "phone":"011 1234 5678"};
$.each(JSONObject, function(i, n) {
// °´Ã¼ÀÇ À̸§°ú °¡°Ý ÇÁ·ÎÆÛƼ¸¦ body °´Ã¼¿¡ Ãß°¡ÇÕ´Ï´Ù.
$('body').append(n.name + ": " + n.price + "<br>");
});
each ¸í·ÉÀ» ÅëÇØ °á°ú°ªÀ» Ãâ·ÂÇØ º¸ÀÚ.
À§¿Í °°ÀÌ Ç¥ÇöÀ» ÇÕ´Ï´Ù. JSONObject °´Ã¼ÀÇ name, street, age, phone À» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
JSONObject.name , JSONObject.street ó·³ »ç¿ëÇÏ¸é µË´Ï´Ù.
µÎ¹ø°·Î À§ÀÇ ¹æ½ÄÀº À¯Àç¼® À̶ó´Â »ç¶÷¿¡ ´ëÇÑ µ¥ÀÌÅ͸¸ Á¸ÀçÇÕ´Ï´Ù. ´Ù¸¥»ç¶÷ÀÇ Á¤º¸µµ Ç¥ÇöÇÏ°í ½ÍÀ» °æ¿ì¿¡´Â ¾Æ·¡¿Í °°Àº ¹æ¹ýÀ¸·Î »ç¿ë ÇÒ ¼ö ÀÖ½À´Ï´Ù.
var JSONObject = [
{"name":"À¯Àç¼®","street":"¼¿ïƯº°½Ã", "age":41, "phone":"011 1234 5678"},
{"name":"°È£µ¿","street":"¼¿ïƯº°½Ã", "age":41, "phone":"011 1234 5678"},
{"name":"Ã־ȱÔ","street":"±¤ÁÖ±¤¿ª½Ã", "age":41, "phone":"011 1234 5678"}
];
¹®ÀÚ¿¿¡ ´ë°ýÈ£ [] ·Î ¹¾î ÁÖ¸é µÇ¸ç, Ç׸ñÀº ÄÞ¸¶(,) ·Î ±¸ºÐÇÏ¿© Ç¥Çö ÇÒ ¼ö ÀÖ½À´Ï´Ù.
µ¥ÀÌÅÍ Ãâ·ÂÀº
$('body').append(JSONObject[0].name + ": " + JSONObject[0].street);
$('body').append(JSONObject[1].name + ": " + JSONObject[1].street);
$('body').append(JSONObject[2].name + ": " + JSONObject[2].street);
$('body').append(JSONObject[3].name + ": " + JSONObject[3].street);
À§¿Í °°Àº ÇüÅ·Π¿øÇÏ´Â µ¥ÀÌÅ͸¦ Ãâ·Â ÇÒ ¼ö ÀÖ½À´Ï´Ù.
each() ÇÔ¼ö¸¦ ÀÌ¿ëÇÑ´Ù¸é
$.each(JSONObject, function(i, n) {
// °´Ã¼ÀÇ À̸§°ú °¡°Ý ÇÁ·ÎÆÛƼ¸¦ body °´Ã¼¿¡ Ãß°¡ÇÕ´Ï´Ù.
$('body').append(n.name + ": " + n.price + "<br>");
});
¿Í °°ÀÌ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
¼¼¹ø° °æ¿ìÀÇ µ¥ÀÌÅ͸¦ º¸µµ·Ï ÇÕ´Ï´Ù.
var menu = { "employees1" : [
{"name" : "À¯Àç¼®", "price" : "8,000", "job":"¹é¼ö1" },
{"name" : "¼ÛÁ¾±¹", "price" : "8,000", "job":"¹é¼ö2" },
{"name" : "°È£µ¿", "price" : "9,000", "job":"¹é¼ö3" }],
"employees2" : [
{"name" : "±è¿µÁØ", "price" : "8,000", "job":"¹é¼ö4" },
{"name" : "Á¤ÁØÀÏ", "price" : "8,000", "job":"¹é¼ö5" },
{"name" : "Ã־ȱÔ", "price" : "8,000", "job":"¹é¼ö6" }],
"employees3" : [
{"name" : "À̺´Çå", "price" : "8,000", "job":"¹é¼ö7" },
{"name" : "¹Ú±ÙÇý", "price" : "8,000", "job":"¹é¼ö8" },
{"name" : "±è¹ÎÁ¤", "price" : "8,000", "job":"¹é¼ö9" }]
};
À§ ¼Ò½º¿Í °°ÀÌ µ¥ÀÌÅÍ ÇüÀ» ÁöÁ¤ÇÒ ¼ö Àִµ¥¿ä. ȸ»ç¿¡ º¸¸é »ç¿øÀÌ ¸¹ÀÌ Á¸ÀçÇϸé, ºÎ¼º°·Î ³ª´©¾îÁ® ÀÖÀ» ¼ö ÀÖ½À´Ï´Ù.
employees1 ,employees2 , employees3 °ú °°ÀÌ ³ª´©¾îÁ® ÀÖ½À´Ï´Ù.
employees1 ¿¡´Â À¯Àç¼®, ¼ÛÁ¾±¹, °È£µ¿ÀÌ Á¸ÀçÇÏ°í,
employees2 ¿¡´Â ±è¿µÁØ, Á¤ÁØÀÏ, Ã־ȱ԰¡ Á¸ÀçÇÏ°í,
employees3 ¿¡´Â À̺´Çå, ¹Ú±ÙÇý, ±è¹ÎÁ¤ÀÌ Á¸ÀçÇÒ °æ¿ì JSON ¹®ÀÚ¿Àº À§¿Í °°Àº ¹æ¹ýÀ¸·Î ¼³Á¤ÇÒ ¼ö ÀÖ½À´Ï´Ù.
1Â÷¿øÀº {} ·Î ¹À» ¼ö ÀÖ±¸¿ä.
2Â÷¿øÀº {} ·Î ¹Àº ¹®ÀÚ¿¿¡ ´ë°ýÈ£·Î ¹¾î ÁÝ´Ï´Ù. [ {} ] ¿Í °°Àº Çü½ÄÀÌ µË´Ï´Ù.
3Â÷¿øÀº [{}] ·Î ¹¿© ÀÖ´Â ¹®ÀÚ¿¿¡ Áß°ýÈ£{} ·Î ´Ù½Ã ¹¾î ÁÝ´Ï´Ù. { [{}] } ¿Í °°Àº Çü½ÄÀÌ µÉ ¼ö ÀÖ½À´Ï´Ù.
menu ¶ó´Â ¹®ÀÚ¿À» Ãâ·ÂÇÏ´Â ¹æ¹ýÀº ¾Æ·¡¿Í °°½À´Ï´Ù.
$('body').append( menu.employees1[0].name + ": " + menu.employees1[0].price + ": "+ menu.employees1[0].job );
$('body').append( menu.employees1[1].name + ": " + menu.employees1[1].price + ": "+ menu.employees1[1].job );
$('body').append( menu.employees1[2].name + ": " + menu.employees1[2].price + ": "+ menu.employees1[2].job );
$('body').append( menu.employees2[0].name + ": " + menu.employees2[0].price + ": "+ menu.employees2[0].job );
$('body').append( menu.employees2[1].name + ": " + menu.employees2[1].price + ": "+ menu.employees2[1].job );
$('body').append( menu.employees2[2].name + ": " + menu.employees2[2].price + ": "+ menu.employees2[2].job );
$('body').append( menu.employees3[0].name + ": " + menu.employees3[0].price + ": "+ menu.employees3[0].job );
$('body').append( menu.employees3[1].name + ": " + menu.employees3[1].price + ": "+ menu.employees3[1].job );
$('body').append( menu.employees3[2].name + ": " + menu.employees3[2].price + ": "+ menu.employees3[2].job );