转换之前需要知道什么是json
json类似于map集合:键值对的方式存在,是一种轻量级数据交互格式。
eg:
{name:" ",age:15}
[{name:" ",age:15},{name:" ",age:15}]
使用工具生成json
JsonLib来转换对象
JsonArray 将数组或集合转化为json
JSONObject 将对象装换为json格式
依赖的java包
编写一个javabean
package com.baidu;public class User { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
测试类:
package com.baidu;import java.util.ArrayList;import net.sf.json.JSONArray;public class Demo { public static void main(String[] args) { User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); JSONArray fromObject = JSONArray.fromObject(array); System.out.println(fromObject.toString()); }}
输出结果:
[{"id":11,"name":"风清扬"},{"id":12,"name":"小师妹"}]
测试类:(过滤id)
package com.baidu;import java.util.ArrayList;import net.sf.json.JSONArray;import net.sf.json.JsonConfig;public class Demo { public static void main(String[] args) { User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); JsonConfig js=new JsonConfig(); js.setExcludes(new String[]{"id"}); JSONArray fromObject = JSONArray.fromObject(array,js); System.out.println(fromObject.toString()); }}
输出结果:
[{"name":"风清扬"},{"name":"小师妹"}]
使用阿里巴巴的jar包 fastjson.jar
测试类
package com.baidu;import java.util.ArrayList;import com.alibaba.fastjson.JSON;public class Demo { public static void main(String[] args) { User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); String arr = JSON.toJSONString(array); System.out.println(arr); }}
输出结果:
[{"id":11,"name":"风清扬"},{"id":12,"name":"小师妹"}]
使用过滤器(过滤id)
package com.baidu;import java.util.ArrayList;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;public class Demo { public static void main(String[] args) { User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); //需要什麼写什么 SimplePropertyPreFilter sdfs=new SimplePropertyPreFilter(User.class,"name"); String arr = JSON.toJSONString(array,sdfs); System.out.println(arr); }}
输出结果:
[{"name":"风清扬"},{"name":"小师妹"}]
细致过滤某个值
@Test public void JsonTest1(){ User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); SerializeFilter sf=new PropertyFilter() {//具体的过滤器 @Override public boolean apply(Object object, String name, Object value) { /*if("小师妹".equals(value)){ return false; }*/ System.out.println(object);//表示json对象 System.out.println(name);//表示字段 System.out.println(value);//表示字段的值 return true; } }; String arr = JSON.toJSONString(array,sf); //System.out.println(arr); }
使用Jockson
@Test public void JsonTest1() throws JsonGenerationException, JsonMappingException, IOException{ User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); ObjectMapper om=new ObjectMapper(); String strjson = om.writeValueAsString(array); System.out.println(strjson); }
输出结果:
[{"id":11,"name":"风清扬"},{"id":12,"name":"小师妹"}]
对某个字段进行格式化
@Test public void JsonTest1() throws JsonGenerationException, JsonMappingException, IOException{ User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); ObjectMapper om=new ObjectMapper(); om.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));//设置日期格式化器 String strjson = om.writeValueAsString(array); System.out.println(strjson); }
使用过滤器进行对字段进行过滤
@Test public void JsonTest1() throws JsonGenerationException, JsonMappingException, IOException{ User u=new User(); u.setId(11); u.setName("风清扬"); User u1=new User(); u1.setId(12); u1.setName("小师妹"); ArrayListarray=new ArrayList (); array.add(u); array.add(u1); ObjectMapper om=new ObjectMapper(); //处理过滤属性 //只显示这个属性 //[{"id":11},{"id":12}] FilterProvider f=new SimpleFilterProvider().addFilter("UserFilter",SimpleBeanPropertyFilter.filterOutAllExcept("id")); //[{"name":"风清扬"},{"name":"小师妹"}] 不包含这个属性 //FilterProvider f=new SimpleFilterProvider().addFilter("UserFilter",SimpleBeanPropertyFilter.serializeAllExcept("id")); om.setFilters(f); String strjson = om.writeValueAsString(array); System.out.println(strjson); }