Open lduffy69 opened 6 years ago
经测试,采用JSONPath格式仍然无法Set或者Read JsonPath.read(jsonString, "$.['valid.key.with.dot']");无法获取
JSONPath.set(json,"/root/a\\.b\\.c","hello")
目前只支持这种写法,这种写法不够友好,我想想如何改进
@wenshao this works for me 不过确实像您说的,写法不够友好,不能原生支持jsonpath的复杂格式,目前在考虑用com.jayway.jsonpath的下面的json-path包,不过这个框架目前没有内置fastjson中的缓存设计,需开发者单独去实现,期待您后面的改进
路径中包含数字的时候set会报错
Exception in thread "main" com.alibaba.fastjson.JSONPathException: unsupported set operation.class com.alibaba.fastjson.JSONObject
at com.alibaba.fastjson.JSONPath.setArrayItem(JSONPath.java:2146)
at com.alibaba.fastjson.JSONPath$ArrayAccessSegement.setValue(JSONPath.java:1504)
at com.alibaba.fastjson.JSONPath.set(JSONPath.java:347)
at com.alibaba.fastjson.JSONPath.set(JSONPath.java:261)
at com.alibaba.fastjson.JSONPath.set(JSONPath.java:385)
JSONPath除了支持使用“.”来分隔,还支持使用“[‘nodeName’]”。 路径中含有数字时,可以这样写:“$['aaa'][1]['bbb']['101']['value']”。 对应路径为:$.aaa[1].bbb.101.value 供参考。
原始json { "root": { "a.b.c": "" } } JSONPath.set(json,"/root/a.b.c","hello") 新json: { "root": { "a.b.c": "", "a": { "b": { "c": "Reject" } } } } 即按照a.b.c包结构新生成一系列节点