从json到java.util.Map编码/解码的库? [英] Library to encode/decode from json to java.util.Map?
本文介绍了从json到java.util.Map编码/解码的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更新
由于原因无法解释(有时我讨厌)我不能在我的环境中使用泛型。
我想要做的是这样做:
映射a = new HashMap();
a.put(name,Oscar);
Map b = new HashMap();
b.put(name,MyBoss);
a.put(boss,b);
列表list = new ArrayList();
list.add(a);
list.add(b);
String json = toJson(list);
//并创建json:
/ *
[
{
name:Oscar,
boss:{
name:MyBoss
}
},
{
name:MyBoss
}
]
* /
可以再次将其作为地图列表
从json(jsonStirng)列出aList =(List);
解决方案
JSON-Simple 看起来比较容易使用(下面的例子)。
地图到JSON:
Map map = new HashMap();
map.put(name,foo);
map.put(昵称,bar);
String jsonText = JSONValue.toJSONString(map);
JSON到列表/地图:
String s = yourJsonString;
列表列表=(JSONArray)JSONValue.parse(s);
Map map =(JSONObject)list.get(0);
Does anyone knows a java library that could easily encode java Maps into json objects and the other way around?
UPDATE
For reasons couldn't explain ( and I hate sometimes ) I can't use generics on my environment.
What' I'm trying to do is to have something like this:
Map a = new HashMap();
a.put( "name", "Oscar" );
Map b = new HashMap();
b.put( "name", "MyBoss");
a.put( "boss", b ) ;
List list = new ArrayList();
list.add( a );
list.add( b );
String json = toJson( list );
// and create the json:
/*
[
{
"name":"Oscar",
"boss":{
"name":"MyBoss"
}
},
{
"name":"MyBoss"
}
]
*/
And be able to have it again as a list of maps
List aList = ( List ) fromJson( jsonStirng );
解决方案
JSON-Simple looks relatively easy to use (examples below).
Map to JSON:
Map map = new HashMap();
map.put("name", "foo");
map.put("nickname", "bar");
String jsonText = JSONValue.toJSONString(map);
JSON to List/Map:
String s = yourJsonString;
List list = (JSONArray) JSONValue.parse(s);
Map map = (JSONObject) list.get(0);
这篇关于从json到java.util.Map编码/解码的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文