1

I've an issue with a json array row length...

I want to extract the correct length of data-row json (in this case "2")... If I try to count the length I get the value "1". Why ? How can I get the correct length ?

{
    "data-row": {
        "data1": [{
            "firstName": "John",
            "lastName": "Doe"
        }, {
            "firstName": "Anna",
            "lastName": "Smith"
        }, {
            "firstName": "Peter",
            "lastName": "Jones"
        }],
        "data2": [{
            "firstName": "John",
            "lastName": "Doe"
        }, {
            "firstName": "Anna",
            "lastName": "Smith"
        }]
    }
}
3
  • How did you try to count the length? Commented Jan 21, 2016 at 9:40
  • 1
    try Object.keys(jsondata.data-row).length Commented Jan 21, 2016 at 9:41
  • Please paste your code, how you count the length. Commented Jan 21, 2016 at 9:42

2 Answers 2

3

You have a Object and to find length of Object

var json = {
  "data-row": {
    "data1": [],
    "data2": []
  }
}


alert(Object.keys(json["data-row"]).length);

As you are having - in key use json["data-row"] to access object

Sign up to request clarification or add additional context in comments.

2 Comments

can you create a demo for this?
@AnoopJoshi i have created.
1

This is the another way to find length. but not right way.

var json = {
  "data-row": {
    "data1": [],
    "data2": []
  }
}
length=0;
$.each(json["data-row"],function(i){
  length++;
});
alert(length);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.