javascript - 如何在javascript中获取json编码数组的长度?

标签 javascript arrays json

我有一个像这样的 json 编码数组:

{
  "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]]}"

 } 

我的问题是:

(1) 如何求这个数组的长度? //这里是3

(2) 如何使用它的值(value)?
//例如:对于 as0,值为 {\"start\":{\"lat\":22.9939202,\"lng\":72.50009499999999},\"end\":{\"lat\":23.0394491,\"lng\":72.51248850000002},\"航路点\":[[23.0316834,72.4779436]]}

我使用上层东西的 javascript 代码:

 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); }
    }}
}

最佳答案

您的 JSON 不是数组,而是对象。如果你想让它成为一个数组,它应该是这样的:

[
  "{\"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]]}"

 ]

然后,你可以得到一个javascript数组,如下:

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

并按如下方式访问值:

array[0]

array.length

编辑:为了使用 PHP json_encode 方法获得真正的 JSON 数组,请参阅相关的 question .

通过此修改,您将能够使用 JS 数组的所有可能性而无需变通方法。

关于javascript - 如何在javascript中获取json编码数组的长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19516871/

相关文章:

javascript - 单击时更改 anchor 的颜色

python - C中的高效数组查找

javascript - 在 JavaScript 中查找单词数组中的第 n 个字符

c# - ASP.NET MVC Ajax Actions 结果封装

javascript - 使用 AngularJS 处理自定义 localStorage 数据

javascript - egghead.io 在 Redux 视频中使用哪个文本编辑器

javascript - 组织 JavaScript 函数和文件的最佳方式?

java - 我的代码给出了 java.lang.StringIndexOutOfBoundsException 并且命令以非零状态退出

javascript - javascript中唯一键上的俱乐部JSON对象

java - org.springframework.http.converter.HttpMessageNotWritableException : Could not write JSON: Specified map is empty