map轉換成字符串的方法

第一種:json-lib

依賴:

<dependency>
    <groupId>net.sf.json-lib</groupId>
    <artifactId>json-lib</artifactId>
    <version>2.4</version>
    <classifier>jdk15</classifier>
</dependency>

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        JSONObject jsonObject = JSONObject.fromObject(map);
	        //將json對象轉化爲json字符串
	        String result = jsonObject.toString();

第二種:alibaba的fastJson(推薦)

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
</dependency>

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        String result = JSONUtils.toJSONString(map);

第三種:google的gson

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>

方法:

            Map map = new HashMap();
	        map.put("sex", "男");
	        String result = new Gson().toJson(param).toString();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章