关于 JsonObject 的使用

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

实体

[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>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值