从反应js中的字典中提取数据
Extracting data from a dictionary in react js
我从我的 api 中输出 json 中的数据,并在字典中格式化,我想要一个值,例如每个 frame_number 的 intensity0。我如何在 React JS 中执行此操作我已经尝试了多种方法,但我似乎无法理解。你可以看到我在渲染部分的尝试。
import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import Buttons_Footer from "./components/Buttons_Footer.js";
import LeftPane from "./components/LeftPane.js"
//import './App.css';
class App extends React.Component {
constructor(props) {
super(props);
this.state = { apiResponse: "" };
}
// Comunicate with API
callAPI() {
fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
.then(res => res.text())
.then(res => this.setState({ apiResponse: res }));
}
componentWillMount() {
this.callAPI();
}
render() {
return (
<div className="App">
<ul>
{this.state.apiResponse.map(function(item,index){
return (<div><h1>{item.frame_number}</h1></div>)
})}
</ul>
<header className="App-header">
<p></p>
<div class="row fixed-bottom no-gutters">
<div class="col-3 fixed-top fixed-bottom">
<LeftPane></LeftPane>
</div>
<div class="offset-md-3" >
</div>
</div>
</header>
</div>
);
}
}
export default App;
[{"frame_number": 1, "roi0": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity0": 80.0, "roi1": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity1": 157.0},
{"frame_number": 2, "roi0": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity0": 80.0, "roi1": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity1": 158.0},
{"frame_number": 3, "roi0": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity0": 80.0, "roi1": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity1": 157.0}, ... etc
而不是 this.state = { apiResponse: "" };
使用 this.state = { apiResponse: [] };
将数据解析为 json。而不是 .then(res => res.text())
使用 .then(res => res.json())
我从我的 api 中输出 json 中的数据,并在字典中格式化,我想要一个值,例如每个 frame_number 的 intensity0。我如何在 React JS 中执行此操作我已经尝试了多种方法,但我似乎无法理解。你可以看到我在渲染部分的尝试。
import "bootstrap/dist/css/bootstrap.css";
import React from "react";
import Buttons_Footer from "./components/Buttons_Footer.js";
import LeftPane from "./components/LeftPane.js"
//import './App.css';
class App extends React.Component {
constructor(props) {
super(props);
this.state = { apiResponse: "" };
}
// Comunicate with API
callAPI() {
fetch("http://localhost:9000/IntensityAPI") //React app talks to API at this url
.then(res => res.text())
.then(res => this.setState({ apiResponse: res }));
}
componentWillMount() {
this.callAPI();
}
render() {
return (
<div className="App">
<ul>
{this.state.apiResponse.map(function(item,index){
return (<div><h1>{item.frame_number}</h1></div>)
})}
</ul>
<header className="App-header">
<p></p>
<div class="row fixed-bottom no-gutters">
<div class="col-3 fixed-top fixed-bottom">
<LeftPane></LeftPane>
</div>
<div class="offset-md-3" >
</div>
</div>
</header>
</div>
);
}
}
export default App;
[{"frame_number": 1, "roi0": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity0": 80.0, "roi1": [101.78202823559488, 99.39509279584912, 49.546951219239915, 29.728170731543948], "intensity1": 157.0},
{"frame_number": 2, "roi0": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity0": 80.0, "roi1": [102.56623228630755, 97.95906005049548, 50.25603182631066, 30.153619095786393], "intensity1": 158.0},
{"frame_number": 3, "roi0": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity0": 80.0, "roi1": [103.39336535376313, 98.20468223716023, 49.58465295946593, 29.750791775679556], "intensity1": 157.0}, ... etc
而不是 this.state = { apiResponse: "" };
使用 this.state = { apiResponse: [] };
将数据解析为 json。而不是 .then(res => res.text())
使用 .then(res => res.json())