user663724
user663724

Reputation:

How to calculate the length of a array in this case

I am geting the following response from a Ajax call

[
    {
        "orderjson": "[{\"contact_phone_no\":\"9866545438\"},{\"contact_phone_no\":\"9866545438\"}]"
    }
]

I am trying to take the length of orderjson this way .

var data = 

    [
    {
        "orderjson": "[{\"contact_phone_no\":\"9866545438\"},{\"contact_phone_no\":\"9866545438\"}]"
    }
]

  console.log(JSON.stringify(data[0].orderjson.length));

But when i do so i am geting its length as 69 wheer as it is 2 .

Please tell me how to resolve this

Upvotes: 2

Views: 43

Answers (1)

A. Rama
A. Rama

Reputation: 912

Using JSON.stringify you're transforming the JSON into a string then the length call gives you the length of the resulting string.

Just do:

console.log(JSON.parse(data[0].orderjson).length) 

and it should work.

You need JSON.parse because the orderjson content is a string.

Upvotes: 4

Related Questions