json java javabean xml 之相互转换 适合初学者

<span style="font-size:18px;">

package json;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import json.UserInfo;
import org.apache.log4j.*;
import org.apache.log4j.Logger;
/**
 * author lz
 * date 2015-09-20
 */
public class JsonObjectTest {
	 private static Logger logger = Logger.getLogger(JsonObjectTest.class);

	      //java封装成json串
	     public static void javaToJSON() {
	    	 
			 System.out.println("java代码封装为json字符串");
			 JSONObject jsonObj = new JSONObject();
			 jsonObj.put("username", "lizhe");
			 jsonObj.put("password", "123456");
			 System.out.println("java--->json \n" + jsonObj.toString());
		 }
	      //解析json串
		 public static void jsonToJAVA() {
			 
			 System.out.println("json字符串转java代码");
			 String jsonStr = "{\"username\":\"lizhe\",\"password\":\"123456\"}";
			 JSONObject jsonObj = JSONObject.fromString(jsonStr);
			 String username = jsonObj.getString("username");
			 String password = jsonObj.getString("password");
			 System.out.println("json--->java\n username=" + username
			 + "\t password=" + password);
		 }
		//json2xml
		 public static void jsonToXML() {
			 
			 System.out.println("json字符串转xml字符串");
			 String jsonStr = "{\"username\":\"lizhe\",\"password\":\"123456\"}";
			 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);
		 }
		 //javabean2json
		 public static void javaBeanToJSON() {
			 
			 System.out.println("javabean转json字符串");
			 UserInfo userInfo = new UserInfo();
			 userInfo.setUsername("lizhe");
			 userInfo.setPassword("123456");
			 JSONObject json = JSONObject.fromBean(userInfo);
			 System.out.println("javabean--->json \n" + json.toString());
		 }
		 //javabean2xml
		 public static void javaBeanToXML() {
			 
			 System.out.println("javabean转xml字符串");
			 UserInfo userInfo = new UserInfo();
			 userInfo.setUsername("lizhe");
			 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);
		 }
		 //xml2json
		 public static void xmlToJSON(){
		  
			 System.out.println("xml字符串转json字符串");
			 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password>123456</password><username>lizhe</username></user_info>";
			 JSON json=XMLSerializer.read(xml);
			 System.out.println("xml--->json \n"+json.toString());
		   
		 
		   
		   }
		 
		 public static void main(String args[]) {
					 
			  //javaToJSON();
			  //jsonToJAVA();
			  jsonToXML();
			  //javaBeanToJSON();
			  //javaBeanToXML();
			  //xmlToJSON();
			
		 }
		 

}
</span>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值