/**
* 项目名称:tools
* 项目包名:com.songfayuantools.json
* 创建时间:2017年7月31日上午11:58:51
* 创建者:Administrator-宋发元
* 创建地点:
*/
package com.songfayuantools.json;
import com.songfayuantools.entity.UserInfo;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
/**
* 描述:JSONObject使用方法详解
* JSONObject-lib包是一个beans,collections,maps,java arrays和xml和JSON互相转换的包。
* @author songfayuan
* 2017年7月31日上午11:58:51
*/
public class Json {
/**
* 描述:json字符串转java代码
* @author songfayuan
* 2017年8月2日下午2:24:47
*/
public static void jsonToJava() {
System.out.println("json字符串转java代码");
String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
String username = jsonObject.getString("username");
String password = jsonObject.getString("password");
System.err.println("json--->java \n username="+username+"\t passwor="+password);
}
/**
* 描述:java代码封装为json字符串
* @author songfayuan
* 2017年8月2日下午2:30:58
*/
public static void javaToJSON() {
System.out.println("java代码封装为json字符串");
JSONObject jsonObject = new JSONObject();
jsonObject.put("username", "宋发元");
jsonObject.put("age", 24);
jsonObject.put("sex", "男");
System.out.println("java--->json \n " + jsonObject.toString());
}
/**
* 描述:json字符串转xml字符串
* @author songfayuan
* 2017年8月2日下午2:56:30
*/
public static void jsonToXML() {
System.out.println("json字符串转xml字符串");
String jsonStr = "{\"username\":\"宋发元\",\"password\":\"123456\",\"age\":\"24\"}";
JSONObject jsonObject = JSONObject.fromObject(jsonStr);
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setRootName("user_info");
xmlSerializer.setTypeHintsEnabled(false);
String xml = xmlSerializer.write(jsonObject);
System.out.println("json--->xml \n" + xml);
}
/**
* 描述:xml字符串转json字符串
* @author songfayuan
* 2017年8月2日下午3:19:25
*/
public static void xmlToJSON() {
System.out.println("xml字符串转json字符串");
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>宋发元</username></user_info>";
XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println("xml--->json \n" + json.toString());
}
/**
* 描述:javaBean转json字符串
* @author songfayuan
* 2017年8月2日下午3:39:10
*/
public static void javaBeanToJSON() {
System.out.println("javaBean转json字符串");
UserInfo userInfo = new UserInfo();
userInfo.setUsername("宋发元");
userInfo.setPassword("123456");
JSONObject jsonObject = JSONObject.fromObject(userInfo);
System.out.println("JavaBean-->json \n" + jsonObject.toString());
}
/**
* 描述:javaBean转xml字符串
* @author songfayuan
* 2017年8月2日下午3:48:08
*/
public static void javaBeanToXML() {
System.out.println("javaBean转xml字符串");
UserInfo userInfo = new UserInfo();
userInfo.setUsername("songfayuan");
userInfo.setPassword("66666");
JSONObject jsonObject = JSONObject.fromObject(userInfo);
XMLSerializer xmlSerializer = new XMLSerializer();
String xml = xmlSerializer.write(jsonObject, "UTF-8");
System.out.println("javaBean--->xml \n" + xml);
}
public static void main(String args[]) {
// jsonToJava();
// javaToJSON();
// jsonToXML();
// xmlToJSON();
// javaBeanToJSON();
javaBeanToXML();
}
}
实体
/**
* 项目名称:tools
* 项目包名:com.songfayuantools.entity
* 创建时间:2017年8月2日下午3:34:46
* 创建者:Administrator-宋发元
* 创建地点:
*/
package com.songfayuantools.entity;
/**
* 描述:实体
*
* @author songfayuan 2017年8月2日下午3:34:46
*/
public class UserInfo {
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
maven引入资源
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tools</groupId>
<artifactId>tools</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>tools Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency> -->
<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/xom/xom -->
<dependency>
<groupId>xom</groupId>
<artifactId>xom</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
<build>
<finalName>tools</finalName>
</build>
</project>