日期:2014-05-16  浏览次数:20529 次

JAVABean_XML_JSON相互转换
package com.yaodaqing.json.test;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;

/**
 * JAVABean_XML_JSON相互转换
 * 
 * @author 姚大庆
 *
 * 2012年4月23日11:24:48
 */
public class JAVABean_XML_JSON {
	
	public static void main(String args[]) {
		// javaToJSON();
		// jsonToJAVA();
		// jsonToXML();
		// javaBeanToJSON();
		// javaBeanToXML();
		// xmlToJSON();
		 jsonToJAVABean();
		// xmlToJAVABean();
	}
	
	/**
	 * JSON格式转JAVA
	 */
	public static void jsonToJAVA() {
		System.out.println("json字符串转java代码");
		String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
		JSONObject jsonObj = JSONObject.fromString(jsonStr);
		String username = jsonObj.getString("username");
		String password = jsonObj.optString("password");
		//在这里一个属性一个属性的设置到JAVABean中
		System.out.println("json--->java\n username=" + username + "\t password=" + password);
	}

	/**
	 * JSON格式转XML格式
	 */
	public static void jsonToXML() {
		System.out.println("json字符串转xml字符串");
		String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
		JSONObject json = JSONObject.fromString(jsonStr);
		XMLSerializer xmlSerializer = new XMLSerializer();
		xmlSerializer.setRootName("user_info");
		xmlSerializer.setTypeHintsEnabled(false);
		String xml = xmlSerializer.write(json);
		System.out.println("json--->xml \n" + xml);
	}
	
	/**
	 * JSON格式直接转JAVABean
	 */
	@SuppressWarnings("static-access")
	public static void jsonToJAVABean(){
		String jsonStr = "{\"password\":\"123456\",\"username\":\"张三\"}";
		JSONObject jsonObj = JSONObject.fromString(jsonStr);
		UserInfo user = (UserInfo) jsonObj.toBean(jsonObj,UserInfo.class);
		System.out.println(user.getPassword());
		System.out.println(user.getUsername());
	}

	/**
	 * XML文件转JSON格式
	 */
	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>";
		JSON json = XMLSerializer.read(xml);
		System.out.println("xml--->json \n" + json.toString());
	}
	
	/**
	 * XML直接转JAVABean
	 */
	public static void xmlToJAVABean(){
		String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>张三</username></user_info>";
		System.out.println("xml=="+xml);
		JSON json = XMLSerializer.read(xml);
		JSONObject jsonObj = JSONObject.fromString(json.toString());
		System.out.println("json=="+json.toString());
		UserInfo user = (UserInfo) jsonObj.toBean(jsonObj,UserInfo.class);
		System.out.println(user.getPassword());
		System.out.println(user.getUsername());
	}
	
	/**
	 * JAVABean转JSON格式
	 */
	public static void javaBeanToJSON() {
		System.out.println("javabean转json字符串");
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername("张三");
		userInfo.setPassword("123456");
		JSONObject json = JSONObject.fromBean(userInfo);
		System.out.println("javabean--->json \n" + json.toString());
	}
	
	/**
	 * JAVABean转XML格式
	 */
	public static void javaBeanToXML() {
		System.out.println("javabean转xml字符串");
		UserInfo userInfo = new UserInfo();
		userInfo.setUsername("张三");
		userInfo.setPassword("123456");
		JSONObject json = JSONObject.fromBean(userInfo);
		XMLSerializer xmlSerializer = new XMLSerializer();
		String xml = xmlSerializer.write(json, "UTF-8");
		System.out.println("javabean--->xml \n" + xml);
	}

	/**
	 * JAVA转JSON格式
	 */
	public static void javaToJSON() {
		System.out.println("java代码封装为json字符串");
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("username", "张三");
		jsonObj.put("password", "123456");
		System.out.println("java--->json \n" + jsonObj.toString());
	}

}


package com.yaodaqing.json.test;

public class UserInfo {
	
	public String username;
	public String password;

	public String getUsername