简体   繁体   English

如何统计.json中的物体数量

[英]How to count the number of objects in .json

How do you count the number of different objects in this array json file.你如何计算这个数组 json 文件中不同对象的数量。 A single object is:单个 object 是:

{
    "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]]
   }

In the example below there is 4.在下面的示例中,有 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]]
   }
]

Im working in react and I'm passing this json file through as a prop so I would need to manipulate the prop.我正在做反应,我将这个 json 文件作为道具传递,所以我需要操纵道具。 I want to know how many different entries are in the json file so I can display the number of radio buttons according to this.我想知道 json 文件中有多少不同的条目,这样我就可以根据这个显示单选按钮的数量。


import React from 'react';

const Test = (props) => {
return (

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

)
};
export default Test;

my json file is set as in app.js:我的 json 文件设置为 app.js:

this.state = { apiResponse: [] };

  .then(res => res.json())
   .then(res => this.setState({ apiResponse: res }));

and i call it by saying我这样称呼它

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

在此处输入图像描述

Put your JSON data in JSON.parse and then you can access length property on it:将您的 JSON 数据放入JSON.parse中,然后您可以访问其上的长度属性:

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

As per your code, if props.data contains the example JSON you mentioned, then props.data.length should already be giving you the number of elements in the array.根据您的代码,如果props.data包含您提到的示例 JSON,那么props.data.length应该已经为您提供了数组中的元素数。

If you are passing props as a string, then just do如果您将道具作为字符串传递,那么只需执行

JSON.parse(props);

before accessing the object's length.在访问对象的长度之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM