- package com.zkn.newlearn.json;
-
- import com.alibaba.fastjson.JSON;
- import com.alibaba.fastjson.JSONObject;
- import java.util.Map;
-
- public class JsonToMapTest01 {
-
- public static void main(String[] args){
-
- String str = "{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}";
-
- Map maps = (Map)JSON.parse(str);
- System.out.println("这个是用JSON类来解析JSON字符串!!!");
- for (Object map : maps.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" " + ((Map.Entry)map).getValue());
- }
-
- Map mapTypes = JSON.parseObject(str);
- System.out.println("这个是用JSON类的parseObject来解析JSON字符串!!!");
- for (Object obj : mapTypes.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapTypes.get(obj));
- }
-
- Map mapType = JSON.parseObject(str,Map.class);
- System.out.println("这个是用JSON类,指定解析类型,来解析JSON字符串!!!");
- for (Object obj : mapType.keySet()){
- System.out.println("key为:"+obj+"值为:"+mapType.get(obj));
- }
-
-
- Map json = (Map) JSONObject.parse(str);
- System.out.println("这个是用JSONObject类的parse方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- JSONObject jsonObject = JSONObject.parseObject(str);
- System.out.println("这个是用JSONObject的parseObject方法来解析JSON字符串!!!");
- for (Object map : json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
-
-
- Map mapObj = JSONObject.parseObject(str,Map.class);
- System.out.println("这个是用JSONObject的parseObject方法并执行返回类型来解析JSON字符串!!!");
- for (Object map: json.entrySet()){
- System.out.println(((Map.Entry)map).getKey()+" "+((Map.Entry)map).getValue());
- }
- String strArr = "{{\"0\":\"zhangsan\",\"1\":\"lisi\",\"2\":\"wangwu\",\"3\":\"maliu\"}," +
- "{\"00\":\"zhangsan\",\"11\":\"lisi\",\"22\":\"wangwu\",\"33\":\"maliu\"}}";
-
- System.out.println(json);
- }
- }
posted @
2017-05-08 19:49
高手坟墓、ls
阅读(
696)
评论()
编辑
收藏
举报