Javascript json数组中的项数

Javascript json数组中的项数,javascript,json,Javascript,Json,寻找一个简单的JS来计算.json文件中的项目数(在本例中,每个项目代表一张被拉入web应用程序的instagram照片;我想计算照片数)。Json的结构就是这样 { "type":"FeatureCollection", "features":[ { "type":"Feature", "geometry":{ "coordinates":[ -79.40916, 43.87767 ],

寻找一个简单的JS来计算.json文件中的项目数(在本例中,每个项目代表一张被拉入web应用程序的instagram照片;我想计算照片数)。Json的结构就是这样

{
 "type":"FeatureCollection",
 "features":[
  {
     "type":"Feature",
     "geometry":{
        "coordinates":[
           -79.40916,
           43.87767
        ],
        "type":"Point"
     },
     "properties":{
        "longitude":-79.40916,
        "latitude":43.87767,
        "title":"",
        "user":"cmay2400",
        "id":"176051485697457528_13947894",
        "image":"http:\/\/distilleryimage0.instagram.com\/1d725a3a8d7511e181bd12313817987b_7.jpg",
        "images":{
           "low_resolution":{
              "url":"http:\/\/distilleryimage0.instagram.com\/1d725a3a8d7511e181bd12313817987b_6.jpg",
              "width":306,
              "height":306
           },
           "thumbnail":{
              "url":"http:\/\/distilleryimage0.instagram.com\/1d725a3a8d7511e181bd12313817987b_5.jpg",
              "width":150,
              "height":150
           },
           "standard_resolution":{
              "url":"http:\/\/distilleryimage0.instagram.com\/1d725a3a8d7511e181bd12313817987b_7.jpg",
              "width":612,
              "height":612
           }
        },
        "description":"Today's ride <span class=\"tag\">#zipcar<\/span>",
        "instagram_id":"13947894",
        "likes":1,
        "profile_picture":"http:\/\/images.instagram.com\/profiles\/profile_13947894_75sq_1322267355.jpg"
     }
  },
  {
     "type":"Feature", [...]
{
“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“几何学”:{
“坐标”:[
-79.40916,
43.87767
],
“类型”:“点”
},
“财产”:{
“经度”:-79.40916,
“纬度”:43.87767,
“名称”:“名称”,
“用户”:“cmay2400”,
“id”:“176051485697457528_13947894”,
“图像”:“http:\/\/蒸馏图像0.instagram.com\/1d725a3a8d7511e181bd12313817987b_7.jpg”,
“图像”:{
“低分辨率”:{
“url:“http:\/\/蒸馏图像0.instagram.com\/1d725a3a8d7511e181bd12313817987b_6.jpg”,
“宽度”:306,
“高度”:306
},
“缩略图”:{
“url:“http:\/\/蒸馏图像0.instagram.com\/1d725a3a8d7511e181bd12313817987b_5.jpg”,
“宽度”:150,
“高度”:150
},
“标准分辨率”:{
“url:“http:\/\/蒸馏图像0.instagram.com\/1d725a3a8d7511e181bd12313817987b_7.jpg”,
“宽度”:612,
“高度”:612
}
},
“描述”:“今天的骑行#zipcar”,
“instagram_id”:“13947894”,
“喜欢”:1,
“个人资料图片”:“http:\/\/images.instagram.com\/profiles\/profile\u 13947894\u 75sq\u 1322267355.jpg”
}
},
{
“类型”:“特征”,[…]
我只想循环浏览json文件并计算项目数。完全不知道从哪里开始。

将其放入一个对象中,并像使用JavaScript中的任何其他对象一样使用它:

var o = JSON.parse(jsonstring);

alert(o.features.length); /* number of items in features array */

这或多或少就是您要查找的代码:

var variable = jQuery.parseJSON( stringThatIsStoringJson );

for(var i=0;i<variable.features.length;i++) {
    doStuff(variable.features[i]);

    for(var j=0;j<variable.features[i].geometry.coordinates.length;j++) {
        doMoreStuff(variable.features[i].geometry.coordinates[j]);
    }
}
var variable=jQuery.parseJSON(stringThatIsStoringJson);

对于(var i=0;i当然,首先必须将json字符串转换为js对象。使用
json.parse()
(IE6\7不支持)或包含Crockford,以便在IE<8上支持它

var obj = JSON.parse(jsonstr);
// loop the obj to find out what you want
或者,您可以尝试使用类似lib(类似CSS的JSON选择器)或类似的方法,然后可以轻松地操作数据,如:

var reslut = JSONSelect.match('css selector', obj); 

那么,您想计算哪些属性?您想计算哪些项目?您已经将.json文件加载到JS对象中了吗?