java的json问题实习遇到的问题及解决以下问题怎么解决

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
您的访问请求被拒绝 403 Forbidden - ITeye技术社区
您的访问请求被拒绝
亲爱的会员,您的IP地址所在网段被ITeye拒绝服务,这可能是以下两种情况导致:
一、您所在的网段内有网络爬虫大量抓取ITeye网页,为保证其他人流畅的访问ITeye,该网段被ITeye拒绝
二、您通过某个代理服务器访问ITeye网站,该代理服务器被网络爬虫利用,大量抓取ITeye网页
请您点击按钮解除封锁&君,已阅读到文档的结尾了呢~~
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
java 中的json 解码和编码有关问题
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口java的json问题遇到以下问题怎么解决_百度知道java与json互相转换(解决日期问题)
java与json互相转换(解决日期问题)
JSON&即&JavaScript&Object&Natation,它是一种轻量级的数据交换格式,非常适合于服务器与&JavaScript&的交互。本文主要讲解下java和JSON之间的转换,特别是解决互相转换遇到日期问题的情况。&&&&一、需要相关的jar包:&&&&json-lib-xxx.jar&&&&ezmorph-xxx.jar&&&&commons-httpclient-xxx.jar&&&&commons-lang-xxx.jar&&&&commons-logging-xxx.jar&&&&commons-collections-xxx.jar&&&&上面的包可以从下面的连接下载:&&&&http://commons.apache.org/index.html&&&&http://json-lib.sourceforge.net&&&&http://ezmorph.sourceforge.net&&&二、java-》JSON&&&&&<SPAN style="COLOR: #.List-》JSONview&plaincopy&to&clipboardprint?List&String&&list&=&new&ArrayList&String&();list.add("apple");list.add("orange");JSONArray&jarr&=&JSONArray.fromObject(list);System.out.println("list-&json:"&+&jarr.toString());&&&&打印结果:list-&json:["apple","orange"]&&&&&<SPAN style="COLOR: #.Map-》JSONview&plaincopy&to&clipboardprint?Map&String,&Object&&map&=&new&HashMap&String,&Object&();map.put("name",&"Michael");map.put("baby",&new&String[]&{&"Lucy",&"Lily"&});map.put("age",&<SPAN style="COLOR: #);JSONObject&jo&=&JSONObject.fromObject(map);System.out.println("map-&json:"&+&jo.toString());&&&&打印结果:map-&json:{"age":<SPAN style="COLOR: #,"name":"Michael","baby":["Lucy","Lily"]}&&&&<SPAN style="COLOR: #.bean-&JSONview&plaincopy&to&clipboardprint?JsonBean&bean&=&new&JsonBean();bean.setName("NewBaby");bean.setAge(<SPAN style="COLOR: #);bean.setBorn(new&Date());jo&=&JSONObject.fromObject(bean);System.out.println("bean-&json:"&+&jo.toString());打印结果:bean-&json:{"age":<SPAN style="COLOR: #,"born":{"date":<SPAN style="COLOR: #,"day":<SPAN style="COLOR: #,"hours":<SPAN style="COLOR: #,"minutes":<SPAN style="COLOR: #,"month":<SPAN style="COLOR: #,"seconds":<SPAN style="COLOR: #,"time":<SPAN style="COLOR: #,"timezoneOffset":-<SPAN style="COLOR: #0,"year":<SPAN style="COLOR: #0},"name":"NewBaby"}&&&这时你会发现它把bean对象里的util.Date这个类型的所有属性一一转换出来。在实际运用过程中,大多数情况下我们希望能转化为yyyy-MM-dd这种格式,下面就讲一讲如何实现:&&&首先要写一个新的类JsonDateValueProcessor如下:view&plaincopy&to&clipboardprint?/***&JSON&日期格式处理(java转化为JSON)*&@author&Michael&sun*/public&class&JsonDateValueProcessor&implements&JsonValueProcessor&{&&&&/**&&&&&*&datePattern&&&&&*/&&&&private&String&datePattern&=&"yyyy-MM-dd";&&&&/**&&&&&*&JsonDateValueProcessor&&&&&*/&&&&public&JsonDateValueProcessor()&{&&&&&&&&super();&&&&}&&&&/**&&&&&*&@param&format&&&&&*/&&&&public&JsonDateValueProcessor(String&format)&{&&&&&&&&super();&&&&&&&&this.datePattern&=&&&&&}&&&&/**&&&&&*&@param&value&&&&&*&@param&jsonConfig&&&&&*&@return&Object&&&&&*/&&&&public&Object&processArrayValue(Object&value,&JsonConfig&jsonConfig)&{&&&&&&&&return&process(value);&&&&}&&&&/**&&&&&*&@param&key&&&&&*&@param&value&&&&&*&@param&jsonConfig&&&&&*&@return&Object&&&&&*/&&&&public&Object&processObjectValue(String&key,&Object&value,&&&&&&&&&&&&JsonConfig&jsonConfig)&{&&&&&&&&return&process(value);&&&&}&&&&/**&&&&&*&process&&&&&*&@param&value&&&&&*&@return&&&&&*/&&&&private&Object&process(Object&value)&{&&&&&&&&try&{&&&&&&&&&&&&if&(value&instanceof&Date)&{&&&&&&&&&&&&&&&&SimpleDateFormat&sdf&=&new&SimpleDateFormat(datePattern,&&&&&&&&&&&&&&&&&&&&&&&&Locale.UK);&&&&&&&&&&&&&&&&return&sdf.format((Date)&value);&&&&&&&&&&&&}&&&&&&&&&&&&return&value&==&null&?&""&:&value.toString();&&&&&&&&}&catch&(Exception&e)&{&&&&&&&&&&&&return&"";&&&&&&&&}&&&&}&&&&/**&&&&&*&@return&the&datePattern&&&&&*/&&&&public&String&getDatePattern()&{&&&&&&&&return&dateP&&&&}&&&&/**&&&&&*&@param&pDatePattern&the&datePattern&to&set&&&&&*/&&&&public&void&setDatePattern(String&pDatePattern)&{&&&&&&&&datePattern&=&pDateP&&&&}}测试代码:view&plaincopy&to&clipboardprint?JsonBean&bean&=&new&JsonBean();bean.setName("NewBaby");bean.setAge(<SPAN style="COLOR: #);bean.setBorn(new&Date());JsonConfig&jsonConfig&=&new&JsonConfig();jsonConfig.registerJsonValueProcessor(Date.class,new&JsonDateValueProcessor());JSONObject&jo&=&JSONObject.fromObject(bean,&jsonConfig);System.out.println("bean-&json:"&+&jo.toString());打印结果:bean-&json:{"age":<SPAN style="COLOR: #,"born":"<SPAN style="COLOR: #10-03-10","name":"NewBaby"}这就能得到我们想要的结果了。三、JSON-》java<SPAN style="COLOR: #.如何把json的yyyy-MM-dd的转换为Bean中的util.Date类型:view&plaincopy&to&clipboardprint?JSONUtils.getMorpherRegistry().registerMorpher(&&&&&&&&&&new&DateMorpher(new&String[]&{&"yyyy-MM-dd"&}));String&jsonStr&=&"[{\"name\":&\"husband\",&\"age\":&\"<SPAN style="COLOR: #\",&\"born\":&\"<SPAN style="COLOR: #84-<SPAN style="COLOR: #-<SPAN style="COLOR: #\"},{\"name\":&\"wife\",&\"age\":&\"<SPAN style="COLOR: #\",&\"born\":&\"<SPAN style="COLOR: #90-<SPAN style="COLOR: #-<SPAN style="COLOR: #\"}]";Collection&JsonBean&&list&=&JSONArray.toCollection(JSONArray.fromObject(jsonStr),&JsonBean.class);&&&&&&//DateUtil.getFormatDate(date,fmtstr)日期转字符串这里不再写代码了for&(JsonBean&o&:&list)&{&&&System.out.println(DateUtil.getFormatDate(o.getBorn(),&"yyyy-MM-dd"));}打印结果:&&&&&&&&&&&<SPAN style="COLOR: #84-<SPAN style="COLOR: #-<SPAN style="COLOR: #&&&&&&&&&&<SPAN style="COLOR: #90-<SPAN style="COLOR: #-<SPAN style="COLOR: #&&&<SPAN style="COLOR: #.&JSON-》List、&Mapview&plaincopy&to&clipboardprint?String&listStr&=&"[\"apple\",\"orange\"]";Collection&String&&strlist&=&JSONArray.toCollection(JSONArray.fromObject(listStr));for&(String&str&:&strlist)&{&&&&System.out.println(str);}String&mapStr&=&"{\"age\":30,\"name\":\"Michael\",\"baby\":[\"Lucy\",\"Lily\"]}";Map&String,&Object&&map&=&(Map)&JSONObject.toBean(JSONObject&&&&&&&&&&&&&&&&.fromObject(mapStr),&Map.class);for&(Entry&String,&Object&&entry&:&map.entrySet())&{&&&&System.out.println(entry.getKey()&+&"&"&+&entry.getValue());}打印结果:&&&&&&&&&&&&apple&&&&&&&&&&&orange&&&&&&&&&&&name&Michael&&&&&&&&&&&age&<SPAN style="COLOR: #&&&&&&&&&&&baby&[Lucy,&Lily]
发表评论:
TA的推荐TA的最新馆藏[转]&[转]&java的json问题遇到以下问题怎么解决_百度知道

我要回帖

更多关于 遇到了以下问题 的文章

 

随机推荐