Gson解析(3)——Map处理数据(上)
2015-10-05 17:56
645 查看
Map的存储结构式Key/Value形式,Key 和 Value可以是普通类型,也可以是自己写的JavaBean(本文),还可以是带有泛型的List(下一篇博客).本例中您要重点看如何将Json转回为普通JavaBean对象时TypeToken的定义.
实体类:
[java] view
plaincopy
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
测试类:
[java] view
plaincopy
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class GsonTest3 {
public static void main(String[] args) {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
.create();
Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 使用LinkedHashMap将结果按先进先出顺序排列
map1.put(new Point(5, 6), "a");
map1.put(new Point(8, 8), "b");
String s = gson.toJson(map1);
System.out.println(s);// 结果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
Map<Point, String> retMap = gson.fromJson(s,
new TypeToken<Map<Point, String>>() {
}.getType());
for (Point p : retMap.keySet()) {
System.out.println("key:" + p + " values:" + retMap.get(p));
}
System.out.println(retMap);
System.out.println("----------------------------------");
Map<String, Point> map2 = new LinkedHashMap<String, Point>();
map2.put("a", new Point(3, 4));
map2.put("b", new Point(5, 6));
String s2 = gson.toJson(map2);
System.out.println(s2);
Map<String, Point> retMap2 = gson.fromJson(s2,
new TypeToken<Map<String, Point>>() {
}.getType());
for (String key : retMap2.keySet()) {
System.out.println("key:" + key + " values:" + retMap2.get(key));
}
}
}
结果:
[plain] view
plaincopy
[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
key:Point [x=5, y=6] values:a
key:Point [x=8, y=8] values:b
{Point [x=5, y=6]=a, Point [x=8, y=8]=b}
----------------------------------
{"a":{"x":3,"y":4},"b":{"x":5,"y":6}}
key:a values:Point [x=3, y=4]
key:b values:Point [x=5, y=6]
文章转自:http://blog.csdn.net/lk_blog/article/details/7685210
感谢博主的无私分享!
实体类:
[java] view
plaincopy
public class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}
}
测试类:
[java] view
plaincopy
import java.util.LinkedHashMap;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class GsonTest3 {
public static void main(String[] args) {
Gson gson = new GsonBuilder().enableComplexMapKeySerialization()
.create();
Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 使用LinkedHashMap将结果按先进先出顺序排列
map1.put(new Point(5, 6), "a");
map1.put(new Point(8, 8), "b");
String s = gson.toJson(map1);
System.out.println(s);// 结果:[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
Map<Point, String> retMap = gson.fromJson(s,
new TypeToken<Map<Point, String>>() {
}.getType());
for (Point p : retMap.keySet()) {
System.out.println("key:" + p + " values:" + retMap.get(p));
}
System.out.println(retMap);
System.out.println("----------------------------------");
Map<String, Point> map2 = new LinkedHashMap<String, Point>();
map2.put("a", new Point(3, 4));
map2.put("b", new Point(5, 6));
String s2 = gson.toJson(map2);
System.out.println(s2);
Map<String, Point> retMap2 = gson.fromJson(s2,
new TypeToken<Map<String, Point>>() {
}.getType());
for (String key : retMap2.keySet()) {
System.out.println("key:" + key + " values:" + retMap2.get(key));
}
}
}
结果:
[plain] view
plaincopy
[[{"x":5,"y":6},"a"],[{"x":8,"y":8},"b"]]
key:Point [x=5, y=6] values:a
key:Point [x=8, y=8] values:b
{Point [x=5, y=6]=a, Point [x=8, y=8]=b}
----------------------------------
{"a":{"x":3,"y":4},"b":{"x":5,"y":6}}
key:a values:Point [x=3, y=4]
key:b values:Point [x=5, y=6]
文章转自:http://blog.csdn.net/lk_blog/article/details/7685210
感谢博主的无私分享!
相关文章推荐
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Gson.toJson()时内存溢出StackOverflowError
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- Erlang中的映射组Map详细介绍
- c++中map的基本用法和嵌套用法实例分析
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP6 先修班 JSON实例代码