java - GROOVY 在文件夹中创建 JSON 文件时出现随机反斜杠
问题描述
我在编译 JSON 文件时遇到问题。这是我的方法(主要方法只有我正在编写此代码的方法的名称,所以我没有在此处包含主要方法。)
static void buildJSONFiles() {
String commandJson = "C:/Users/Name/Desktop/docfiles/command_config.json"
def data = [
commands:
JsonOutput.toJson([new Commands(name:"upload", path:"\${BUILTIN_EXE(command)}", includeCommandName: true),
new Commands(name:"file_info", path:"\${BUILTIN_EXE(command)}", includeCommandName: true)])
]
println(data)
def json_str = JsonOutput.toJson(data)
def json_beauty = JsonOutput.prettyPrint(json_str)
File file = new File(commandJson)
file.write(json_beauty)
println(json_str)
}
static class Commands {
String name
String path
boolean includeCommandName
}
我在控制台中的输出确实像这样
[commands:[{"includeCommandName":true,"path":"${BUILTIN_EXE(command)}","name":"upload"},{"includeCommandName":true,"path":"${BUILTIN_EXE(command)}","name":"file_info"}]]
但是将它发送到 JSON 文件,它会像这样
{"commands":"[{\"includeCommandName\":true,\"path\":\"${BUILTIN_EXE(command)}\",\"name\":\"upload\"},{\"includeCommandName\":true,\"path\":\"${BUILTIN_EXE(command)}\",\"name\":\"file_info\"}]"}
我知道 JSON 反斜杠是一个特殊字符,所以我希望它只会出现在:"${BUILTIN_EXE(command)}周围,但它会出现在我什至没有反斜杠的任何地方。
解决方案
你没有“随机反斜杠”,你有双引号 JSON,因为你使用JsonOutput
了两次。json_str
是一个包含 JSON 的字符串,然后您将其作为 JSON 值包装在更多 JSON 中。
推荐阅读
- c - 套接字:操作不允许
- c# - 在 asp.net mvc 中使用存储过程的级联下拉菜单
- javascript - 在 Vue 控制的 DOM 结构中修改子 DOM 元素在多大程度上是不好的?(缓慢迁移到 Vue)
- arrays - 要散列的 Perl 数组数字
- c# - 用户无法在 wpf 数据网格上添加空的新行
- nginx - 无论语言如何重定向 URL
- android - Android gradle 在构建时更改文件
- single-sign-on - 来自 IdP 的 mod_auth_mellon 加密返回值
- sql - 为什么我在从时间戳中获取小时数时会得到不同的答案?
- react-native - 使用 Expo 的 react-navigation 标题折叠动画