首页 > 代码库 > 3中转换JSON数据的方式
3中转换JSON数据的方式
一:前言
来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用JSON来传数据,所以我就找了集中传json的方式,其实是有五种的,但是有一个我没有用过,太陌生了,上次也在网上看了看,估计可以照着用,但是要我讲的话我还是觉得挺有难度的。还有个也没有用过。我都会在下面提一下
二:内容
我现在可以用的JSON有三种:
(一):Google的JSON的jar包处理
(二):阿里巴巴解析JSON的jar包
(三):Struts2解析的jar包
(四):jsonrpc,这个我看看了,主要是在网页里面进行,它可以你透明地在JavaScript中调用Java代码:具体可以看这篇文章
http://blog.csdn.net/yaerfeng/article/details/26079889
(五):json-simple.jar的jar包,其实我不会用,只是知道有这种jar包,没有用过,刚刚看到的。
二:内容
(一):google的Json解析方式
1 package org.wh.JsonDemo; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import com.google.gson.Gson;10 import com.google.gson.reflect.TypeToken;11 12 public class GJsonTest {13 14 public static void main(String[] args) {15 Student s1=new Student(1,"mahone","男",23,"湖北随州",new Date(),new java.sql.Date(0));16 Student s2=new Student(2,"mouse","男",23,"湖北随州",new Date(),new java.sql.Date(0));17 Student s3=new Student(3,"moon","女",23,"湖北随州",new Date(),new java.sql.Date(0));18 Student s4=new Student(4,"mahone1","男",23,"湖北随州",new Date(),new java.sql.Date(0));19 Student s5=new Student(5,"mahone2","男",23,"湖北随州",new Date(),new java.sql.Date(0));20 21 List<Student> list=new ArrayList<Student>();22 list.add(s1);23 list.add(s2);24 list.add(s3);25 list.add(s4);26 list.add(s5);27 28 Map<String,String> map=new HashMap<String,String>();29 map.put("a", "aa");30 map.put("b", "bb");31 map.put("c", "cc");32 map.put("d", "dd");33 map.put("e", "ee");34 35 36 //--------------------google 的json-----------------------------------------------------------------37 System.out.println(list);38 Gson g=new Gson();39 String exInfo1=g.toJson(s1);40 System.out.println("Google转化为的单个对象:-->"+exInfo1);41 42 String exList=g.toJson(list);43 System.out.println("Google的List转化的json:-->"+exList);44 45 String exMap=g.toJson(map);46 System.out.println("Google的map的转换---->"+exMap);47 48 49 //把json转换为List50 Student st=g.fromJson("{\"id\":1,\"name\":\"mahone\",\"sex\":\"男\",\"age\":23,\"birthday\":\"Dec 18, 2014 3:59:45 PM\",\"address\":\"湖北随州\",\"senior\":\"一月 1, 1970\"}", Student.class);51 System.out.println("-->"+st);52 List<Student> list1=g.fromJson("[{\"id\":1,\"name\":\"mahone\",\"sex\":\"男\",\"age\":23,\"birthday\":\"Dec 18, 2014 3:59:45 PM\",\"address\":\"湖北随州\",\"senior\":\"一月 1, 1970\"},{\"id\":2,\"name\":\"mahone\",\"sex\":\"男\",\"age\":23,\"birthday\":\"Dec 18, 2014 3:59:45 PM\",\"address\":\"湖北随州\",\"senior\":\"一月 1, 1970\"}]",new TypeToken<List<Student>>(){}.getType());53 List<Student> list2=g.fromJson(exList,new TypeToken<List<Student>>(){}.getType());54 for(Student ss:list1){55 System.out.println("id:"+ss.getId());56 }57 58 for(Student ss:list2){59 System.out.println("id:"+ss.getId());60 }61 62 //把json转化为Map63 Map<String,String> m=g.fromJson(exMap,new TypeToken<Map<String,String>>(){}.getType());64 for(String ms:map.keySet()){65 //key---->value66 System.out.println(ms+"--->"+map.get(ms));67 }68 //http://blog.csdn.net/lk_blog/article/details/7685210链接上有更加详细的解析,我这里只是日常用的69 }70 71 }
(二):阿里巴巴的解析方式
1 package org.wh.JsonDemo; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import com.alibaba.fastjson.JSON;10 import com.alibaba.fastjson.JSONObject;11 12 public class AliJsonDemo {13 14 public static void main(String[] args) {15 Student s1=new Student(1,"mahone","男",23,"湖北随州",new Date(),new java.sql.Date(0));16 Student s2=new Student(2,"mouse","男",23,"湖北随州",new Date(),new java.sql.Date(0));17 Student s3=new Student(3,"moon","女",23,"湖北随州安居",new Date(),new java.sql.Date(0));18 Student s4=new Student(4,"mahone1","男",23,"湖北随州",new Date(),new java.sql.Date(0));19 Student s5=new Student(5,"mahone2","男",23,"湖北随州",new Date(),new java.sql.Date(0));20 21 22 Student1 s11=new Student1(1,"mahone","男",23,"湖北随州",new Date());23 Student1 s22=new Student1(2,"mouse","男",23,"湖北随州",new Date());24 Student1 s33=new Student1(3,"moon","女",23,"湖北随州安居",new Date());25 Student1 s44=new Student1(3,"moon","女",23,"湖北随州安居",new Date());26 Student1 s55=new Student1(3,"moon","女",23,"湖北随州安居",new Date());27 28 List<Student> list=new ArrayList<Student>();29 list.add(s1);30 list.add(s2);31 list.add(s3);32 list.add(s4);33 list.add(s5);34 35 Map<String,String> map=new HashMap<String,String>();36 Map<String,Student1> map1=new HashMap<String,Student1>();37 map.put("a", "aa");38 map.put("b", "bb");39 map.put("c", "cc");40 map.put("d", "dd");41 map.put("e", "ee");42 43 map1.put("a",s11);44 map1.put("b",s22);45 map1.put("c",s33);46 map1.put("d",s44);47 map1.put("e",s55);48 49 50 //--------------------阿里巴巴的json51 52 String aliInfo=JSON.toJSONString(s1);53 System.out.println("阿里巴巴转换单个对象的json格式:"+aliInfo);54 55 String aliList=JSON.toJSONString(list);56 System.out.println("阿里巴巴转换List对象的json的格式"+aliList);57 58 String aliMap=JSON.toJSONString(map);59 System.out.println("阿里巴巴转换map对象的json的个格式"+aliMap);60 61 62 //单个对象的反序列化63 Student ss=JSON.parseObject(aliInfo,Student.class);64 System.out.println(ss.getId()+"-->"+ss.getName());65 66 //将Json转换为List67 List<Student> ls=JSON.parseArray(aliList,Student.class);68 System.out.println(ls);69 for(Student su:ls){70 System.out.println("id的值"+su.getId());71 }72 73 //将map转化为74 String aliMap1=JSON.toJSONString(map1);75 JSONObject jo=JSON.parseObject(aliMap1);76 System.out.println("获取"+jo.getString("a"));77 System.out.println("获取"+jo.getObject("a", Student.class).getId());78 79 }80 81 }
最后一种,我在写的时候老是报错,现在不知道是由于里面有日期还是由于加了日期使得整个对象变得复杂了的鱼那样。还是把代码加上,没有完整
1 package org.wh.JsonDemo; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import net.sf.json.JSONArray;10 import net.sf.json.JSONObject;11 12 public class StrutsJsonDemo {13 public static void main(String args[]){14 Student1 s1=new Student1(1,"mahone","男",23,"湖北随州",new Date());15 Student1 s2=new Student1(2,"mouse","男",23,"湖北随州",new Date());16 Student1 s3=new Student1(3,"moon","女",23,"湖北随州安居",new Date());17 Student1 s4=new Student1(4,"mahone1","男",23,"湖北随州",new Date());18 Student1 s5=new Student1(5,"mahone2","男",23,"湖北随州",new Date());19 20 21 List<Student1> list=new ArrayList<Student1>();22 list.add(s1);23 list.add(s2);24 // list.add(s3);25 // list.add(s4);26 // list.add(s5);27 28 Map<String,String> map=new HashMap<String,String>();29 map.put("a", "aa");30 map.put("b", "bb");31 map.put("c", "cc");32 map.put("d", "dd");33 map.put("e", "ee");34 35 //将list转换为json数据36 JSONArray listTojson=JSONArray.fromObject(list);37 System.out.println("将list转换为json:"+listTojson);38 39 //将json转换为List40 JSONObject jsonObject=JSONObject.fromObject(listTojson.toString());41 JSONArray jsonArray = new JSONArray();42 Object o=JSONArray.toList(jsonArray,Student1.class);43 System.out.println(o);
出现的bug如下:
将list转换为json:[{"address":"湖北随州","age":23,"birthday":{"date":18,"day":4,"hours":18,"minutes":24,"month":11,"seconds":49,"time":1418898289977,"timezoneOffset":-480,"year":114},"id":1,"name":"mahone","senior":null,"sex":"男"},{"address":"湖北随州","age":23,"birthday":{"date":18,"day":4,"hours":18,"minutes":24,"month":11,"seconds":49,"time":1418898289977,"timezoneOffset":-480,"year":114},"id":2,"name":"mouse","senior":null,"sex":"男"}]Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with ‘{‘ at character 1 of [{"address":"湖北随州","age":23,"birthday":{"date":18,"day":4,"hours":18,"minutes":24,"month":11,"seconds":49,"time":1418898289977,"timezoneOffset":-480,"year":114},"id":1,"name":"mahone","senior":null,"sex":"男"},{"address":"湖北随州","age":23,"birthday":{"date":18,"day":4,"hours":18,"minutes":24,"month":11,"seconds":49,"time":1418898289977,"timezoneOffset":-480,"year":114},"id":2,"name":"mouse","senior":null,"sex":"男"}] at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:505) at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1144) at net.sf.json.JSONObject._fromString(JSONObject.java:1373) at net.sf.json.JSONObject.fromObject(JSONObject.java:161) at net.sf.json.JSONObject.fromObject(JSONObject.java:130) at org.wh.JsonDemo.StrutsJsonDemo.main(StrutsJsonDemo.java:40)
这里的日期又成了对象,所以我觉得很可能是这个日期变得复杂了才使得其解析不了,下篇在仔细看看。
三:总结
做了决定,下了决心就得主动动手来做了。自己做了才知道是怎么回事,所以必须的做。前段时间看了《匆匆那年》,挺感慨的,我的青春一去不复回啊,还是想起了高总时刻,特别是高一啊。MouseMoon。电影最后的那句”不悔梦归处,只恨太匆匆“啊!错过就是一辈子啊。
3中转换JSON数据的方式