- 浏览: 26361 次
- 性别:
- 来自: 西安
文章分类
最新评论
收藏列表
- 全部 [4]
- 默认 [3]
- 二进制数据,换成图片 [1]
标题 | 标签 | 来源 | |
js读取本地xml文件 兼容IE Firfox Chrome 等浏览器 | js读取本地xml文件 兼容IE Firfox Chrome 等浏览器 | ||
var isIE = document.all; if(isIE) { var oXmlDom = new ActiveXObject("Microsoft.XMLDOM"); } else { var oXmlDom = document.implementation.createDocument("","",null); Element.prototype.selectNodes = function(sXpath){ var oEvaluator = new XPathEvaluator(); var oResult = oEvaluator.evaluate(sXpath,this,null,XPathResult.ORDERED_NODE_ITERATOR_TYPE,null); var aNodes = new Array; if(oResult!=null) { var oElement = oResult.iterateNext(); while(oElement) { aNodes.push(oElement); oElement = oResult.iterateNext(); } } return aNodes; }; } if(!oXmlDom)oXmlDom = new ActiveXObject("Microsoft.XMLDOM"); function loadXML() { try{ oXmlDom.async="false" if(isIE) { oXmlDom.setProperty("SelectionLanguage","XPath"); } oXmlDom.load("/XML/City.xml"); } catch(e) { var xhr = new XMLHttpRequest(); xhr.open("GET", "/XML/City.xml", false); xhr.send(null); oXmlDom = xhr.responseXML; } } |
|||
Google Gson解析Json数据应用实例 | Google Gson解析Json数据应用实例 | ||
1.需要的Jar包 1) Google Gson(gson-xxx.jar)下载地址:http://code.google.com/p/google-gson/downloads/list 2)JUnit4 2. 应用实例代码 下载地址:http://download.csdn.net/source/3499627 包括如下类: 1)普通JavaBean类/带日期属性的JavaBean类:JavaBean.java/DateBean.java 2)日期序列/反序列工具类:DateSerializerUtils.java、DateDeserializerUtils.java 3)测试类GsonTester.java 具体代码: 1)JavaBean类/DateBean类 JavaBean属性:String id、String name、int age、String addr; DateBean属性:String id、String name、int age、java.util.Date date; 2) DateSerializerUtils.java package com.lupeng.javase.json.util; import java.lang.reflect.Type; import java.util.Date; import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; /** * 日期解序列实用工具类 * @author Lupeng * @date 2011-08-06 */ public class DateSerializerUtils implements JsonSerializer<java.util.Date>{ @Override public JsonElement serialize(Date date, Type type, JsonSerializationContext content) { return new JsonPrimitive(date.getTime()); } } DateDeserializerUtils.java package com.lupeng.javase.json.util; import java.lang.reflect.Type; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; /** * 日期序列化实用工具类 * @author Lupeng * @date 2011-08-06 */ public class DateDeserializerUtils implements JsonDeserializer<java.util.Date>{ @Override public java.util.Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { return new java.util.Date(json.getAsJsonPrimitive().getAsLong()); } } 3)测试类GsonTester.java package com.lupeng.javase.apps.json; import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.junit.Before; import org.junit.Test; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.lupeng.javase.json.bean.DateBean; import com.lupeng.javase.json.bean.JavaBean; import com.lupeng.javase.json.util.DateDeserializerUtils; import com.lupeng.javase.json.util.DateSerializerUtils; /** * Google Gson解析Json数据实例 * * 1.Bean、Json转换 testBeanJson() * 2.List -> Json转换 testList2Json() * 3.泛型List、Json相互转换 testGenericList2Json() * 4.Map -> Json转换 testMap2Json() * 5.泛型Map、Json相互转换 testGenericMap2Json() * 6.带日期属性Bean、Json转换 testDateBeanJson() * 7.带日期属性泛型List、Json转换 * testDateGenericListJson() * * @author Lupeng * @create 2011-08-05 * @modify 2011-08-06 */ @SuppressWarnings("unchecked") public class GsonTester { private Gson gson = null; private GsonBuilder gsonBuilder = null; @Before public void setUp() { gson = new Gson(); gsonBuilder = new GsonBuilder(); } /** * JavaBean、Json相互转换 */ @Test public void testBeanJson() { JavaBean bean = new JavaBean("1001", "scott", 20, "TL"); // Bean -> Json String json = gson.toJson(bean); System.out.println(json); // Json -> Bean bean = gson.fromJson(json, JavaBean.class); System.out.println(bean); } /** * List转换成Json字符串 */ @Test public void testList2Json() { // List List list = new ArrayList(); for(int i = 0; i < 5; i++) { list.add("element" + i); } System.out.println(list); // List -> Json String json = gson.toJson(list); System.out.println(json); } /** * 泛型List、Json相互转换 */ @Test public void testGenericListJson() { // 泛型List List<JavaBean> list = new ArrayList<JavaBean>(); for(int i = 0; i < 3; i++) { JavaBean user = new JavaBean("100" + i, "name" + i, 20 + i, "BJ" + i); list.add(user); } System.out.println(list); // 泛型List -> Json java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<JavaBean>>(){}.getType(); String json = gson.toJson(list, type); System.out.println(json); // Json -> 泛型List List<JavaBean> users = gson.fromJson(json.toString(), type); System.out.println(users); } /** * Map转换成Json字符串 */ @Test public void testMap2Json() { // Map数据 Map map = new HashMap(); map.put("id", "1001"); map.put("name", "scott"); map.put("age", 20); map.put("addr", "BJ"); System.out.println(map); // Map -> Json String json = gson.toJson(map); System.out.println(json); } /** * 泛型Map、Json相互转换 */ @Test public void testGenericMapJson() { // 泛型Map数据 Map<String, JavaBean> map = new HashMap<String, JavaBean>(); for(int i = 0; i < 5; i++) { JavaBean user = new JavaBean("100" + i, "name" + i, 20 + i, "LN" + i); map.put("100" + i, user); } System.out.println(map); // 泛型Map -> Json java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<Map<String, JavaBean>>(){}.getType(); String json = gson.toJson(map, type); System.out.println(json); // Json -> Map Map<String, JavaBean> users = gson.fromJson(json.toString(), type); System.out.println(users); } /** * 带日期类型Bean、Json相互转换 */ @Test public void testDateBeanJson() { // 日期Bean数据 DateBean bean = new DateBean("1001", "scott", 20, new Date()); // Bean(带日期属性) -> Json gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateSerializerUtils()).setDateFormat(DateFormat.LONG).create(); String json = gson.toJson(bean); System.out.println(json); // Json -> Bean(带日期类型属性) gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateDeserializerUtils()).setDateFormat(DateFormat.LONG).create(); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<DateBean>(){}.getType(); DateBean b = gson.fromJson(json, type); System.out.println(b); } /** * 泛型日期List、Json相互转换 */ @Test public void testDateGenericListJson() { // 泛型日期List List<DateBean> list = new ArrayList<DateBean>(); for(int i = 0; i < 3; i++) { DateBean user = new DateBean("100" + i, "name" + i, 20 + i, new Date()); list.add(user); } System.out.println(list); // 泛型日期List -> Json gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateSerializerUtils()).setDateFormat(DateFormat.LONG).create(); java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<DateBean>>(){}.getType(); String json = gson.toJson(list, type); System.out.println(json); // Json -> 泛型日期List gson = gsonBuilder.registerTypeAdapter(java.util.Date.class, new DateDeserializerUtils()).setDateFormat(DateFormat.LONG).create(); List<DateBean> users = gson.fromJson(json.toString(), type); System.out.println(users); } } |
|||
写一个toJSON方法将Map对象转换成JSON字符串 | 写一个toJSON方法将Map对象转换成JSON字符串 | ||
/** * * @author gavin * */ public class Gson{ public static String toJson(Map<String,String> map){ Set<String> keys = map.keySet(); String key = ""; String value = ""; StringBuffer jsonBuffer = new StringBuffer(); jsonBuffer.append("{"); for(Iterator<String> it = keys.iterator();it.hasNext();){ key = (String)it.next(); value = map.get(key); jsonBuffer.append(key+":"+value); if(it.hasNext()){ jsonBuffer.append(","); } } jsonBuffer.append("}"); return jsonBuffer.toString(); } public static String toJson2(Map<String,String> map){ Set<Map.Entry<String, String>> entrys = map.entrySet(); Map.Entry<String, String> entry = null; String key = ""; String value = ""; StringBuffer jsonBuffer = new StringBuffer(); jsonBuffer.append("{"); for(Iterator<Map.Entry<String, String>> it = entrys.iterator();it.hasNext();){ entry = (Map.Entry<String, String>)it.next(); key = entry.getKey(); value = entry.getValue(); jsonBuffer.append(key+":"+value); if(it.hasNext()){ jsonBuffer.append(","); } } jsonBuffer.append("}"); return jsonBuffer.toString(); } public static void main(String args[]){ Map<String,String> map = new TreeMap<String,String>(); map.put("1", "zhangyi"); map.put("2", "zhanger"); map.put("3", "zhangsan"); map.put("4", "zhangsi"); map.put("5", "zhangwu"); System.out.println(toJson(map)); System.out.println(toJson2(map)); } } 运行结果: {1:zhangyi,2:zhanger,3:zhangsan,4:zhangsi,5:zhangwu} {1:zhangyi,2:zhanger,3:zhangsan,4:zhangsi,5:zhangwu} 你的这个不行,你加一个这个试试。 map.put("attributes", "{url=menu/toMaintain}"); map.put("children", "[{id=102, text=路灯管理-根据局所ID查看配电箱, attributes={url=adminManage/findBoxByBureauId}, checked=true}, {id=103, text=路灯管理-根据局所ID查看道路, attributes={url=adminManage/findWayByBureauId}, checked=true}, {id=104, text=路灯管理-检查是否有双灯头, attributes={url=adminManage/findLampBydisboxId}, checked=true}, {id=101, text=局所管理-异步查询上级局所, attributes={url=bureau/getAllBurears}, checked=true}, {id=1, text=维护人员管理主页, attributes={url=man/tomaintainmanindex}, checked=true}, {id=2, text=维护人员-添加, attributes={url=man/addMan}, checked=true}, {id=3, text=维护人员-删除, attributes={url=man/deleteMan}, checked=true}, {id=4, text=维护人员-修改, attributes={url=man/modifyMan}, checked=true}, {id=5, text=维护人员-查询所有, attributes={url=man/getCurrentPageOfMan}, checked=true}, {id=6, text=维护人员与配电箱关联管理主页, attributes={url=manbox/tomaintainboxindex}, checked=true}, {id=7, text=维护人员与配电箱关联管理-添加, attributes={url=manbox/addMainBox}, checked=true}, {id=8, text=维护人员与配电箱关联管理-删除, attributes={url=manbox/deleteMainBox}, checked=true}, {id=9, text=维护人员与配电箱关联管理-修改, attributes={url=manbox/modifyMainBox}, checked=true}, {id=10, text=维护人员与配电箱关联管理-查询所有, attributes={url=manbox/getCurrentPageOfMainbox}, checked=true}, {id=11, text=配电箱管理-查询所有, attributes={url=box/getBoxes}, checked=true}, {id=22, text=奖励管理主页, attributes={url=encouragement/toencouragement}, checked=true}, {id=23, text=奖励管理-添加, attributes={url=encouragement/addEncouragement}, checked=true}, {id=24, text=奖励管理-删除, attributes={url=encouragement/deleteEncouragement}, checked=true}, {id=25, text=奖励管理-修改, attributes={url=encouragement/modifyEncouragement}, checked=true}, {id=26, text=奖励管理-查询所有, attributes={url=encouragement/getCurrentPageOfEncouragement}, checked=true}, {id=27, text=维护工单管理主页, attributes={url=dispatch/todispatch}, checked=true}, {id=28, text=维护工单管理-添加, attributes={url=dispatch/addDispatch}, checked=true}, {id=29, text=维护工单管理-删除, attributes={url=dispatch/deleteDispatch}, checked=true}, {id=30, text=维护工单管理-修改, attributes={url=dispatch/modifyDispatch}, checked=true}, {id=31, text=维护工单管理-按id修改, attributes={url=dispatch/modifyByIdDispatch}, checked=true}, {id=32, text=维护工单管理-查询所有, attributes={url=dispatch/getCurrentPageOfDispatch}, checked=true}, {id=33, text=维护工单管理-按id查询, attributes={url=dispatch/getByIdDispatch}, checked=true}, {id=34, text=工程验收管理主页, attributes={url=projectm/toprojectm}, checked=true}, {id=35, text=工程验收管理-添加, attributes={url=projectm/addProjectm}, checked=true}, {id=36, text=工程验收管理-删除, attributes={url=projectm/deleteProjectm}, checked=true}, {id=37, text=工程验收管理-修改, attributes={url=projectm/modifyPMAction}, checked=true}, {id=38, text=工程验收管理-查询所有, attributes={url=projectm/getCurrentPageOfProjectm}, checked=true}, {id=58, text=局所管理主页, attributes={url=bureau/tobureauindex}, checked=true}, {id=59, text=局所管理-添加, attributes={url=bureau/saveBureau}, checked=true}, {id=60, text=局所管理-删除, attributes={url=bureau/deleteBureau}, checked=true}, {id=61, text=局所管理-修改, attributes={url=bureau/updateBureau}, checked=true}, {id=62, text=局所管理-查询所有, attributes={url=bureau/getAllBur}, checked=true}, {id=63, text=局所管理-查询id、nane, attributes={url=bureau/getBureauIdAndName}, checked=true}, {id=64, text=局所管理-查询上级局所, attributes={url=bureau/getUpperBureaus}, checked=true}, {id=65, text=配电箱管理主页, attributes={url=disbox/todisboxindex}, checked=true}, {id=66, text=配电箱管理-添加, attributes={url=disbox/saveDistributionBox}, checked=true}, {id=67, text=配电箱管理-删除, attributes={url=disbox/deleteDistributionBox}, checked=true}, {id=68, text=配电箱管理-修改, attributes={url=disbox/updateDistributionBox}, checked=true}, {id=69, text=配电箱管理-查询所有, attributes={url=disbox/getAllDisbox}, checked=true}, {id=70, text=路灯管理主页, attributes={url=lamp/tolampindex}, checked=true}, {id=71, text=路灯管理-添加, attributes={url=lamp/saveLamp}, checked=true}, {id=72, text=路灯管理-删除, attributes={url=lamp/deleteLamp}, checked=true}, {id=73, text=路灯管理-修改, attributes={url=lamp/updateLamp}, checked=true}, {id=74, text=路灯管理-查询所有, attributes={url=lamp/getAllLamps}, checked=true}]"); |
|||
将二进制数据流转换成图片保存 | 二进制数据,换成图片 | 将二进制数据流转换成图片保存 | |
package com.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import sun.misc.BASE64Decoder;//eclipse中自带的包 public class CodeTest { public static void main(String[] args) throws IOException { String str = "R0lGODlhgwBVAPcAAAAAAAwLBxkGBQ4ODhAQEBsSChUVFS4TDB8eGQkA9koPCDAAzy4mFVgAp2UYC0IqEUYuBVwiDEAsI1QnFX8AgDU1NUozFlgxD6cBWVY5FnIwEmQ4Gc0AMlhDHPEADlVJMEpKSm1IHOUBWpY3FZMyVY9IGXRWIEFmWGNYUmpdPXJgHQB8HK9EGGBgX4lXIACoAHhkMyt4m4VkJtstbv8A/65UHZBlG3plXotkNW5tZ5hmJMxQGJtlNIlzKHJyce1AQCGqOIl1PLtiIRClmXd4d65qL5x1J9hYIAKg6IR2bHx8e+FcGtpiG8drKZ1/MXCNbYGBgY2AbdxnIbd5KlKUr9hpMoaFhN1ZiPxUVOVqH9VxKpKFd5KFel2wKcx4KqCKSYqKivBrIJWJfoyMjK2NNJmNf992PJCPkJqPg+l4J5OSifJ1IuZ4NvF1MJSUlJ6ShtJ3k7CYRtKOLOeEOqOYioCsf5qameqHJvSEH6aajli23cqZRLaeZfaKJbanQ6GhoPOMM+mOReiXK9GrEcmdeM6oM6enpq+ml7+fovmZKLeub+mkKvqaM5qztq2treylNLetoc+xT7iwpOqVrbKysPumKrqyorW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ihoqpndCqSeZE8H/GiWdJZFK5K2ZmnrqmuSNuqqaYJra4bCXDOthhMUeqyyGyRqbIaW5YrrqtP7teihGalYqraVEvsooSbJKpGdJ41pLbpuANpruudeay1i5NQb27YC0Duquvfh6Cu688bZ7r17wZuTtuvR+6u+/k6Krb8H7CopRijCmB3GKASNMUXU9urmixm5mPPDCizo8kmobX0YyXxVbLK5JPQLX8ctXfawuyAKXlHHJ0rnc4LI8O9vzzz4Hrex2PqKZscfyEhxyvRctuaLEUIeVssqZvZs0zdA5ejDVOk0N2tUzh81uvlzv5PXFYDeM9WoKl901SSk67XTEUqfN8N39PoymgLyh5Hl22VMWTbd0G8usttgk0de3ioNX63ZO5C3OuN92L4143npPfvPGjj8uE8YWcv50y7oqzenlNW/tuch/SfVUVK4XBXvsQ81O+1Cr515f1BNNvCmQVR7Ge+aDz+Z7pMGTzHHyHCMvm4/BHb33Xpu/WPL0TT/dvPaWafx775NHzKP3yLcM9d58mTzyy42Lj1zOyxMvuvvPX1/++PQbB/PIhGM/vstwCx/4AAgb/B2pY/3L3vxIJzrlOc9/0JueA4l3M4kZzX+wOV/9nmc+mw2PfnTj4MM+aJ/j6e6EKEyhClfIQq4FBAA7"; BASE64Decoder decoder=new BASE64Decoder(); byte[] bytes=decoder.decodeBuffer(str); File file=new File("D:/a.jpeg");//可以是任何图片格式.jpg,.png等 FileOutputStream fos=new FileOutputStream(file); fos.write(bytes); fos.flush(); fos.close(); } } |