The following is actually an array of JSON objects
:
var JSONObject = [{ "name":"John Johnson", "street":"Oslo West 16", "age":33,
"phone":"555 1234567"}, {"name":"John Johnson", "street":"Oslo West 16",
"age":33, "phone":"555 1234567" }];
So, in JavaScript length is a property of an array. And in your second case i.e.
var JSONObject = {"name":"John Johnson", "street":"Oslo West 16", "age":33,
"phone":"555 1234567"};
the JSON object is not an array. So the length property is not available and will be undefined. So you can make it as an array as follows:
var JSONObject = [{"name":"John Johnson", "street":"Oslo West 16", "age":33,
"phone":"555 1234567"}];
Or if you already have object say JSONObject
. You can try following:
var JSONObject = {"name":"John Johnson", "street":"Oslo West 16", "age":33,
"phone":"555 1234567"};
var jsonObjArray = []; // = new Array();
jsonObjArray.push(JSONObject);
And you do get length
property.