如何获得JSON的反应多少? [英] How to get number of response of JSON?
问题描述
这是JSON请求
http://www.omdbapi.com/?s=harry
按照链接查看响应的结构。我怎样才能以创建对象的数量循环
?
Follow the link to see the structure of response. How can I get a number of objects in order to create for loop
?
推荐答案
首先,你需要的 JSON.parse在这种情况下的数据(字符串):
First you need to JSON.parse the data (string) in this case:
var data = JSON.parse(string);
获取的项目数是:
Getting the number of items is:
data.length;
您可以做同样的一个文件(假设的node.js):
You can do the same with a file (assuming node.js):
require('fs').readFile(JSON.parse(file), 'utf8', function (err, data
{
if (err) throw err;
// your data is now parsed
});
然后你的迭代的使用for循环您感兴趣的数组:
Then you iterate using a for-loop the array you're interested in:
for (var prop in data)
{
// do something with item
var item = data[prop];
}
迭代时下不需要阵列长度的先验知识:
Iteration nowadays doesn't require the a-priori knowledge of the array length:
for (var i = 0; i < data.length; ++i)
这JSON似乎有对象的搜索
阵列(请参阅您的自我:的 http://json.parser.online.fr/ )
That JSON seems to have a Search
array of objects (see for your self: http://json.parser.online.fr/)
这篇关于如何获得JSON的反应多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!