Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在React Native中使用setState存储JSON数据_Javascript_Json_Reactjs_React Native - Fatal编程技术网

Javascript 在React Native中使用setState存储JSON数据

Javascript 在React Native中使用setState存储JSON数据,javascript,json,reactjs,react-native,Javascript,Json,Reactjs,React Native,我正在通过setState将JSON数据保存到构造函数变量。有什么办法吗? 这是我的助手: class House extends Component{ constructor(){ super(); this.state = { Lights_1: "", Windows_1: "", WantedTemp_1: "", }; } [ { "ID": 1, "name": "hall1", "light_

我正在通过setState将JSON数据保存到构造函数变量。有什么办法吗?

这是我的助手:

class House extends Component{
constructor(){
    super();
    this.state = {
      Lights_1: "",
      Windows_1: "",
      WantedTemp_1: "",
    };
}
[
  {
    "ID": 1,
    "name": "hall1",
    "light_state": 0,
    "window_state": 0,
    "wanted_temp": "20",
    "actual_temp": "20"
  }
]
这是我的JSON数据:

class House extends Component{
constructor(){
    super();
    this.state = {
      Lights_1: "",
      Windows_1: "",
      WantedTemp_1: "",
    };
}
[
  {
    "ID": 1,
    "name": "hall1",
    "light_state": 0,
    "window_state": 0,
    "wanted_temp": "20",
    "actual_temp": "20"
  }
]
如何将数据保存到VAR?

非常感谢。

如果您通过axios之类的调用获取json数据,您可以将其与响应一起使用

this.setState({
    id: response.ID,
    window_1: response.window_state,

})
对于要设置为状态的其余项,依此类推