Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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
Reactjs 如何从json文件中获取和添加元素?_Reactjs_React Native - Fatal编程技术网

Reactjs 如何从json文件中获取和添加元素?

Reactjs 如何从json文件中获取和添加元素?,reactjs,react-native,Reactjs,React Native,例如,假设我有这样一个JSON文件 const rooms = { livingroom: { "tv":true, "laptop": true, "air-conditioner":true, }, kitchen: { "fridge":true, "dining-table": true, }, gameroom: { &

例如,假设我有这样一个JSON文件

const rooms = {
  livingroom: {
    "tv":true,
    "laptop": true,
    "air-conditioner":true,
  },
  kitchen: {
    "fridge":true,
    "dining-table": true,
  },
  gameroom: {
    "computer":true,
    "headphones":true,
    "microphone":true,
    "cd-player":true,
  }
}
这是我的示例项目。我想做的是-

我希望能够根据房间名称获取物品,并显示它有哪些物品,“计算机”、“麦克风”等。 但我想添加新的房间名称和新项目


如何在React Native中实现类似的功能?

这不是json文件这是一个js文件,其中包含一个名为rooms的对象

要将房间添加到对象,请执行以下操作:

rooms[<new room name>] = { <Key:value pairs>}
要添加项目,请对项目名称和值执行相同操作:

rooms["study"]["chair"] = true

您可以创建一个
Data.js
文件,然后在类似这样的另一个文件中使用它

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';

import Data from './Data';

export default function App() {
  return (
    <View style={styles.container}>
      <Text>{Data.gameroom.computer.toString()}</Text>
      <Text>{Data.kitchen.fridge.toString()}</Text>
      <Text>{Data.livingroom.laptop.toString()}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
    alignItems: 'center',
  },
});

Data.js
应该是这样的-

const rooms = {
  livingroom: {
    tv: true,
    laptop: true,
    'air-conditioner': true,
  },
  kitchen: {
    fridge: true,
    'dining-table': true,
  },
  gameroom: {
    computer: true,
    headphones: true,
    microphone: true,
    'cd-player': true,
  },
};

export default rooms;
App.js
应该是这样的

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';

import Data from './Data';

export default function App() {
  return (
    <View style={styles.container}>
      <Text>{Data.gameroom.computer.toString()}</Text>
      <Text>{Data.kitchen.fridge.toString()}</Text>
      <Text>{Data.livingroom.laptop.toString()}</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
    alignItems: 'center',
  },
});
import*as React from'React';
从“react native”导入{Text,View,StyleSheet};
从“./Data”导入数据;
导出默认函数App(){
返回(
{Data.gameroom.computer.toString()}
{Data.kitchen.fredge.toString()}
{Data.livingroom.laptop.toString()}
);
}
const styles=StyleSheet.create({
容器:{
弹性:1,
为内容辩护:“中心”,
背景颜色:“#ecf0f1”,
填充:8,
对齐项目:“居中”,
},
});

谢谢您的回复。我只能按一个按钮吗?例如,我想按下一个按钮(addRoomButton),会有一些项目(关闭),我可以通过单击来打开,在房间中添加这些项目是否容易。数据?您可以将房间转换为一种状态,并将按钮的On Press设置为状态谢谢您的回复。我不想手工增加房间和物品。我的意思是我将有一个按钮,比如addRoomButton,当我按下按钮时,它将打开一个页面,其中一些项目被关闭。我将能够切换到该项目。和我切换的项目,应该添加rooms.data,所以我想在我的相关屏幕上看到它。这容易吗?我可能会问你更多关于你的问题的上下文,比如文件存储在哪里?你是从API请求中获得的,还是项目文件夹中扩展名为.json的物理文件,取决于不同的情况有不同的解决方案它是项目中的一个文件。我想要的更详细一点是,我将有一个按钮,比如addRoomButton,当我按下按钮时,它将打开一个页面,其中一些项目被关闭。我将能够切换到该项目。和我切换的项目,应该添加rooms.data,所以我想在我的相关屏幕上看到它。简单吗?当我按下on按钮时,会有一个文本,我将写下房间名称,其他项目部分将被关闭,我将能够打开它,以添加到房间属性中