欢迎光临Software MyZone,有问题可留言或到站点论坛发帖,争取第一时间帮忙解决 || 站点论坛:火龙论坛 || 淘宝小店:应小心的易淘屋 【欢迎大家提建设性意见】

Json转换利器Gson之实例四-Map处理(下)

欢迎热爱编程的朋友们参与到cocos2d-x编程中,为了给大家提供良好的交流环境,网站以开启QQ群
Software MyZone:66202765(群号,欢迎加入,若满,请加1群)
Software MyZone 1群(2dx):286504621
Software MyZone 2群(山东):204954191
Software MyZone【Java群】:162865493
【加群请写:Software MyZone或者是firedragonpzy】
淘宝店:【应小心的易淘屋】初次开店,大家多多支持……
群论坛:【火龙论坛】正试运营阶段,欢迎大家多提些建设性意见……
群问答:【火龙问答】正试运营阶段,欢迎大家多提些建设性意见……
摘自:http://blog.csdn.net/lk_blog/article/details/7685224

我的digitalocean推广链接:https://www.digitalocean.com/?refcode=65dfdb158f1a,Everyone you refer gets $10 in credit,从此链接注册,你将获得10美元。

Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(上一篇博客),还可以是带有泛型的List(本文).本例中您要重点看如何将Json转回为带泛型的对象List,并且List中的泛型对象有多种实体.

实体类:

import java.util.Date;

public class Student {
	private int id;
	private String name;
	private Date birthDay;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getBirthDay() {
		return birthDay;
	}

	public void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}

	@Override
	public String toString() {
		return "Student [birthDay=" + birthDay + ", id=" + id + ", name="
				+ name + "]";
	}

}
public class Teacher {
	private int id;

	private String name;

	private String title;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public String toString() {
		return "Teacher [id=" + id + ", name=" + name + ", title=" + title
				+ "]";
	}

}

测试类:

package com.tgb.lk.demo.gson.test4;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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

public class GsonTest4 {
	public static void main(String[] args) {
		Student student1 = new Student();
		student1.setId(1);
		student1.setName("李坤");
		student1.setBirthDay(new Date());

		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> stulist = new ArrayList</student><student>();
		stulist.add(student1);
		stulist.add(student2);
		stulist.add(student3);

		Teacher teacher1 = new Teacher();
		teacher1.setId(1);
		teacher1.setName("米老师");
		teacher1.setTitle("教授");

		Teacher teacher2 = new Teacher();
		teacher2.setId(2);
		teacher2.setName("丁老师");
		teacher2.setTitle("讲师");
		List<teacher> teacherList = new ArrayList</teacher><teacher>();
		teacherList.add(teacher1);
		teacherList.add(teacher2);

		Map<string , Object> map = new LinkedHashMap</string><string , Object>();
		map.put("students", stulist);
		map.put("teachers", teacherList);

		Gson gson = new Gson();
		String s = gson.toJson(map);
		System.out.println(s);

		System.out.println("----------------------------------");

		Map</string><string , Object> retMap = gson.fromJson(s,
				new TypeToken<map <String, List<Object>>>() {
				}.getType());

		for (String key : retMap.keySet()) {
			System.out.println("key:" + key + " values:" + retMap.get(key));
			if (key.equals("students")) {
				List<student> stuList = (List</student><student>) retMap.get(key);
				System.out.println(stuList);
			} else if (key.equals("teachers")) {
				List<teacher> tchrList = (List</teacher><teacher>) retMap.get(key);
				System.out.println(tchrList);
			}
		}

	}
}

输出结果:

{“students”:[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":2,"name":"曹贵生","birthDay":"Jun 22, 2012 9:48:19 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:48:19 PM"}],”teachers”:[{"id":1,"name":"米老师","title":"教授"},{"id":2,"name":"丁老师","title":"讲师"}]}
———————————-
key:students values:[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹贵生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
[{id=1.0, name=李坤, birthDay=Jun 22, 2012 9:48:19 PM}, {id=2.0, name=曹贵生, birthDay=Jun 22, 2012 9:48:19 PM}, {id=3.0, name=柳波, birthDay=Jun 22, 2012 9:48:19 PM}]
key:teachers values:[{id=1.0, name=米老师, title=教授}, {id=2.0, name=丁老师, title=讲师}]
[{id=1.0, name=米老师, title=教授}, {id=2.0, name=丁老师, title=讲师}]

  1. Json转换利器Gson之实例一-简单对象转化和带泛型的List转化
  2. Json转换利器Gson之实例二-Gson注解和GsonBuilder
  3. Json转换利器Gson之实例三-Map处理(上)
  4. Json转换利器Gson之实例四-Map处理(下)
  5. Json转换利器Gson之实例五-实际开发中的特殊需求处理
  6. Json转换利器Gson之实例六-注册TypeAdapter及处理Enum类型

Tags: ,

发表评论