Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the values in json using javascript

I'm new to javascript and i want to count the values of this json string:

{
    "files": [
        {
            "name": "doc1.pdf",
            "title": "networking",
            "path": "mfpreader.comze.com\/files\/doc1.pdf"
        },
        {
            "name": "doc2.pdf",
            "title": "Armoogum",
            "path": "mfpreader.comze.com\/files\/doc2.pdf"
        }
    ]
}

the json is saved in the res.responseJSON.data.

here is the code i tried:

$("#demo").html(JSON.stringify(res.responseJSON.data));

var jsonObject = JSON.parse(res.responseJSON.data);
var propertyNames = Object.keys(jsonObject);

alert("There are "+propertyNames.length+" properties in the object");

The value is get is 1. It should be 2 since we have 2 documents.

can i have some please. Thanks

like image 843
Gunesh.John Avatar asked Mar 26 '26 21:03

Gunesh.John


1 Answers

For the required output what you want you have to do like below

var v={"files":[{"name":"doc1.pdf","title":"networking","path":"mfpreader.comze.com\/files\/doc1.pdf"},{"name":"doc2.pdf","title":"Armoogum","path":"mfpreader.comze.com\/files\/doc2.pdf"}]};

console.log(v.files.length)
like image 99
shreyansh Avatar answered Apr 02 '26 20:04

shreyansh