alibaba / fastjson

FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
https://github.com/alibaba/fastjson2/wiki/fastjson_1_upgrade_cn
Apache License 2.0
25.72k stars 6.5k forks source link

JSONObject字段中含有“.”号时,set数据异常 #1793

Open lduffy69 opened 6 years ago

lduffy69 commented 6 years ago

原始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包结构新生成一系列节点

lduffy69 commented 6 years ago

经测试,采用JSONPath格式仍然无法Set或者Read JsonPath.read(jsonString, "$.['valid.key.with.dot']");无法获取

wenshao commented 6 years ago
JSONPath.set(json,"/root/a\\.b\\.c","hello")

目前只支持这种写法,这种写法不够友好,我想想如何改进

lduffy69 commented 6 years ago

@wenshao this works for me 不过确实像您说的,写法不够友好,不能原生支持jsonpath的复杂格式,目前在考虑用com.jayway.jsonpath的下面的json-path包,不过这个框架目前没有内置fastjson中的缓存设计,需开发者单独去实现,期待您后面的改进

ahypnos commented 6 years ago

路径中包含数字的时候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)
wolf51983 commented 5 years ago

JSONPath除了支持使用“.”来分隔,还支持使用“[‘nodeName’]”。 路径中含有数字时,可以这样写:“$['aaa'][1]['bbb']['101']['value']”。 对应路径为:$.aaa[1].bbb.101.value 供参考。