2

Here is my JSON array code

var App = [
 {
   "id": "123",
   "caption": "Test",
   "description": "Test Desc"
 },
 {
   "id": "345",
   "caption": "adsasdasd",
   "description": ""
 },
 {
   "id": "456",
   "caption": "adsasdasd",
   "description": ""
 },
 {
   "id": "578",
   "caption": "adsasdasd",
   "description": ""
 }
]

i tried with the following code

var obj = $.parseJSON(App);
alert(JSON.stringify(obj,4,null));
var AppLen = obj[i].length;
alert(AppLen);

but i didn't get any solution. Let me know if i missed any thing to get the JSON object array length.

NaniG
  • 119
  • 1
  • 12
  • where is question? can you explain wnat you want? anyway see [mdn](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/length) – Grundy Dec 18 '14 at 08:17
  • 2
    you need to use `obj.length` instead of `obj[i].length` – Pavel Reznikov Dec 18 '14 at 08:22

2 Answers2

1

your data is already json format:do like this

var App = [
           {
             "id": "123",
             "caption": "Test",
             "description": "Test Desc"
           },
           {
             "id": "345",
             "caption": "adsasdasd",
             "description": ""
           },
           {
             "id": "456",
             "caption": "adsasdasd",
             "description": ""
           },
           {
             "id": "578",
             "caption": "adsasdasd",
             "description": ""
           }
          ];

console.log(App);
console.log(App[0].length);// you can not get length from this because it is not array it's an object now.

var AppLen = App.length;
alert(AppLen);
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
-1
obj.size() or obj.length

If that dont run try this: Length of a JavaScript object

Object.size = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};

// Get the size of an object
var size = Object.size(myArray);
Community
  • 1
  • 1
Raúl Monge
  • 223
  • 2
  • 7
  • There is not `.count()` neither for `Object` nor for `Array` – t.niese Dec 18 '14 at 08:40
  • 1
    Extending `Object.prototype` is a **very bad** practice, that can be compared to killing kittens or genocide. – Ginden Dec 18 '14 at 08:44
  • @Ginden In this case it is not the `Object.prototype` but the `Object` itself. But yes neither the `prototype` nor the Object itself of a standard Object should be changed/modified unless it is a poly-fill. – t.niese Dec 18 '14 at 09:12