从导入的 JSON 中删除引号

Remove quotes from imported JSON

我正在使用执行此操作的 python 脚本导出一些数据:

data = {......., 'noTick': 'true'}
with open('items.json', 'w') as sample:
    sample.write(json.dumps(data))

我正尝试以这种方式在 vue 应用程序中导入 (src/data/items.json):

export default [
    {
        "PropertyOne": "ValueOne",
        .....,
        "noTick": "true"
    },

    {
        ......
    }
]

然后在我的 component.vue 中导入它:

import ALL_ITEMS from 'src/data/items.json'
...
const codes = ALL_ITEMS

问题是 quasar 框架的 q-tree 组件期望 noTick 属性 不被引用:

{
    "PropertyOne": "ValueOne",
    .....,
    noTick: true
}

到目前为止我只想出了:

  1. 在 vscode 中做 find/replace。

  2. 转换为字符串并返回 JSON:

    export default JSON.stringify(
        {
            "PropertyOne": "ValueOne",
            .....,
            "noTick": "true"
        }
    )

并在 Component.vue 中:

import ALL_ITEMS from 'src/data/items.json'
...
const codes = JSON.parse(ALL_ITEMS)

这也失败了。有什么方法可以通过使用 JavaScript?

将此 属性 更改为不带引号的

注意:为了更清楚,我已经编辑了问题

直接去掉引号,JSON可以表示原始类型(字符串、数字、布尔值和null)和两种结构化类型(对象和数组)。

    export default [
        {
            "PropertyOne": "ValueOne",
            .....,
            "noTick": true
        },
    
        {
            ......
        }
    ]

编辑: 在您的 python 脚本中,您应该做同样的事情:

data = {......., 'noTick': True}
with open('items.json', 'w') as sample:
     sample.write(json.dumps(data))

如果您要导入 .json 文件,则不需要 export default

您可以删除它并使用简单的 .json 和 {}

或者您可以将文件重命名为 .js(简单 javascript 文件),保留导出默认值

例如:

export default {
    name: 'John Doe',
}