Javascript 如何计算.json中的对象数

Javascript 如何计算.json中的对象数,javascript,arrays,node.js,reactjs,dictionary,Javascript,Arrays,Node.js,Reactjs,Dictionary,如何计算此数组json文件中不同对象的数量。单个对象是: { "short_name": "Demo", "roi_number": 0, "finding": "null", "dt": 0.1, "norm_times_series": [ [80.0, 68.16335617854995, 91.83664382145005], [80.0, 67.66379980989896, 92.33620019010104],

如何计算此数组json文件中不同对象的数量。单个对象是:

{
    "short_name": "Demo",
    "roi_number": 0,
    "finding": "null",
    "dt": 0.1,
    "norm_times_series": [
      [80.0, 68.16335617854995, 91.83664382145005],
      [80.0, 67.66379980989896, 92.33620019010104],
      [83.0, 72.78200156354234, 93.21799843645766],
      [83.0, 71.22165069319166, 94.77834930680834],
      [81.0, 67.74249757065954, 94.25750242934046],
      [80.0, 67.12186571458821, 92.87813428541179],
      [81.0, 68.71545011387893, 93.28454988612107]]
   }
在下面的例子中有4个

[
  {
    "short_name": "Demo",
    "roi_number": 0,
    "finding": "null",
    "dt": 0.1,
    "norm_times_series": [
      [80.0, 68.16335617854995, 91.83664382145005],
      [80.0, 67.66379980989896, 92.33620019010104],
      [83.0, 72.78200156354234, 93.21799843645766],
      [83.0, 71.22165069319166, 94.77834930680834],
      [81.0, 67.74249757065954, 94.25750242934046],
      [80.0, 67.12186571458821, 92.87813428541179],
      [81.0, 68.71545011387893, 93.28454988612107]]
   },
 {
    "short_name": "Demo",
    "roi_number": 1,
    "finding": "null",
    "dt": 0.1,
    "norm_times_series": [
      [80.0, 68.16335617854995, 91.83664382145005],
      [80.0, 67.66379980989896, 92.33620019010104],
      [83.0, 72.78200156354234, 93.21799843645766],
      [83.0, 71.22165069319166, 94.77834930680834],
      [81.0, 67.74249757065954, 94.25750242934046],
      [80.0, 67.12186571458821, 92.87813428541179],
      [81.0, 68.71545011387893, 93.28454988612107]]
   },
 {
    "short_name": "Demo",
    "roi_number": 2,
    "finding": "null",
    "dt": 0.1,
    "norm_times_series": [
      [80.0, 68.16335617854995, 91.83664382145005],
      [80.0, 67.66379980989896, 92.33620019010104],
      [83.0, 72.78200156354234, 93.21799843645766],
      [83.0, 71.22165069319166, 94.77834930680834],
      [81.0, 67.74249757065954, 94.25750242934046],
      [80.0, 67.12186571458821, 92.87813428541179],
      [81.0, 68.71545011387893, 93.28454988612107]]
   },
 {
    "short_name": "Demo",
    "roi_number": 3,
    "finding": "null",
    "dt": 0.1,
    "norm_times_series": [
      [80.0, 68.16335617854995, 91.83664382145005],
      [80.0, 67.66379980989896, 92.33620019010104],
      [83.0, 72.78200156354234, 93.21799843645766],
      [83.0, 71.22165069319166, 94.77834930680834],
      [81.0, 67.74249757065954, 94.25750242934046],
      [80.0, 67.12186571458821, 92.87813428541179],
      [81.0, 68.71545011387893, 93.28454988612107]]
   }
]

我在react中工作,我将这个json文件作为一个道具传递,所以我需要操纵这个道具。我想知道json文件中有多少个不同的条目,这样我就可以根据这些条目显示单选按钮的数量


import React from 'react';

const Test = (props) => {
return (

 console.log(JSON.parse(props).length); // My attempt = prop is ouput.json

)
};
export default Test;
我的json文件在app.js中设置为:

this.state = { apiResponse: [] };

  .then(res => res.json())
   .then(res => this.setState({ apiResponse: res }));
我称之为

<Test test = {this.state.apiResponse}/>


将您的JSON数据放入
JSON.parse
中,然后您可以访问其中的length属性:

JSON.parse(JSON.stringify(data)).length

根据您的代码,如果props.data包含您提到的示例JSON,那么props.data.length应该已经给出了数组中的元素数

如果您将道具作为字符串传递,那么只需这样做

JSON.parse(props);

在访问对象的长度之前。

谢谢!我如何检查它的工作控制台日志似乎不起作用。我在问题中添加了一些内容,以使其更具体,如果你能帮我检查的话,谢谢!哦,我明白了,你不能把console.log放在return语句中,把它移到return语句之外。另外,我对原来的答案做了一点修改,有一个看起来很傻的问题,但是当我在本地主机上运行它时,它会打印到哪里呢?在这里,请阅读评论:我还看到了你的代码的另一个问题,而不是
JSON.parse(props)
它应该是
JSON.parse(props.test)
。希望它能帮到我,不管我把json文件放在什么初始状态,就像在我的代码中一样。状态={apiResponse:[]};如果你能帮我查一下的话,我在问题的最后加了一点谢谢