json字符串与map/list集合之间的相互转换以及map集合的三种遍历方式

package com.tydic.background.ws.paperless.client;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.google.gson.Gson;
import com.tydic.jtcrm.server.utils.JsonInfoUtil;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class JsonToMap {
    public static void main(String[] args) {
          List> list = new ArrayList>();
            Map map1 = new HashMap();
            map1.put("name", "p");
            map1.put("cj", "5");
            Map map2 = new HashMap();
            map2.put("name", "h");
            map2.put("cj", "12");
            Map map3 = new HashMap();
            map3.put("name", "f");
            map3.put("cj", "31");
            list.add(map1);
            list.add(map3);
            list.add(map2);
            
            //输出对象值
            System.out.println(map1);//{cj=5, name=p}
            System.out.println(list);//[{cj=5, name=p}, {cj=31, name=f}, {cj=12, name=h}]
            
            //1.json 与map/list 之间的相互转换
            //将对象值转换成json格式字符串
            String jsonMap = JsonInfoUtil.objToJson(map1); 
            String jsonList = JsonInfoUtil.objToJson(list);
            System.out.println(jsonMap);//{"cj":"5","name":"p"}
            System.out.println(jsonList);//[{"cj":"5","name":"p"},{"cj":"31","name":"f"},{"cj":"12","name":"h"}]
            
           //(1)通过com.google.gson包实现Json字符串与Map/List>)对象之间的格式转换
            Gson gson = new Gson();
            Map map=gson.fromJson(jsonMap, Map.class);
            List> listMap = gson.fromJson(jsonList,List.class);
            System.out.println(map);
            System.out.println(listMap);
            
            //(2)通过net.sf.json包实现Json字符串与Map/List>)对象之间的格式转换
            System.out.println("==============================================");
            System.out.println(JSONObject.fromObject(map1));
            System.out.println(JSONArray.fromObject(list));
            System.out.println((Map)JSONObject.toBean(JSONObject.fromObject(jsonMap),Map.class));
            System.out.println((List>)JSONArray.toList(JSONArray.fromObject(jsonList),Map.class));
            
            System.out.println("==============================================");
            
            //2.map 遍历
           //第一种:通过Map.keySet()遍历key和value:
            Set keys = map1.keySet(); 
            Iterator iterator = keys.iterator();
            while(iterator.hasNext()){
                String key = iterator.next(); 
                String value = map1.get(key).toString();//
                System.out.println("key:"+key+" value:"+value );
            }
            
            for(String key:map1.keySet()){//keySet获取map集合key的集合  然后在遍历key即可
                String value = map1.get(key).toString();//
                System.out.println("key:"+key+" vlaue:"+value);
            }
            
            //第二种:通过Map.entrySet使用iterator遍历key和value
            Iterator> it = map1.entrySet().iterator();
            while(it.hasNext()){
                Entry entry = it.next();
                System.out.println("key:"+entry.getKey()+"  key:"+entry.getValue());
            }
            // Map集合循环遍历方式推荐,尤其是容量大时
            for (Map.Entry m : map1.entrySet()) {
                System.out.println("key:" + m.getKey() + " value:" + m.getValue());
            }
            
            //第三种:通过Map.values()遍历所有的value,但不能遍历key");
            for(Object m:map.values()){
                System.out.println(m);
            }
            
            //3.补充json与map之间转换的内容
            //装换成map/List时,会将json数据的所有的内容都转换成对象的形式,后面取值是直接用map对象get对象的方式去获取;如果有需要可以进行强转和设置泛型
            System.out.println("==============================================");

            map1.put("map2", map2);
            System.out.println(list);//[{map2={cj=12, name=h}, cj=5, name=p}, {cj=31, name=f}, {cj=12, name=h}]
            Gson gson2 = new Gson();
            String listString = gson2.toJson(list);
            System.out.println(listString);//[{"map2":{"cj":"12","name":"h"},"cj":"5","name":"p"},{"cj":"31","name":"f"},{"cj":"12","name":"h"}]
            List> listMap2 = gson2.fromJson(listString,List.class);
            System.out.println(listMap2);//[{map2={cj=12, name=h}, cj=5, name=p}, {cj=31, name=f}, {cj=12, name=h}]
            System.out.println((List>)JSONArray.toList(JSONArray.fromObject(listString),Map.class));//[{map2=net.sf.ezmorph.bean.MorphDynaBean@1534f01b[{cj=12, name=h}], cj=5, name=p}, {cj=31, name=f}, {cj=12, name=h}]
           System.out.println("==============================================");

            Map map4 = (Map) listMap2.get(0);
            System.out.println((Map)listMap2.get(0).get("map2"));
            System.out.println(listMap2.get(0).get("map2"));
            
          
    }

}
 

你可能感兴趣的:(Java,后端开发)