Android开发

json 转换 用法集合

时间:2016-4-16 13:50:29  作者:WPF之家  来源:http://www.wpf123.com/  查看:693  评论:0
内容摘要:使用Google的Gson实现对象和json字符串之间的转换:import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;import j...

使用Google的Gson实现对象和json字符串之间的转换:

import java.util.ArrayList;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.domain.Student;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class TestJson {
public static void main(String[] args) {
Gson gson = new Gson();
Student student1 = new Student();
student1.setId(1);
student1.setBirthDay(new Date());
student1.setName("zhang");
///////////简单对象转化为json//////////////////
String string1 = gson.toJson(student1);
System.out.println("简单Bean转化为Json=========="+string1);
//json转化为简单Bean
Student student = gson.fromJson(string1, Student.class);
System.out.println("Json转化为简单Bean========"+student);
//运行结果:
// 简单Bean转化为Json=========={"id":1,"name":"zhang","birthDay":"Apr 16, 2016 2:19:57 PM"}
// Json转化为简单Bean========Student [id=1, name=zhang, birthDay=Sat Apr 16 14:19:57 CST 2016]

Student student2 = new Student();
   student2.setId(2);
   student2.setName("李");
   student2.setBirthDay(new Date());

   Student student3 = new Student();
   student3.setId(3);
   student3.setName("波");
   student3.setBirthDay(new Date());

   List<Student> list = new ArrayList<Student>();
   list.add(student1);
   list.add(student2);
   list.add(student3);
   
   System.out.println("--------------带泛型的List之间的转化------------------------------");
   //带泛型的list转化为json
   String string2 = gson.toJson(list);
   System.out.println("带泛型的list转化为json===="+string2);
   
   //json转化为带泛型的list
   List<Student> students = gson.fromJson(string2, new TypeToken<List<Student>>(){}.getType());  
   for (Student student4 : students) {
System.out.println(student4);
}
   
   //运行结果:
//    --------------带泛型的List之间的转化------------------------------
//         带泛型的list转化为json====[{"id":1,"name":"zhang","birthDay":"Apr 16, 2016 2:27:17 PM"},
//     {"id":2,"name":"李","birthDay":"Apr 16, 2016 2:27:17 PM"},
//     {"id":3,"name":"波","birthDay":"Apr 16, 2016 2:27:17 PM"}]
//    Student [id=1, name=zhang, birthDay=Sat Apr 16 14:27:17 CST 2016]
//    Student [id=2, name=李, birthDay=Sat Apr 16 14:27:17 CST 2016]
//    Student [id=3, name=波, birthDay=Sat Apr 16 14:27:17 CST 2016]
Map<Integer, Student> map = new HashMap<Integer, Student>();
map.put(student1.getId(), student1);
map.put(student1.getId(), student2);
map.put(student3.getId(), student3);
//javabean(map)转换为json
String stringjsonString = gson.toJson(map);
System.out.println("map转换为json:========"+stringjsonString);
//运行结果为:
//
// map转换为json:========{"1":{"id":2,"name":"李","birthDay":"Apr 16, 2016 3:00:57 PM"},
//                       "3":{"id":3,"name":"波","birthDay":"Apr 16, 2016 3:00:57 PM"}}
//json转换为map对象
System.out.println("json转换为map对象测试。。。。。。");
Map<Integer, Student> map2 = gson.fromJson(stringjsonString, new TypeToken<Map<Integer,Student>>(){}.getType());
for (Entry<Integer, Student> entry : map2.entrySet()) {
Integer keyInteger = entry.getKey();
Student student4 = entry.getValue();
System.out.println("key:"+keyInteger+","+student4.getId()+","+student4.getName());
}
   
   
}
}


标签:json 转换 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号