JSONObject使用方法详解

  1. /**  
  2.  * 项目名称:tools  
  3.  * 项目包名:com.songfayuantools.json  
  4.  * 创建时间:2017年7月31日上午11:58:51  
  5.  * 创建者:Administrator-宋发元  
  6.  * 创建地点:  
  7.  */  
  8. package com.songfayuantools.json;  
  9.   
  10. import com.songfayuantools.entity.UserInfo;  
  11.   
  12. import net.sf.json.JSON;  
  13. import net.sf.json.JSONObject;  
  14. import net.sf.json.xml.XMLSerializer;  
  15.   
  16. /**  
  17.  * 描述:JSONObject使用方法详解  
  18.  *     JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。  
  19.  * @author songfayuan  
  20.  * 2017年7月31日上午11:58:51  
  21.  */  
  22. public class Json {  
  23.   
  24.     /**  
  25.      * 描述:json字符串转java代码  
  26.      * @author songfayuan  
  27.      * 2017年8月2日下午2:24:47  
  28.      */  
  29.     public static void jsonToJava() {  
  30.         System.out.println("json字符串转java代码");  
  31.         String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";  
  32.         JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
  33.         String username = jsonObject.getString("username");  
  34.         String password = jsonObject.getString("password");  
  35.         System.err.println("json--->java \n username="+username+"\t passwor="+password);  
  36.     }  
  37.       
  38.     /**  
  39.      * 描述:java代码封装为json字符串  
  40.      * @author songfayuan  
  41.      * 2017年8月2日下午2:30:58  
  42.      */  
  43.     public static void javaToJSON() {  
  44.         System.out.println("java代码封装为json字符串");  
  45.         JSONObject jsonObject = new JSONObject();  
  46.         jsonObject.put("username", "宋发元");  
  47.         jsonObject.put("age", 24);  
  48.         jsonObject.put("sex", "男");  
  49.         System.out.println("java--->json \n " + jsonObject.toString());  
  50.     }  
  51.       
  52.     /**  
  53.      * 描述:json字符串转xml字符串  
  54.      * @author songfayuan  
  55.      * 2017年8月2日下午2:56:30  
  56.      */  
  57.     public static void jsonToXML() {  
  58.         System.out.println("json字符串转xml字符串");  
  59.         String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\"}";  
  60.         JSONObject jsonObject = JSONObject.fromObject(jsonStr);  
  61.         XMLSerializer xmlSerializer = new XMLSerializer();  
  62.         xmlSerializer.setRootName("user_info");  
  63.         xmlSerializer.setTypeHintsEnabled(false);  
  64.         String xml = xmlSerializer.write(jsonObject);  
  65.         System.out.println("json--->xml \n" + xml);  
  66.     }  
  67.       
  68.     /**  
  69.      * 描述:xml字符串转json字符串  
  70.      * @author songfayuan  
  71.      * 2017年8月2日下午3:19:25  
  72.      */  
  73.     public static void xmlToJSON() {  
  74.         System.out.println("xml字符串转json字符串");  
  75.         String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>宋发元</username></user_info>";  
  76.         XMLSerializer xmlSerializer = new XMLSerializer();  
  77.         JSON json = xmlSerializer.read(xml);  
  78.         System.out.println("xml--->json \n" + json.toString());  
  79.     }  
  80.       
  81.     /**  
  82.      * 描述:javaBean转json字符串  
  83.      * @author songfayuan  
  84.      * 2017年8月2日下午3:39:10  
  85.      */  
  86.     public static void javaBeanToJSON() {  
  87.         System.out.println("javaBean转json字符串");  
  88.         UserInfo userInfo = new UserInfo();  
  89.         userInfo.setUsername("宋发元");  
  90.         userInfo.setPassword("123456");  
  91.         JSONObject jsonObject = JSONObject.fromObject(userInfo);  
  92.         System.out.println("JavaBean-->json \n" + jsonObject.toString());  
  93.     }  
  94.       
  95.     /**  
  96.      * 描述:javaBean转xml字符串  
  97.      * @author songfayuan  
  98.      * 2017年8月2日下午3:48:08  
  99.      */  
  100.     public static void javaBeanToXML() {  
  101.         System.out.println("javaBean转xml字符串");  
  102.         UserInfo userInfo = new UserInfo();  
  103.         userInfo.setUsername("songfayuan");  
  104.         userInfo.setPassword("66666");  
  105.         JSONObject jsonObject = JSONObject.fromObject(userInfo);  
  106.         XMLSerializer xmlSerializer = new XMLSerializer();  
  107.         String xml = xmlSerializer.write(jsonObject, "UTF-8");  
  108.         System.out.println("javaBean--->xml \n" + xml);  
  109.     }  
  110.       
  111.     public static void main(String args[]) {  
  112. //      jsonToJava();  
  113. //      javaToJSON();  
  114. //      jsonToXML();  
  115. //      xmlToJSON();  
  116. //      javaBeanToJSON();  
  117.         javaBeanToXML();  
  118.     }  
  119.       
  120. }  

实体

 

[html] view plain copy
 
  1. /**  
  2.  * 项目名称:tools  
  3.  * 项目包名:com.songfayuantools.entity  
  4.  * 创建时间:2017年8月2日下午3:34:46  
  5.  * 创建者:Administrator-宋发元  
  6.  * 创建地点:  
  7.  */  
  8. package com.songfayuantools.entity;  
  9.   
  10. /**  
  11.  * 描述:实体  
  12.  *   
  13.  * @author songfayuan 2017年8月2日下午3:34:46  
  14.  */  
  15. public class UserInfo {  
  16.     public String username;  
  17.     public String password;  
  18.   
  19.     public String getUsername() {  
  20.         return username;  
  21.     }  
  22.   
  23.     public void setUsername(String username) {  
  24.         this.username = username;  
  25.     }  
  26.   
  27.     public String getPassword() {  
  28.         return password;  
  29.     }  
  30.   
  31.     public void setPassword(String password) {  
  32.         this.password = password;  
  33.     }  
  34. }  

maven引入资源

[html] view plain copy
 
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>tools</groupId>  
  5.   <artifactId>tools</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>tools Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junit</groupId>  
  13.       <artifactId>junit</artifactId>  
  14.       <version>3.8.1</version>  
  15.       <scope>test</scope>  
  16.     </dependency>  
  17.       
  18.     <!-- <dependency>  
  19.         <groupId>com.alibaba</groupId>  
  20.         <artifactId>fastjson</artifactId>  
  21.         <version>1.2.8</version>  
  22.     </dependency> -->  
  23.   
  24.     <!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->  
  25.     <dependency>  
  26.         <groupId>net.sf.json-lib</groupId>  
  27.         <artifactId>json-lib</artifactId>  
  28.         <version>2.4</version>  
  29.         <classifier>jdk15</classifier>  
  30.     </dependency>  
  31.     <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->  
  32.     <dependency>  
  33.         <groupId>commons-lang</groupId>  
  34.         <artifactId>commons-lang</artifactId>  
  35.         <version>2.6</version>  
  36.     </dependency>  
  37.     <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->  
  38.     <dependency>  
  39.         <groupId>commons-logging</groupId>  
  40.         <artifactId>commons-logging</artifactId>  
  41.         <version>1.2</version>  
  42.     </dependency>  
  43.     <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->  
  44.     <dependency>  
  45.         <groupId>commons-beanutils</groupId>  
  46.         <artifactId>commons-beanutils</artifactId>  
  47.         <version>1.9.3</version>  
  48.     </dependency>  
  49.     <!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->  
  50.     <dependency>  
  51.         <groupId>commons-collections</groupId>  
  52.         <artifactId>commons-collections</artifactId>  
  53.         <version>3.2.1</version>  
  54.     </dependency>  
  55.       
  56.     <!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->  
  57.     <dependency>  
  58.         <groupId>net.sf.ezmorph</groupId>  
  59.         <artifactId>ezmorph</artifactId>  
  60.         <version>1.0.6</version>  
  61.     </dependency>  
  62.     <!-- https://mvnrepository.com/artifact/xom/xom -->  
  63.     <dependency>  
  64.         <groupId>xom</groupId>  
  65.         <artifactId>xom</artifactId>  
  66.         <version>1.2.5</version>  
  67.     </dependency>  
  68.       
  69.           
  70.   </dependencies>  
  71.   <build>  
  72.     <finalName>tools</finalName>  
  73.   </build>  
  74. </project>  
posted @ 2017-08-05 14:29  宋发元  阅读(4757)  评论(0编辑  收藏  举报