Map类型获取json数组,如何提取值
{
"0": {
"林XX": [
{
"first": "姓名:",
"second": "XX",
"third": "周一",
"forth": "周二",
"fifth": "周三",
"sixth": "周四",
"seventh": "周五"
},
{
"first": "第1节",
"second": "学科",
"third": "数学",
"forth": "数学",
"fifth": "数学",
"sixth": "数学",
"seventh": "数学"
}
],
"赵XX": [
{
"first": "姓名:",
"second": "赵XX",
"third": "周一",
"forth": "周二",
"fifth": "周三",
"sixth": "周四",
"seventh": "周五"
},
{
"first": "第1节",
"second": "学科",
"third": "数学",
"forth": "数学",
"fifth": "数学",
"sixth": "数学",
"seventh": "数学"
}
]
}
}
如我想获取林xx的 第一个second值 怎么用Map类型获取 展开
我们需要先把json字符串转化为net.sf.json.JSONObject对象,java中这样就可以完成json字符串到Map的转换了。
1.将数组转换为JSON:String[] arr = {"asd","dfgd","asd","234"};JSONArray jsonarray = JSONArray.fromObject(arr);System.out.println(jsonarray);
2.对象转换成JSON:UserInfo user = new UserInfo(1001,"张三");JSONArray jsonArray = JSONArray.fromObject(user);System.out.println( jsonArray );
3.把Map转换成json, 要使用jsonObject对象:Map<String, Object> map = new HashMap<String, Object>();map.put("userId", 1001);map.put("userName", "张三");map.put("userSex", "男");JSONObject jsonObject = JSONObject.fromObject(map);System.out.println(jsonObject);
4.把List转换成JSON数据:List<UserInfo> list = new ArrayList<UserInfo>();UserInfo user = new UserInfo(1001, "张三");list.add(user);list.add(user);list.add(user);JSONArray jsonArray = JSONArray.fromObject(list);System.out.println(jsonArray);