program/javascript

JSON 예제

yoursyun 2015. 4. 13. 14:30

// 객체 형태

var jsontext = '{"firstname":"Jesper","surname":"Aaberg","phone":["555-0100","555-0120"]}';

// 배열형태
var jsonarray = '["hello", true, 1]';

       

var contact = JSON.parse(jsontext);
var vartype = JSON.parse(jsonarray);

// 혼합형태
var emp = '{"employees":['
             + '{"firstName":"John", "lastName":"Doe"},'
             + '{"firstName":"Anna", "lastName":"Smith"},'
             + '{"firstName":"Peter", "lastName":"Jones"}'
             + ']}';

 

var employee = JSON.parse(emp);

       

$("#result").text(contact.surname + ", " + contact.firstname);

       

var str = $("#result").text();

       

$("#result").text(str += " : " + vartype[1]);

       

$("#result").text(str += " : " + employee.employees[0].firstName);

 

// Output: Aaberg, Jesper : true : John

반응형