JSON格式化各种数据类型
2013-09-26 14:56
381 查看
JSON格式化各种数据类型
JSON可格式化的数据:
普通的JavaBean(有些特殊处理的不能被格式化)
Map
List
JSON转换所需的jar包:
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk13.jar
数据格式化为JSONObject:
输出结果:
{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":35,"month":8,"seconds":56,"time":1380173756175,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}
输出结果:
{"type":"map","textBean":{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":37,"month":8,"seconds":57,"time":1380173877438,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}}
输出结果:
[{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":43,"month":8,"seconds":9,"time":1380174189900,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}},{"p1":"tb1111","p2":"tb2222"}]
过滤某个字段:
解决JSON乱码问题和out输出:
如果转换的时候报错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException,有可能是因为你的JavaBean中有Date类型的字段,解决方法如下:
1.可以过滤掉Date类型的那个字段
2.解决方法:添加自定义的日期格式转化类
自定义的日期格式转化类:
JSON可格式化的数据:
普通的JavaBean(有些特殊处理的不能被格式化)
Map
List
JSON转换所需的jar包:
commons-beanutils.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-lang-2.4.jar
commons-logging-1.1.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk13.jar
数据格式化为JSONObject:
//建2个简单的javaBean(TextBean.java,TextBean1.java)具体代码如下: public class TextBean { private String propertys1; private String propertys2; private Date propertys3; private TextBean1 tb1; public String getPropertys1() { return propertys1; } public void setPropertys1(String propertys1) { this.propertys1 = propertys1; } public String getPropertys2() { return propertys2; } public void setPropertys2(String propertys2) { this.propertys2 = propertys2; } public Date getPropertys3() { return propertys3; } public void setPropertys3(Date propertys3) { this.propertys3 = propertys3; } public TextBean1 getTb1() { return tb1; } public void setTb1(TextBean1 tb1) { this.tb1 = tb1; } }
public class TextBean1 { private String p1; private String p2; public String getP1() { return p1; } public void setP1(String p1) { this.p1 = p1; } public String getP2() { return p2; } public void setP2(String p2) { this.p2 = p2; } }
//普通JavaBean格式化为JSON格式数据 public static void main(String[] args) { TextBean tb=new TextBean(); TextBean1 tb1=new TextBean1(); tb1.setP1("tb1111"); tb1.setP2("tb2222"); tb.setTb1(tb1); tb.setPropertys1("abcdef"); tb.setPropertys2("123456"); tb.setPropertys3(new Date()); JSONObject jsonObj=JSONObject.fromObject(tb); System.out.println(jsonObj); }
输出结果:
{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":35,"month":8,"seconds":56,"time":1380173756175,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}
//Map格式化为JSON格式数据 public static void main(String[] args) { TextBean tb=new TextBean(); TextBean1 tb1=new TextBean1(); tb1.setP1("tb1111"); tb1.setP2("tb2222"); tb.setTb1(tb1); tb.setPropertys1("abcdef"); tb.setPropertys2("123456"); tb.setPropertys3(new Date()); Map map=new HashMap(); map.put("textBean", tb); map.put("type", "map"); JSONObject jsonObj=JSONObject.fromObject(map); System.out.println(jsonObj); }
输出结果:
{"type":"map","textBean":{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":37,"month":8,"seconds":57,"time":1380173877438,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}}}
//List格式化为JSON格式数据 public static void main(String[] args) { TextBean tb=new TextBean(); TextBean1 tb1=new TextBean1(); tb1.setP1("tb1111"); tb1.setP2("tb2222"); tb.setTb1(tb1); tb.setPropertys1("abcdef"); tb.setPropertys2("123456"); tb.setPropertys3(new Date()); Map map=new HashMap(); map.put("type", "map"); List list=new ArrayList(); list.add(tb); list.add(tb1); JSONArray jsonArray=JSONArray.fromObject(list); System.out.println(jsonArray); }
输出结果:
[{"propertys1":"abcdef","propertys2":"123456","propertys3":{"date":26,"day":4,"hours":13,"minutes":43,"month":8,"seconds":9,"time":1380174189900,"timezoneOffset":-480,"year":113},"tb1":{"p1":"tb1111","p2":"tb2222"}},{"p1":"tb1111","p2":"tb2222"}]
过滤某个字段:
//过滤掉time和name字段JsonConfig jsonConfig=new JsonConfig(); jsonConfig.setExcludes(new String[]{"time","name"}); JSONArray jsonArray=JSONArray.fromObject(students,jsonConfig);
解决JSON乱码问题和out输出:
//解决JSON乱码问题 request.setCharacterEncoding("UTF-8"); response.setContentType("text/json"); response.setCharacterEncoding("UTF-8");
//页面接收JSON PrintWriter out=response.getWriter(); out.write(json); out.flush();
如果转换的时候报错net.sf.json.JSONException: java.lang.reflect.InvocationTargetException,有可能是因为你的JavaBean中有Date类型的字段,解决方法如下:
1.可以过滤掉Date类型的那个字段
2.解决方法:添加自定义的日期格式转化类
自定义的日期格式转化类:
/** * 可以自己写一个JsonValueProcessor实现类来实现JsonValueProcessor接口 * 实现processArrayValue方法和processObjectValue方法 */ public class JsonValueProcessorImpl implements JsonValueProcessor { private String format = "yyyy-MM-dd"; public JsonValueProcessorImpl() { } public JsonValueProcessorImpl(String format) { this.format = format; } public Object processArrayValue(Object value, JsonConfig jsonConfig) { String[] obj = {}; if (value instanceof Date[]) { SimpleDateFormat sf = new SimpleDateFormat(format); Date[] dates = (Date[]) value; obj = new String[dates.length]; for (int i = 0; i < dates.length; i++) { obj[i] = sf.format(dates[i]); } } return obj; } public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) { if (value instanceof Date) { String str = new SimpleDateFormat(format).format((Date) value); return str; } if(null != value){ return value.toString(); } return ""; } public String getFormat() { return format; } public void setFormat(String format) { this.format = format; } }
/** * 然后定义一个JsonConfig,设置JsonConfig的相应属性: */ JsonConfig config = new JsonConfig(); config.setExcludes(new String[]{"handler","hibernateLazyInitializer"}); //字段过滤 config.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT); //防止自包含 config.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl()); //引用日期格式转换类
//转换的时候可以这么调用 JSONArray.fromObject(dutyList,config);
相关文章推荐
- 使用Gson解析Json得到各种类型的数据
- 各种类型的JSON格式化
- asp.net中各种类型的JSON格式化
- c# 【MVC】WebApi返回各种类型(图片/json数据/字符串)
- 解决easyui 中显示Date类型数据为[object Object],json中格式化日期
- java 各种类型数据,集合对象封装 成json
- springmvc访问返回数据类型格式化json报http status 406 和json时间格式化
- 在java中将各种类型数据转化为json和从json中取数据
- java 各种类型数据,集合对象封装 成json
- c# 【MVC】WebApi返回各种类型(图片/json数据/字符串)
- handlebars获取json数据(集合对象)及各种自定义helper数据格式化工具
- struts+ajax返回json数据类型的各种情形及对应的Demo
- 返回json数据,格式化日期类型去掉时分秒
- C#各种数据类型转json字符串(转贴)
- JS处理各种JSON类型数据
- asp.net中各种类型的JSON格式化
- 使用Newtonsoft.json 解决 Asp.Net MVC DateTime类型数据Json格式化问题
- C#.NET WebApi返回各种类型(图片转二进制流或byte /json数据/字符串)
- C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte
- 【转载】C#.NET WebApi返回各种类型(图片/json数据/字符串),.net图片转二进制流或byte