当前位置:编程学习 > JAVA >>

急!急!急! --关于java中json问题

在将一个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二维数组

JSON java  javascript 转换 类 --------------------编程问答-------------------- 要是有个这样的输入:
{"input":[ [{"key":"direction","type":"","value":"upload"},{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}],       
           [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="},{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] 
         ],           
 "ip":"10.66.203.142"}
是不是要过滤成这样:
{"input":[ [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}],       
           [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="},{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] 
         ],           
 "ip":"10.66.203.142"} --------------------编程问答-------------------- JSONObject、JSONArray一层一层解析下去,到指定的层次,get("key")判断一下;
这步可以放到生成json字符串之前来做吗?这样解析起来有点麻烦哎。 --------------------编程问答-------------------- 主要是LZ需要把
{"input":[ [{"key":"direction","type":"","value":"upload"}],       
           [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] 
         ],           
  "ip":"10.66.203.142"}
转换成
{"input":[ [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] ],           
  "ip":"10.66.203.142"
}
而不是
{"input":[ [],       
           [{"key":"strategy","type":"","value":"PDUyY8IdYtT=="}] 
         ],           
  "ip":"10.66.203.142"
}
补充:Java ,  Java相关
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,