使用的jar包为net.sf.json
//转化为list
String jsonString = "[{'goodsId':'11','name':'苹果'},{'goodsId':'222','name':'橡胶'}]";
JSONArray jsonArray = JSONArray.fromObject(jsonString);
List<Map<String, Object>> mapListJson = (List) jsonArray;
for (int i = 0; i < mapListJson.size(); i++)
{
Map<String, Object> map = mapListJson.get(i);
System.out.println(map.get("name"));
}//转化为map
String jsonString = "{'goodsId':'11','name':'苹果'}";
JSONObject jsonArray = JSONObject.fromObject(jsonString);
Map<String, Object> mapListJson = (Map<String, Object>)jsonArray;
System.out.println(mapListJson.get("name"));
本文介绍如何使用net.sf.json库将JSON字符串转换为Java中的List和Map类型,并通过示例代码展示了具体的实现过程。
838

被折叠的 条评论
为什么被折叠?



