yaml
- 参考 Yaml解析(基于jyaml))
- json-yaml互转工具:https://www.bejson.com/json/json2yaml
- yaml多行配置规则参考:https://www.cnblogs.com/didispace/p/12524194.html
1 | # 配置与显示,都严格按段落展示 |
json
json5
json格式
- json格式校验工具:https://qqe2.com/
- 正确的json字符串
1 | { |
- 错误的json字符串
1 | { |
jsonpath
- 类似xpath获取json值
基于java工具类hutool取值和设值:
JSONUtil.getByPath
,JSONUtil.putByPath
1
2
3
4
5
6
7
8
9
10
11
12Map<String, Object> map = new HashMap<>();
map.put("a", "aa");
map.put("b", MiscU.Instance.toList(
MiscU.Instance.toMap("b1", "b11"),
MiscU.Instance.toMap("b1", "b21", "b2", "b22")));
map.put("c", MiscU.Instance.toMap("c1", "c11"));
System.out.println(JSONUtil.getByPath(new JSONObject(map), "a")); // aa
System.out.println(JSONUtil.getByPath(new JSONObject(map), "b[1]")); // {"b1":"b21","b2":"b22"}
// System.out.println(JSONUtil.getByPath(new JSONObject(map), "b[].b1")); // 不支持
System.out.println(JSONUtil.getByPath(new JSONObject(map), "c.c1")); // c11
System.out.println(JSONUtil.getByPath(new JSONObject(map), "c.c2")); // null- 提供Javascript、Java、Python、PHP相关类库:https://jmespath.org/libraries.html
- js版本的jmespath
- 问题
- 好像不能对json进行修改
使用
1
2
3
4
5
6
7
8// 安装
npm install jmespath -S
import jmespath from 'jmespath'
// 特殊key取值方式(v0.16.0),官网引入的jmespath版本(v2015)可以通过"a.b-1"进行取值
// js库不支持search(path, obj)函数(python库支持)
jmespath.search({'a': {'b-1': 1}}, 'a."b-1"') // 1解决无法修改json(建议直接使用 lodash.merge 等函数)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89let originReport = {
extMapJson: {
print: true,
event: {
"checkbox-all": "() => {}"
}
}
}
const assignReport = {
extMapJson: {
print: false,
prop: {
height: "100px"
}
}
}
deepAssignReport(originReport, assignReport, '', assignReport)
// originReport = JSON.parse(JSON.stringify(originReport))
/*
{
extMapJson: {
print: false,
event: {
"checkbox-all": "() => {}"
},
prop: {
height: "100px"
}
}
}
*/
console.log(originReport)
deepAssignReport (originReport, assignReport, prefix, obj) {
if (obj == null || typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'function') {
let originObj = originReport
const jkeyArr = this.getJkeyArr(prefix)
if (jkeyArr[0]) {
originObj = jmespath.search(originReport, jkeyArr[0])
if (originObj == null) {
// eg: extMapJson.prop 为空
originObj = {}
const jkeyArrParent = this.getJkeyArr(jkeyArr[0])
const parentObj = jmespath.search(originReport, jkeyArrParent[0])
parentObj[jkeyArrParent[1]] = originObj
}
}
originObj[jkeyArr[1]] = obj
} else if (obj && typeof obj === 'object') {
if (obj instanceof Array) {
// todo...
} else {
const keys = Object.getOwnPropertyNames(obj)
for (const key of keys) {
if (key === '__ob__') {
continue
}
let jkey = key
if (key.indexOf('-') >= 0) {
jkey = '"' + key + '"'
}
const childPrefix = prefix ? prefix + '.' + jkey : jkey
const childObj = jmespath.search(assignReport, childPrefix)
this.deepAssignReport(originReport, assignReport, childPrefix, childObj)
}
}
}
},
function getJkeyArr (jkey) {
let prefix = ''
let last = ''
let arr = jkey.split('.')
if (arr.length < 2) {
return [prefix, jkey]
} else {
// func.doCheckbox
// event."checkbox-all"
// tailTables[0].prop."footer-method"
last = arr[arr.length - 1]
prefix = arr.slice(0, arr.length - 1).join('.')
}
if (last.indexOf('[') > 0) {
prefix = prefix + '.' + last.substr(0, last.indexOf('['))
last = last.substr(last.indexOf('[') + 1).replaceAll(']', '')
}
last = last.replaceAll('"', '')
return [prefix, last]
},
JsonPath
- 提供Javascript、Java、Python、PHP相关类库;js库没找到
- java版本的json-path