JSON


DESCRIPTION

    JavaScript Object Notation
    • Lightweight data-interchange format (simple textual representation of data).
    • Easy to read and write.
    • Easy for machines to parse and generate.
    • Completely independent of any language.
    • Great format for passing data from server to client & back.
    • Syntax based on JS object literal, but JSON isn't a JS object literal, it must be converted.
    • JSON.parse to convert from JSON string to object.
    • JSON.stringify to convert from object o JSON string.

    Syntax Rules
    • Subset of JavaScript object literal syntax
    • (Subset das configurações do JS), but...
    • Property names and string values must be in double quotes " ".
    • Other syntaxes are the same.
    • Ex:
      '{"firstName": "Diego",
      "lastName": "Martinelli",
      "numberOfDisplays": 2 ... }'

    JSON conversion into object
    • Object Literal Syntax
      Object literals are defined using the following syntax rules:
      * A colomn separates property name[1] from value.
      * A comma separates each name-value pair from the next.
      * There should be no comma after the last name-value pair.[2]

      Object Literal Example:
      var myObject = {
      sProp: 'some string value',
      numProp: 2,
      bProp: false
      };

    JSON isn't a JavaScript Object Literal.
    • JSON isn't a JavaScript Object Literal.
    • JSON is just a string.
    • The syntax of JSON is based on objet literal, but it's not an object, you must convert.
    • If you want an object in JS, you'd have to take the JSON string and convert it into a JS Object.
    • If you want JSON you'd need to take an object and convert it back to a JSON string.

    Converting JSON to Object & Back to JSON string
    • Converts from json string to object
      var obj = JSON.parse(jsonString);

    • Converts from object to json string
    • Var str = JSON.stringify(obj);