简体   繁体   中英

How to count the number of objects in .json

How do you count the number of different objects in this array json file. A single object is:

{
    "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.

[
  {
    "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. 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.


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:

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.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.

If you are passing props as a string, then just do

JSON.parse(props);

before accessing the object's length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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