• map转换成JSON的方法


    第一种alibaba falstjson:

    1.Map转JSON

      Map map = new HashMap();
            map.put("a", "a");
            map.put("b", "123");
    
            JSONObject json = new JSONObject(map);
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.map转string

      Map map = new HashMap<>();
            map.put("a", "b");
            String s = JSONObject.toJSONString(map);
    
    • 1
    • 2
    • 3

    3.JSON转String

      JSONObject json = new JSONObject();
            json.put("c", "v");
            json.put("z", "123n);
    
            json.toJSONString();
    
    • 1
    • 2
    • 3
    • 4
    • 5

    4.JSON转Map

    JSONObject json = new JSONObject();
            json.put("ccc", "321");
            json.put("bbb", "123");
    
            Map map = (Map)json;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.String转JSON

     String str = "{"username":"dsad","qwewqe":"123"}";
    JSONObject json = JSONObject.parseObject(str);
    
    • 1
    • 2

    2 google

    maven坐标

    
    
        com.google.code.gson
        gson
        2.3.1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    //Map转换成JSON

    Map map = new HashMap(); 
    map.put("a","aaa"); 
    map.put("b","bbb"); 
    map.put("c","ccc"); 
    String json=JSON.toJSONString(map); 
    System.out.println(json);//输出{"a":"aaa","b":"bbb","c":"ccc"}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    //JSON转换成Map

    Map map1 = JSON.parseObject(json);
    System.out.println(map1.get("a"));
    for (Object mapData : map.entrySet()) {
    Map.Entry entry = (Map.Entry)mapData;
    System.out.println(entry.getKey()+"--->"+entry.getValue());
    }
    /*输出
    b--->bbb
    c--->ccc
    a--->aaa
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    map中含有对象Map -> JSON

    //Map -> JSON
    Map map = new HashMap(); 
    map.put("a",new Bar()); 
    map.put("b",new Bar()); 
    map.put("c",new Bar()); 
    String json = JSON.toJSONString(map,true); 
    System.out.println(json); 
    /*
    输出{
    	"a":{
    		"barAge":383687382,
    		"barDate":1494945882018,
    		"barName":"name_1689176802"
    	},
    	"b":{
    		"barAge":-100528778,
    		"barDate":1494945882018,
    		"barName":"name_-878176366"
    	},
    	"c":{
    		"barAge":-344075192,
    		"barDate":1494945882018,
    		"barName":"name_-1710740534"
    	}
    }
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    //JSON -> Map

    Map map1 = (Map)JSON.parse(json); 
    for (String key : map1.keySet()) { 
    System.out.println(key+":"+map1.get(key)); 
    } 
    /*输出
    b:{"barAge":-100528778,"barDate":1494945882018,"barName":"name_-878176366"}
    c:{"barAge":-344075192,"barDate":1494945882018,"barName":"name_-1710740534"}
    a:{"barAge":383687382,"barDate":1494945882018,"barName":"name_1689176802"}
    */
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    -------------------=-------------------------附–MAP的ASCII排序-----------------------=------------------------

    StringBuilder sb = new StringBuilder();
    	List keys = new ArrayList(map.keySet());
    	Collections.sort(keys);//排序。
    	for(String k : keys){
    		String v = params.get(k);
    		if(StringUtils.isNotEmpty(v)){
    			sb.append(v);
    		}
    	}
    	//return MD5.toMD5(sb+key, "UTF-8");这个就不用看了~~~
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    Log4j2 ThreadContext日志链路追踪
    yolo项目中如何训练自己的数据集
    数据仓库综述
    九、2023.10.3.Linux(end).9
    暂停Windows更新方法
    阿里云ECS服务器安装docker
    《多GPU大模型训练与微调手册》
    Linux常用命令
    华为云云耀云服务器L实例评测|部署功能强大的办公套件 ONLYOFFICE
    3倍通气的医用外科口罩,佩戴舒适过滤不打折
  • 原文地址:https://blog.csdn.net/m0_67401606/article/details/126435043