Michael Carey

CIT 261 - JSON

Home

JSON Object

  var empObj ={
    "employees":
     [
      {"firstName":"John", "lastName":"Doe"},
      {"firstName":"Anna", "lastName":"Smith"},
      {"firstName":"Peter", "lastName":"Jones"}
     ]
    }

JSON String

  var empTxt ='{ "employees" : [' +
     '{ "firstName":"John" , "lastName":"Doe" },' +
     '{ "firstName":"Anna" , "lastName":"Smith" },' +
     '{ "firstName":"Peter" , "lastName":"Jones" } ]}';

JSON Object without Conversion

  document.getElementById("rawObjOut").innerHTML =
  empObj.employees[1].firstName + " " + empObj.employees[1].lastName;

JSON String to Object

  var obj = JSON.parse(empTxt);
  document.getElementById("jsonToObject").innerHTML =
  obj.employees[1].firstName + " " + obj.employees[1].lastName;

JSON Object to String

  var text = JSON.stringify(obj);
  document.getElementById("jsonToString").innerHTML = text;