javascript - json 数组中的项目数

标签 javascript json

寻找一个简单的 JS 来计算 .json 文件中的项目数量(在这种情况下,每个项目代表一张被拉入网络应用程序的 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", [...]

我只想遍历 json 文件并计算项目数。完全不知道从哪里开始。

最佳答案

Parse the JSON string进入一个对象并像使用 JavaScript 中的任何其他对象一样使用它:

var o = JSON.parse(jsonstring);

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

关于javascript - json 数组中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229966/

相关文章:

php - 循环仅插入最后一个值

javascript - 使用 page.js 进行 ASP.NET 客户端路由

javascript - 如何制作 .map?

javascript - 客户端接收json对象到javascript

javascript - JSON.解析 : unexpected character

javascript - Javascript 中奇怪的迭代变量作用域

javascript - Gulp - 从 src() 获取文件名

python - 如何防止 json_normalize 在 Pandas 中重复列标题?

javascript - npm update v5.4.2 后 ./node_modules/react-router-dom 中找不到模块错误

json - Elasticsearch:如何动态存储所有json对象