String str = new String( Base64.decodeBase64("eyJtZW5waWFvIjp7ImNvdW50IjoyLCJyb3dzIjpbeyJuYW1lIjoi5Lit5bGx6Zm1MUFBQUHpl6jnpagiLCJzaWdodENvZGUiOjEwMDA2LCJzaWdodE5hbWUiOiLkuK3lsbHpmbUiLCJwcmljZU1hcmtlZCI6MTAwLCJkcmF3VHlwZSI6MiwiaW52b2ljZVR5cGUiOjEsImxpbWl0TnVtIjowLCJzdGFydE51bSI6MSwiY3VzdG9tZXJJbmZvTGltaXQiOjIsInJlbWFyayI6Iua1i+ivleWkh+azqCIsImRyYXdBZGRyZXNzIjoi5Ye656Wo5Zyw5Z2AIiwic2FsZVRpbWUiOiIwODowMH4xNTowMCIsIm93bmVySWQiOjI0OTYsIm1hbmFnZXJJZCI6MjQ5Niwib3duZXJOYW1lIjoi6LSf6LSj5Lq6IiwibWFuYWdlck5hbWUiOiLnu4/nkIYiLCJib29rTm90aWNlIjoi4piFIOmAlOeJm+S8muWRmOaXpeKAlOKAlOeJueaDoOaZr+WMuuS7u+S9oOeOqe+8mmh0dHA6Ly9iYnMudHVuaXUuY29tL3RocmVhZC0xODI4MzUtMC5odG1s4piF6JCl5Lia5pe26Ze077ya6JCl5Lia5pe26Ze077yaMDg6MDDigJQxNzowMOOAguWUruelqOaXtumXtO+8mjA4OjAw4oCUMTc6MDDjgILimIXlj5bnpajmlrnlvI/vvJrpooTorqLmiJDlip/lkI7vvIzlh63nn63kv6HlkozmnInmlYjor4Hku7bliLDljZfkuqzpmIXmsZ/mpbzmma/ljLrljZfpl6jllK7npajlpITkubDnpajjgIIiLCJ3YXJtTm90aWNlIjoi5rip6aao5o+Q6YaSIiwiZmxhZyI6MX1dfX0=")); Map<String, Object> data = new HashMap<String, Object>(); data = JSONObject.fromObject(str); Map<String, Object> outsideData = new HashMap<String, Object>(); System.out.println(data); outsideData.put("wj", str); System.out.println(JSONObject.fromObject(outsideData)); System.out.println(JSONObject.fromObject(str)); System.out.println("####################################"); String str1 = new String( Base64.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wiLFwibmFtZVwiOlwi5a6+6aaG5oi/6Ze06K6+5pa977yaXCIsXCJjb3VudFwiOjAsXCJyb3dzXCI6W119LFwiNVwiOntcImlkXCI6XCI1XCIsXCJuYW1lXCI6XCLlrr7ppobkvJrorq7orr7mlr3vvJpcIixcImNvdW50XCI6MCxcInJvd3NcIjpbXX0sXCI2XCI6e1wiaWRcIjpcIjZcIixcIm5hbWVcIjpcIuWuvummhumZhOWKoOiuvuaWve+8mlwiLFwiY291bnRcIjowLFwicm93c1wiOltdfX0iLCJ0ZWwiOiIxNTA1MDUzMTgzMyIsIndlYnNpdGUiOiIiLCJkZXRhaWwiOiIiLCJob3RlbFR5cGUiOjAsImhvdGVsQ29kZSI6IlJIVDM5Mjk5IiwiYm9va05vdGljZSI6IjIxMjMzMTIyMzEzMjEiLCJidXNpbmVzc0Rpc3RyaWN0cyI6eyJjb3VudCI6MCwicm93cyI6W119LCJzcG90cyI6eyJjb3VudCI6MCwicm93cyI6W119fX19")); Map<String, Object> data1 = new HashMap<String, Object>(); data1 = JSONObject.fromObject(str1); System.out.println(str1); System.out.println(data1);
测试程序需要的jar包括:
json-lib-2.4-jdk15.jar
commons-codec-1.6.jar
其他jar包见截图
上面测试代码放在main方法里面,的输入结果如下所示:
{"menpiao":{"count":2,"rows":[{"name":"中山陵1AAAA门票","sightCode":10006,"sightName":"中山陵","priceMarked":100,"drawType":2,"invoiceType":1,"limitNum":0,"startNum":1,"customerInfoLimit":2,"remark":"测试备注","drawAddress":"出票地址","saleTime":"08:00~15:00","ownerId":2496,"managerId":2496,"ownerName":"负责人","managerName":"经理","bookNotice":"★ 途牛会员日——特惠景区任你玩:http://bbs.tuniu.com/thread-182835-0.html★营业时间:营业时间:08:00—17:00。售票时间:08:00—17:00。★取票方式:预订成功后,凭短信和有效证件到南京阅江楼景区南门售票处买票。","warmNotice":"温馨提醒","flag":1}]}}
{"wj":"{\"menpiao\":{\"count\":2,\"rows\":[{\"name\":\"中山陵1AAAA门票\",\"sightCode\":10006,\"sightName\":\"中山陵\",\"priceMarked\":100,\"drawType\":2,\"invoiceType\":1,\"limitNum\":0,\"startNum\":1,\"customerInfoLimit\":2,\"remark\":\"测试备注\",\"drawAddress\":\"出票地址\",\"saleTime\":\"08:00~15:00\",\"ownerId\":2496,\"managerId\":2496,\"ownerName\":\"负责人\",\"managerName\":\"经理\",\"bookNotice\":\"★ 途牛会员日——特惠景区任你玩:http://bbs.tuniu.com/thread-182835-0.html★营业时间:营业时间:08:00—17:00。售票时间:08:00—17:00。★取票方式:预订成功后,凭短信和有效证件到南京阅江楼景区南门售票处买票。\",\"warmNotice\":\"温馨提醒\",\"flag\":1}]}}"}
{"menpiao":{"count":2,"rows":[{"name":"中山陵1AAAA门票","sightCode":10006,"sightName":"中山陵","priceMarked":100,"drawType":2,"invoiceType":1,"limitNum":0,"startNum":1,"customerInfoLimit":2,"remark":"测试备注","drawAddress":"出票地址","saleTime":"08:00~15:00","ownerId":2496,"managerId":2496,"ownerName":"负责人","managerName":"经理","bookNotice":"★ 途牛会员日——特惠景区任你玩:http://bbs.tuniu.com/thread-182835-0.html★营业时间:营业时间:08:00—17:00。售票时间:08:00—17:00。★取票方式:预订成功后,凭短信和有效证件到南京阅江楼景区南门售票处买票。","warmNotice":"温馨提醒","flag":1}]}}
####################################
{"success":true,"msg":"","errorCode":170000,"data":{"hotelInfo":{"id":39299,"delFlag":0,"opTime":"2012-09-13","opUid":0,"opName":"","chineseName":"aaaaaa","englishName":"","pinyinFull":"aaaaaa","pinyinAbbr":"","star":2,"showStar":1,"countryCode":10000,"provinceCode":2600,"cityCode":2602,"districtCode":10175,"countryName":"中国","provinceName":"山西","cityName":"太原","districtName":"小店区","address":"sssassssa","debutYear":"2012","decorateDate":"1234-11","roomNumber":21,"remark":"","longitude":0.0,"latitude":0.0,"facilites":"{\"1\":{\"id\":\"1\",\"name\":\"宾馆服务项目:\",\"count\":0,\"rows\":[]},\"2\":{\"id\":\"2\",\"name\":\"宾馆餐饮设施:\",\"count\":0,\"rows\":[]},\"3\":{\"id\":\"3\",\"name\":\"宾馆娱乐与健身设施:\",\"count\":0,\"rows\":[]},\"4\":{\"id\":\"4\",\"name\":\"宾馆房间设施:\",\"count\":0,\"rows\":[]},\"5\":{\"id\":\"5\",\"name\":\"宾馆会议设施:\",\"count\":0,\"rows\":[]},\"6\":{\"id\":\"6\",\"name\":\"宾馆附加设施:\",\"count\":0,\"rows\":[]}}","tel":"15050531833","website":"","detail":"","hotelType":0,"hotelCode":"RHT39299","bookNotice":"2123312231321","businessDistricts":{"count":0,"rows":[]},"spots":{"count":0,"rows":[]}}}}
{"success":true,"msg":"","errorCode":170000,"data":{"hotelInfo":{"id":39299,"delFlag":0,"opTime":"2012-09-13","opUid":0,"opName":"","chineseName":"aaaaaa","englishName":"","pinyinFull":"aaaaaa","pinyinAbbr":"","star":2,"showStar":1,"countryCode":10000,"provinceCode":2600,"cityCode":2602,"districtCode":10175,"countryName":"中国","provinceName":"山西","cityName":"太原","districtName":"小店区","address":"sssassssa","debutYear":"2012","decorateDate":"1234-11","roomNumber":21,"remark":"","longitude":0,"latitude":0,"facilites":{"1":{"id":"1","name":"宾馆服务项目:","count":0,"rows":[]},"2":{"id":"2","name":"宾馆餐饮设施:","count":0,"rows":[]},"3":{"id":"3","name":"宾馆娱乐与健身设施:","count":0,"rows":[]},"4":{"id":"4","name":"宾馆房间设施:","count":0,"rows":[]},"5":{"id":"5","name":"宾馆会议设施:","count":0,"rows":[]},"6":{"id":"6","name":"宾馆附加设施:","count":0,"rows":[]}},"tel":"15050531833","website":"","detail":"","hotelType":0,"hotelCode":"RHT39299","bookNotice":"2123312231321","businessDistricts":{"count":0,"rows":[]},"spots":{"count":0,"rows":[]}}}}
上面蓝色字体是json字符串,红色字体是把json字符串里面的menpiao部分放在双引号里面展示到java代码中。这样就不用手工将"切换为\"。