starting point: json
another one
object:
- left-curly-bracket
- pair(,pair,pair)
- right-curly bracket
pair:
- name
- colon
- value
array:
- left-square-bracket
- value(,value,value)
- right-square-bracket
two ways to convert a string to a JSON object, one is “eval” – unsafe, better way JSON.parse
var myObject =
{ ‘color’ : ‘blue’
, ‘animal’ :
[
{ ‘dog’ : ‘friendly’
, ‘cat’ : {‘name’:‘marbles’ }
, ‘arrayTest’ : [‘zero!’,‘one!’,‘two’]
}
]
}
;
document.writeln(myObject.animal [0].dog); // outputs friendly
document.writeln(myObject.animal [0].cat.name);
document.writeln(myObject.animal [0].arrayTest1);
// note single quotes dont seem to work inside of the string
// that is being fed into JSON.parse() function
var JSONstring =’ \
{ “color” : “blue” \
, “animal” : \
[ \
{ “dog” : “friendly”\
, “cat” : {name" }\
, “arrayTest” : [“zero!”,“one!”,“two”]\
}\
]\
}\
‘;
//var JSONstring =‘color",“arrayTest”:[“zero!”,“one!”,“two”]}]}’;
var newObject = JSON.parse(JSONstring);
document.writeln(newObject.animal [0].arrayTest1);