简体   繁体   English

如何在反应中编辑 json 文件

[英]How to edit a json file in react

Ok so the company I'm working in, wants a blog feature on their React website and all of the information from that blog will be stored inside a JSON file.好的,所以我工作的公司希望在他们的 React 网站上有一个博客功能,并且该博客的所有信息都将存储在 JSON 文件中。 My problem right now is that they also want the React app to allow to edit the JSON file containing all of the information regarding the blog, through the front end.我现在的问题是他们还希望 React 应用程序允许通过前端编辑包含有关博客的所有信息的 JSON 文件。 I've tried to change the location of the JSON file to see if that would allow me to edit it, but I can't edit the file whether it's on the public folder or on the src folder.我已尝试更改 JSON 文件的位置,以查看是否允许我对其进行编辑,但无论该文件位于公共文件夹还是 src 文件夹中,我都无法编辑该文件。 I'm kind of new to react and I don't know exactly where to store the JSON file, in which folder should I store it?我有点新反应,我不知道在哪里存储 JSON 文件,我应该将它存储在哪个文件夹中? And how do I edit the file?以及如何编辑文件?

In a React project, all your source code and assets are in the src folder.在 React 项目中,您所有的源代码和资产都在src文件夹中。 The compiler will put everything you need in the public folder.编译器会将您需要的所有内容放在public文件夹中。 If the JSON file is not copied, you might need a plugin that copies static files to the public folder.如果未复制 JSON 文件,您可能需要将 static 文件复制到公用文件夹的插件。

About your other question: do you want the user to be able to edit the JSON as a text string?关于您的另一个问题:您是否希望用户能够将 JSON 编辑为文本字符串? Then you can just load the JSON and keep it as a string.然后你可以加载 JSON 并将其保留为字符串。 That string you could put into some text input field.您可以将该字符串放入某个文本输入字段中。 It could look something like this:它可能看起来像这样:

fetch('assets/myfile.json')
    .then(responseString => {
        myEditableTextField.value = responseString
    })

To store the results after the user edited it, you will still need some kind of server page (NodeJS or PHP) that can save the file back to a JSON file after editing.要在用户编辑后存储结果,您仍然需要某种服务器页面(NodeJS 或 PHP),以便在编辑后将文件保存回 JSON 文件。

But note this is still bad practice.但请注意,这仍然是不好的做法。 There is an extremely high chance that the JSON will not be correct after the user has edited it. JSON 在用户编辑后极有可能不正确。

It would be better to build some kind of CMS that edits all the JSON properties separately.最好构建某种 CMS,分别编辑所有 JSON 属性。

fetch('assets/myfile.json')
    .then(responseString => responseString.json())
    .then(data => {
        myEditablenameField.value = data.name
        // etc. for all values in the json file
    })

It would be easy for you, if you load JSON directly into your React app as a response from API call.如果您将 JSON 作为 API 调用的响应直接加载到您的 React 应用程序中,这对您来说会很容易。 Instead of downloading.json file.而不是下载.json 文件。

axios.get("www.website.com/json").then((response)=>{
   let someData = JSON.parse(response);
   // do whatever you want to do with 'someData'
   // you could make post request to save edited data
   axios.post("www.website.com/save-json", {editedJSON: JSON.stringify(someData)})

});


You can use some JSON editor like react-ace您可以使用一些 JSON 编辑器,例如react-ace

Put your json file in assets , then import it to your component and follow the guide on the README: Your component would look like:将您的 json 文件放入assets中,然后将其导入您的组件并按照自述文件中的指南进行操作:您的组件将如下所示:

const [text, setText] = useState(JSON.stringify(yourJsonFile, null, 2));

function handleChange(text, event) {
  try {
    setText(text && JSON.parse(text));
  } catch (error) {
    // pass, user is editing
  }
}

return (
  <AceEditor
    mode="java"
    theme="github"
    onChange={handleChange}
    name="UNIQUE_ID_OF_DIV"
    editorProps={{ $blockScrolling: true }}
    value={text}
  />
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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