fastjson 怎样把字符串转换成json

 我来答
运动一圈
2016-06-22 · TA获得超过949个赞
知道大有可为答主
回答量:964
采纳率:92%
帮助的人:490万
展开全部
// jackson
private static ObjectMapper om = new ObjectMapper ();
public static String objectToString (Object obj ) throws IOException{
return om.writeValueAsString (obj );
}

@Test
public void test1 () throws IOException {
Map<String, Object> map = new HashMap<String, Object>();

Map<String, String> map2 = new HashMap<String, String>();
map2.put ("name", "1");
map2.put ("age", "12");
map.put ("att", map2 );

List<String> list = new ArrayList<String>();
list.add ("hi");list.add ("hello");
map.put ("list", list );

List<Map<String, String>> confuselist = new ArrayList<Map<String, String>>();
Map<String, String> map3 = new HashMap<String, String>();
map3.put ("name", "1");
map3.put ("age", "12");
confuselist.add (map3 );
confuselist.add (map2 );
map.put ("confuselist", confuselist );

Object array = Array.newInstance (String.class, 10 );
Array.set (array, 0, "a");
Array.set (array, 1, "b");
Array.set (array, 2, "c");
map.put ("array", array );
// 使用 jackson
System.out.println (objectToString (map ));
// 使用 fastJson
System.out.println (JSON.toJSONString (map ));
}

输出:
{"att":{"age":"12","name":"1"},"list":["hi","hello"],"confuselist":[{"age":"12","name":"1"}, {"age":"12","name":"1"}],"array":["a","b","c",null,null,null,null,null,null,null]}

{"att":{"age":"12","name":"1"},"list":["hi","hello"],"confuselist":[{"age":"12","name":"1"},{"$ref":"$.att"}],"array":["a","b","c",null,null,null,null,null,null,null]}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式