Map转换json字符串

import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONObject;

public class testJson {
 
    public static void main(String[] args) {
        testPingping();
    }
    public static void testPingping(){ 
        Map smallMap = new HashMap(); 
        smallMap.put("userId", "438");
        smallMap.put("name", "平平/萍萍");
        smallMap.put("sex", "不知男女");
        smallMap.put("reason", "和学成睡一晚,后来学成嘴疼"); 
    JSONObject jsonObject = JSONObject.fromObject(smallMap);
    System.out.println("下面是专家开具的鉴定结果:"); //A joke
    System.out.println(jsonObject);
    System.out.println("患者鉴定完毕!"); //A joke
    System.out.println("下一位"); //A joke

    } 
}
也可以是Map类型

输出的结果:

 
   

下面是结果:
{"b_name":"平平/萍萍","d_reason":"和学成睡一晚,后来学成嘴疼","a_userId":"438","c_sex":"不知男女"}

--------------------------------------------------------------------------------------------------

需要引入的jar包列表:

cglib-nodep-2.2.jar
commons-beanutils-1.8.0.jar
commons-collections-3.2.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
ezmorph-1.0.4.jar
gson-2.8.0-javadoc.jar
gson-2.8.0-sources.jar
gson-2.8.0.jar
jettison-1.3.1.jar
json-lib-2.2.1-jdk13.jar
json.jar

 

转载于:https://www.cnblogs.com/sunBinary/p/10244545.html

你可能感兴趣的:(Map转换json字符串)