json串中如何把值为null的属性过滤掉
例如:{"a":1,"b":2,"c":null,"d":[{"aa":[{"aaa":null,"bbb":222}],"bb":22,"cc":null}]}
需要返回:
{"a":1,"b":2,"d":[{"aa":[{"bbb":222}],"bb":22}]}
求大大们指点....
[解决办法]
String s = ":{"a":1,"b":2,"c":null,"d":[{"aa":[{"aaa":null,"bbb":222}],"bb":22,"cc":null}]}";
// 把形如:"a":null, 把后面有个逗号的给替换掉,这个替换掉,如果最后一个,cc:null是无法替换掉的
s = s.replaceAll(""[^"]+":null,", "");
// 把形如:,"a":null 把前面有个逗号的给替换掉
s = s.replaceAll(","[^"]+":null", "");
System.out.println(s);
:{"a":1,"b":2,"d":[{"aa":[{"bbb":222}],"bb":22}]}