javascript json 数组中的项目数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11229966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:28:24  来源:igfitidea点击:

Number of items in a json array

javascriptjson

提问by Michael C.

Looking for a simple bit of JS to count the number of items in a .json file (each item represents, in this case, an instagram photo being pulled into the web app; I want to count the number of photos). Json is structured thusly...

寻找一个简单的 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", [...]

I just want to loop through the json file and count the number of items. Completely lost on where to begin.

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

回答by xandercoded

Parse the JSON stringinto an object and use it as you would any other object in JavaScript:

将 JSON 字符串解析为一个对象,并像使用 JavaScript 中的任何其他对象一样使用它:

var o = JSON.parse(jsonstring);

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

回答by YuriAlbuquerque

This is more or less the code you are looking for:

这或多或少是您正在寻找的代码:

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

Assuming you are using jQuery. You can parse the JSON with whatever library you want. Just avoid eval(), which opens your site to XSS vulnerabilities.

假设您正在使用 jQuery。您可以使用任何您想要的库来解析 JSON。只是避免eval(),这会使您的站点面临 XSS 漏洞。

回答by wiky

Of course, the first thing you must transform the json string into js object. by use JSON.parse()(IE6\7 not support) or include the Crockford's JSON2 parserin order to support it on IE < 8.

当然,首先你必须将json字符串转化为js对象。通过使用JSON.parse()(IE6\7 不支持)或包含 Crockford 的JSON2 解析器,以便在 IE < 8 上支持它。

var obj = JSON.parse(jsonstr);
// loop the obj to find out what you want

Or another way, you can try to use some lib like jsonSelect(CSS-like selectors for JSON.) or something like JSONPath, then you can easy to manipulate your data like:

或者另一种方式,您可以尝试使用一些库,如jsonSelect(用于 JSON 的类 CSS 选择器。)或诸如JSONPath 之类的东西,然后您可以轻松地操作您的数据,例如:

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