The following is 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" }];
Thus, the length of JavaScript is a property of the 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. Therefore, the length property is not available and will be undefined. Therefore, you can do 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 an object, say JSONObject . You can try the following:
var JSONObject = {"name":"John Johnson", "street":"Oslo West 16", "age":33, "phone":"555 1234567"}; var jsonObjArray = [];
And you will get the length property.
Amol m kulkarni
source share