一:前言

  来公司一个星期,把最近做的东西梳理下,并把觉得有必要的知识点记载下,现在传数据很多都是用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解析方式

 package org.wh.JsonDemo;

 import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; public class GJsonTest { public static void main(String[] args) {
Student s1=new Student(1,"mahone","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s2=new Student(2,"mouse","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s3=new Student(3,"moon","女",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s4=new Student(4,"mahone1","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s5=new Student(5,"mahone2","男",23,"湖北随州",new Date(),new java.sql.Date(0)); List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5); Map<String,String> map=new HashMap<String,String>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.put("d", "dd");
map.put("e", "ee"); //--------------------google 的json-----------------------------------------------------------------
System.out.println(list);
Gson g=new Gson();
String exInfo1=g.toJson(s1);
System.out.println("Google转化为的单个对象:-->"+exInfo1); String exList=g.toJson(list);
System.out.println("Google的List转化的json:-->"+exList); String exMap=g.toJson(map);
System.out.println("Google的map的转换---->"+exMap); //把json转换为List
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);
System.out.println("-->"+st);
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());
List<Student> list2=g.fromJson(exList,new TypeToken<List<Student>>(){}.getType());
for(Student ss:list1){
System.out.println("id:"+ss.getId());
} for(Student ss:list2){
System.out.println("id:"+ss.getId());
} //把json转化为Map
Map<String,String> m=g.fromJson(exMap,new TypeToken<Map<String,String>>(){}.getType());
for(String ms:map.keySet()){
//key---->value
System.out.println(ms+"--->"+map.get(ms));
}
//http://blog.csdn.net/lk_blog/article/details/7685210链接上有更加详细的解析,我这里只是日常用的
} }

  (二):阿里巴巴的解析方式

 package org.wh.JsonDemo;

 import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; public class AliJsonDemo { public static void main(String[] args) {
Student s1=new Student(1,"mahone","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s2=new Student(2,"mouse","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s3=new Student(3,"moon","女",23,"湖北随州安居",new Date(),new java.sql.Date(0));
Student s4=new Student(4,"mahone1","男",23,"湖北随州",new Date(),new java.sql.Date(0));
Student s5=new Student(5,"mahone2","男",23,"湖北随州",new Date(),new java.sql.Date(0)); Student1 s11=new Student1(1,"mahone","男",23,"湖北随州",new Date());
Student1 s22=new Student1(2,"mouse","男",23,"湖北随州",new Date());
Student1 s33=new Student1(3,"moon","女",23,"湖北随州安居",new Date());
Student1 s44=new Student1(3,"moon","女",23,"湖北随州安居",new Date());
Student1 s55=new Student1(3,"moon","女",23,"湖北随州安居",new Date()); List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5); Map<String,String> map=new HashMap<String,String>();
Map<String,Student1> map1=new HashMap<String,Student1>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.put("d", "dd");
map.put("e", "ee"); map1.put("a",s11);
map1.put("b",s22);
map1.put("c",s33);
map1.put("d",s44);
map1.put("e",s55); //--------------------阿里巴巴的json String aliInfo=JSON.toJSONString(s1);
System.out.println("阿里巴巴转换单个对象的json格式:"+aliInfo); String aliList=JSON.toJSONString(list);
System.out.println("阿里巴巴转换List对象的json的格式"+aliList); String aliMap=JSON.toJSONString(map);
System.out.println("阿里巴巴转换map对象的json的个格式"+aliMap); //单个对象的反序列化
Student ss=JSON.parseObject(aliInfo,Student.class);
System.out.println(ss.getId()+"-->"+ss.getName()); //将Json转换为List
List<Student> ls=JSON.parseArray(aliList,Student.class);
System.out.println(ls);
for(Student su:ls){
System.out.println("id的值"+su.getId());
} //将map转化为
String aliMap1=JSON.toJSONString(map1);
JSONObject jo=JSON.parseObject(aliMap1);
System.out.println("获取"+jo.getString("a"));
System.out.println("获取"+jo.getObject("a", Student.class).getId()); } }

  最后一种,我在写的时候老是报错,现在不知道是由于里面有日期还是由于加了日期使得整个对象变得复杂了的鱼那样。还是把代码加上,没有完整

 package org.wh.JsonDemo;

 import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class StrutsJsonDemo {
public static void main(String args[]){
Student1 s1=new Student1(1,"mahone","男",23,"湖北随州",new Date());
Student1 s2=new Student1(2,"mouse","男",23,"湖北随州",new Date());
Student1 s3=new Student1(3,"moon","女",23,"湖北随州安居",new Date());
Student1 s4=new Student1(4,"mahone1","男",23,"湖北随州",new Date());
Student1 s5=new Student1(5,"mahone2","男",23,"湖北随州",new Date()); List<Student1> list=new ArrayList<Student1>();
list.add(s1);
list.add(s2);
// list.add(s3);
// list.add(s4);
// list.add(s5); Map<String,String> map=new HashMap<String,String>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.put("d", "dd");
map.put("e", "ee"); //将list转换为json数据
JSONArray listTojson=JSONArray.fromObject(list);
System.out.println("将list转换为json:"+listTojson); //将json转换为List
JSONObject jsonObject=JSONObject.fromObject(listTojson.toString());
JSONArray jsonArray = new JSONArray();
Object o=JSONArray.toList(jsonArray,Student1.class);
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。电影最后的那句”不悔梦归处,只恨太匆匆“啊!错过就是一辈子啊。

 

最新文章

  1. MongoDB的简单操作(asp.net)
  2. ubuntu 14.04 ns2.35 ***buffer overflow detected **: ns terminated解决办法
  3. 在MFC中使用GDI+的一般方法,以VC6.0编译器为例
  4. IntelliJ IDEA 15.0.2远程debug tomcat
  5. SharePoint Online 创建门户网站系列之定制栏目
  6. CSS3-transform变形功能
  7. 【实例】html5-canvas中实现背景图片的移动
  8. MySQL查询语句
  9. Maven学习(二) -- 坐标和依赖
  10. 对比git rm和rm的使用区别
  11. C# this.invoke()作用 多线程操作UI
  12. ArcGIS_系列视频教程::精品大放送
  13. Oracle层次查询和with函数的使用
  14. Java 基础 程序流程控制 (上)
  15. 【读书笔记】A Swift Tour
  16. java底层学习
  17. TypeScript专题-Static和使用技巧
  18. 不能最为IF判断条件的属性
  19. poj-2337(欧拉回路输出)
  20. Laravel 项目中使用 Bootstrap 框架

热门文章

  1. 解析HTML利器AngleSharp介绍
  2. Leetcode 简略题解 - 共567题
  3. MySQL源码中的String
  4. 『AngularJS』ngValue
  5. ASP NET Core --- HTTP 翻页、过滤、排序
  6. svm+voting
  7. FFT的物理意义(转载)
  8. MySQL 5.6查看数据库的大小
  9. NO1——线段树
  10. Daily Scrum02 11.30