菜鸟笔记
提升您的技术认知

map与json之间转换-ag真人游戏

//map转换成json

map map = new hashmap(); 
map.put("a","aaa"); 
map.put("b","bbb"); 
map.put("c","ccc"); 
string json=json.tojsonstring(map); 
system.out.println(json);//输出{"a":"aaa","b":"bbb","c":"ccc"}

//json转换成map

map map1 = json.parseobject(json);
system.out.println(map1.get("a"));
for (object mapdata : map.entryset()) {
map.entry entry = (map.entry)mapdata;
system.out.println(entry.getkey() "--->" entry.getvalue());
}
/*输出
b--->bbb
c--->ccc
a--->aaa
*/

map中含有对象map -> json

//map -> json
map map = new hashmap(); 
map.put("a",new bar()); 
map.put("b",new bar()); 
map.put("c",new bar()); 
string json = json.tojsonstring(map,true); 
system.out.println(json); 
/*
输出{
	"a":{
		"barage":383687382,
		"bardate":1494945882018,
		"barname":"name_1689176802"
	},
	"b":{
		"barage":-100528778,
		"bardate":1494945882018,
		"barname":"name_-878176366"
	},
	"c":{
		"barage":-344075192,
		"bardate":1494945882018,
		"barname":"name_-1710740534"
	}
}
*/

//json -> map

map map1 = (map)json.parse(json); 
for (string key : map1.keyset()) { 
system.out.println(key ":" map1.get(key)); 
} 
/*输出
b:{"barage":-100528778,"bardate":1494945882018,"barname":"name_-878176366"}
c:{"barage":-344075192,"bardate":1494945882018,"barname":"name_-1710740534"}
a:{"barage":383687382,"bardate":1494945882018,"barname":"name_1689176802"}
*/
网站地图