java中json与java的bean对象转换的问题
在将一个java对象转换为json格式的字符串遇到了难题,恳求各位大神指点迷津!如下描述:public class UploadObj {
private String ip;
private Item[][] input;
//setters and getters
}
----------------
public class Item {
private String key;
private String type;
private String value;
//setters and getters
}
上面建了两个bean,然后在一个TesJsonConfig类的main方法中执行下面红色字体的代码:
public class TestJsonConfig {
public static void main(String[] args) {
Item[][] input = new Item[2][1];
Item i0 = new Item();
i0.setKey("direction");
i0.setType("");
i0.setValue("upload");
input[0][0] = i0;
Item i1 = new Item();
i1.setKey("strategy");
i1.setType("");
i1.setValue("PDUyY8IdYtT==");
input[1][0] = i1;
UploadObj obj = new UploadObj();
obj.setIp("10.66.203.142");
obj.setInput(input);
JsonConfig config = new JsonConfig();
JSONObject json = JSONObject.fromObject(obj);
System.out.println(json.toString());
}
}
那么执行完后,控制台即可打印出obj被转化为json格式后的字符串形式,如下:
{"input":[ [{"key":"direction","type":"","value":"upload"}],
[{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}]
],
"ip":"10.66.203.142"
}
现在要想用json的API将这个json格式的字符串中key为direction的Item去掉,即过滤掉[{"key":"direction","type":"","value":"upload"}]这一部分,如上面绿色所示,只要如下的格式:
{"input":[ [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] ],
"ip":"10.66.203.142"}
我试了多种办法,包括jsonconfig的setExclude方法和PropertyFilter借口等却都没有完美地过滤出来,因此恳请各位高人指点呀! 这个问题难就难在UploadObj对象的Item二维数组 Java JSON Bean JavaEE JavaScript
补充:Java , Java EE