8

I have a json encoded array like this:

{
  "ar0":"{\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"waypoints\":[[23.0316834,72.4779436]]}",

  "ar1":"{\"start\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"end\":{\"lat\":23.0420584,\"lng\":72.67145549999998},\"waypoints\":[[23.02237,72.6500747]]}",

  "ar2":"{\"start\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"end\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"waypoints\":[[23.0016629,72.58898380000005]]}"

 } 

my quetion is :

(1) How to find length of this array? //here it is 3

(2) How to use it's value?
//for example:for as0 the value is {\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"waypoints\":[[23.0316834,72.4779436]]}

javascript code where i use upper things :

 function setroute(os)
{
    var wp = [];
    for(var i=0;i<os.waypoints.length;i++)
        wp[i] = {'location': new google.maps.LatLng(os.waypoints[i][0], os.waypoints[i][1]),'stopover':false }

    ser.route({'origin':new google.maps.LatLng(os.start.lat,os.start.lng),
    'destination':new google.maps.LatLng(os.end.lat,os.end.lng),
    'waypoints': wp,
    'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK')ren.setDirections(res);
    })  
}

function fetchdata()
{
    var jax = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    jax.open('POST','process.php');
    jax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

    jax.send('command=fetch')
    jax.onreadystatechange = function(){ if(jax.readyState==4) {                
    alert(JSON.parse(jax.responseText).ar0);
        try {
                console.log(jax.responseText);

          //it is not work

        for(var i=0;i<JSON.parse(jax.responseText).length;i++) 
                {
                  setroute( eval('(' + jax.responseText + ')') );
                }
            }
        catch(e){ alert(e); }
    }}
}
0

5 Answers 5

11

Your JSON is not an array, but an object. If you want it to be an array, it should be something like this:

[
  "{\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"waypoints\":[[23.0316834,72.4779436]]}",

  "{\"start\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"end\":{\"lat\":23.0420584,\"lng\":72.67145549999998},\"waypoints\":[[23.02237,72.6500747]]}",

  "{\"start\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"end\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"waypoints\":[[23.0016629,72.58898380000005]]}"

 ]

Then, you can get a javascript array as follows:

var array = JSON.parse(jax.responseText);

And access values as follows:

array[0]

array.length

EDIT: In order to have a real JSON array with the PHP json_encode method, see this related question.

With this modification you will be able to use all the possibilities of JS array without workaround.

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

2 Comments

this is not an answer.i create json encode array in php and use it in javascript.
I didn't know that you used PHP. I updated my answer, I hope that will help you more ;)
5

Objects in JavaScript don't have a .length property like Arrays do.

In ES5 you can do: Object.keys({}).length; // 0

The other solution would be to loop over all the properties of your object with a for .. in loop and count.

Comments

4

You can use the following code. It will produce your desired output 3

var test = {
  "ar0":"{\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"waypoints\":[[23.0316834,72.4779436]]}",

  "ar1":"{\"start\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"end\":{\"lat\":23.0420584,\"lng\":72.67145549999998},\"waypoints\":[[23.02237,72.6500747]]}",

  "ar2":"{\"start\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"end\":{\"lat\":22.9999061,\"lng\":72.65318300000001},\"waypoints\":[[23.0016629,72.58898380000005]]}"

 } 
 var getLength = function(obj) {
    var i = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)){
            i++;
        }
    }
    return i;
};
 console.log(getLength(test));

Comments

3

For your first question, you should use Object.keys(item).length

To get the values of your object, you should iterate over it with a for loop:

for(var key in item) { var val = item[key]; }

Comments

2
var count = 0;
Object.keys(json).forEach(function (key) {
    count++;
    alert(json[key].start.lat);
});

alert(count);

Using jQuery

$(json).each(function() { count++; alert(this.start.lat); }); 
alert(count);

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.