简体   繁体   English

JavaScript 保护器普通 json object 同时保存到 json 文件

[英]JavaScript preserver plain json object while saving to json file

May I know how can I preserve the plain JSON object while I save to.json file and retrieve from the json file in the properly way.我可以知道如何保存普通的 JSON object,同时保存到 .json 文件并以正确的方式从 json 文件中检索。

const jsonObj = [
        {
            min:1,
            max:4,
            defaultValue:3,
            width:"100%",
            label:"Column",
            onChange:(evt) => adjustGrid("col", evt),
            type:"InputNumber"
        },
        {
            min:1,
            max:4,
            defaultValue:1,
            width:"100%",
            label:"Row",
            onChange:(evt) => adjustGrid("row", evt),
            type:"InputNumber"
        }
    ]

The intention of preserving the plain JSON object is because I want to achieve fully dynamic form element controls with the helps of JSON object.保留普通的 JSON object 的目的是因为我想借助 JSON object 实现完全动态的表单元素控件。

I have attempted to use JSON.stringify but it escape the onChange key-pair which makes I cannot retrieve back the onChange key when I retrieve it from my.JSON file.我曾尝试使用 JSON.stringify 但它转义了 onChange 键对,这使得我无法在从 my.JSON 文件中检索 onChange 键时取回它。

the onChange function is not restricted for adjustGrid function, it can be any function that has been defined in the JS file. onChange function 不限于adjustGrid function,它可以是JS文件中定义的任何function。

The render code will be:渲染代码将是:

 return jsonObj.map((v) => {
            return (
                <Form.Item label={v.type}>
                        <InputNumber
                        min={v.defaultValue}
                        max={v.max}
                        defaultValue={v.defaultValue}
                        {...v}
                        width={v.width}
                      />
                </Form.Item>
            )
        });

JSON JSON

There is no "JSON Object" to start with, if such a thing can be said to exist: JSON is a notation syntax for serializing data objects.没有“JSON对象”开头,如果可以说存在这样的东西:JSON是一种用于序列化数据对象的符号语法。 By design it does not serialize object properties that are functions, does not distinguish between null and undefined, and can't encode objects with cyclic property references.按照设计,它不会序列化作为函数的 object 属性,不会区分 null 和未定义,并且无法对具有循环属性引用的对象进行编码。

JavaScript JavaScript

You could use a JavaScript file containing the text in the post - which is JavaScript source code and not JSON anyway.您可以使用包含帖子中文本的 JavaScript 文件 - 这是 JavaScript 源代码,而不是 JSON。 If you don't want to create or make use of global variables in the script, you could use export and import statements for jsonObj from within script files of type "module".如果您不想在脚本中创建或使用全局变量,您可以在“模块” 类型的脚本文件中使用jsonObj导出导入语句。 The downside for modules is that they are not available using the file:// protocol and must come from a server.模块的缺点是它们不能使用file://协议并且必须来自服务器。

You can't get a function from a stringified Object.您无法从字符串化的 Object 获得 function。

You want to keep code in source control, not in data, so it's a bad practice to do that.您希望将代码保存在源代码管理中,而不是数据中,因此这样做是一种不好的做法。 Also, it's a security hazard to have executable code in data...此外,在数据中包含可执行代码也是一种安全隐患......

I suggest extracting the handlers and using a conditional:我建议提取处理程序并使用条件:

If you have more handlers, you can create a choose handler function, but i just inlined it here...如果你有更多的处理程序,你可以创建一个选择处理程序 function,但我只是在这里内联它......

const InputItem = ()=>{

  const rowHandler = (evt) => adjustGrid("row", evt)
  const colHandler = (evt) => adjustGrid("col", evt)

  return jsonObj.map((v) => {
    return (
        <Form.Item label={v.type}>
                <InputNumber
                //...other properties
                onChange = {v.label == "Column" ? colHandler : rowHandler}
              />
        </Form.Item>
    )
  });
}

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

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