[英]How to count the number of objects in .json
你如何计算这个数组 json 文件中不同对象的数量。 单个 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]]
}
在下面的示例中,有 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]]
}
]
我正在做反应,我将这个 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
中,然后您可以访问其上的长度属性:
JSON.parse(JSON.stringify(data)).length
根据您的代码,如果props.data包含您提到的示例 JSON,那么props.data.length应该已经为您提供了数组中的元素数。
如果您将道具作为字符串传递,那么只需执行
JSON.parse(props);
在访问对象的长度之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.