首页 > 代码库 > jsonObject关于xml,json,bean之间的转换关系

jsonObject关于xml,json,bean之间的转换关系

1.json转换为JAVA

@Test
        public void jsonToJAVA() {
            System.out.println("json字符串转java代码");
            String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}";//添加转义字符
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            String username = jsonObj.getString("username");
            String password = jsonObj.optString("password");
            System.out.println("json--->java\n username=" + username
                    + "\t password=" + password);
        }

运行结果:

json字符串转java代码
json--->java
 username=张三     password=

 

2.java转换为JSON

@Test
        public void javaToJSON() {
            System.out.println("java代码封装为json字符串");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("username", "张三");
            jsonObj.put("password", "");
            System.out.println("java--->json \n" + jsonObj.toString());
        }

运行结果:

java代码封装为json字符串
java--->json 
{"username":"张三","password":""}

 

3.json转换为XML

@Test
        public void jsonToXML() {
            System.out.println("json字符串转xml字符串");
            String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}";
            JSONObject json = JSONObject.fromObject(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字符串转xml字符串
json--->xml 
<?xml version="1.0" encoding="UTF-8"?>
<user_info><password/><username>张三</username></user_info>

 

4.XML转换为JSON

@Test
        public void xmlToJSON() {
            System.out.println("xml字符串转json字符串");
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password></password><username>张三</username></user_info>";
            XMLSerializer xmlSerializer = new XMLSerializer();
            JSON json = xmlSerializer.read(xml);
            System.out.println("xml--->json \n" + json.toString());
        }

运行结果:

xml字符串转json字符串
五月 20, 2017 9:20:18 下午 net.sf.json.xml.XMLSerializer getType
信息: Using default type string
xml--->json 
{"password":[],"username":"张三"}

 

5.javaBean转换为JSON

@Test
        public void javaBeanToJSON() {
            System.out.println("javabean转json字符串");
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("张三");
            userInfo.setPassword("");
            JSONObject json = JSONObject.fromObject(userInfo);
            System.out.println("javabean--->json \n" + json.toString());
        }

运行结果:

javabean转json字符串
javabean--->json 
{"password":"","username":"张三"}

 

6.javaBean转换为XML

@Test
        public void javaBeanToXML() {
            System.out.println("javabean转xml字符串");
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("张三");
            userInfo.setPassword("");
            JSONObject json = JSONObject.fromObject(userInfo);
            XMLSerializer xmlSerializer = new XMLSerializer();
            String xml = xmlSerializer.write(json, "UTF-8");
            System.out.println("javabean--->xml \n" + xml);
        }

运行结果:

javabean转xml字符串
javabean--->xml 
<?xml version="1.0" encoding="UTF-8"?>
<o><password type="string"/><username type="string">张三</username></o>

 

 

 

贴上全部代码:

package org.xiaowu.json.demo;

import org.junit.Test;

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

public class JsonObjectDemo {
        @Test
        public void jsonToJAVA() {
            System.out.println("json字符串转java代码");
            String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}";
            JSONObject jsonObj = JSONObject.fromObject(jsonStr);
            String username = jsonObj.getString("username");
            String password = jsonObj.optString("password");
            System.out.println("json--->java\n username=" + username
                    + "\t password=" + password);
        }
        
        
        

        @Test
        public void javaToJSON() {
            System.out.println("java代码封装为json字符串");
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("username", "张三");
            jsonObj.put("password", "");
            System.out.println("java--->json \n" + jsonObj.toString());
        }

        
        
        
        @Test
        public void jsonToXML() {
            System.out.println("json字符串转xml字符串");
            String jsonStr = "{\"password\":\"\",\"username\":\"张三\"}";
            JSONObject json = JSONObject.fromObject(jsonStr);
            XMLSerializer xmlSerializer = new XMLSerializer();
            xmlSerializer.setRootName("user_info");
            xmlSerializer.setTypeHintsEnabled(false);
            String xml = xmlSerializer.write(json);
            System.out.println("json--->xml \n" + xml);
        }
        
        
        

        @Test
        public void xmlToJSON() {
            System.out.println("xml字符串转json字符串");
            String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><user_info><password></password><username>张三</username></user_info>";
            XMLSerializer xmlSerializer = new XMLSerializer();
            JSON json = xmlSerializer.read(xml);
            System.out.println("xml--->json \n" + json.toString());
        }
        
        
        

        @Test
        public void javaBeanToJSON() {
            System.out.println("javabean转json字符串");
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("张三");
            userInfo.setPassword("");
            JSONObject json = JSONObject.fromObject(userInfo);
            System.out.println("javabean--->json \n" + json.toString());
        }
        
        
        
        

        @Test
        public void javaBeanToXML() {
            System.out.println("javabean转xml字符串");
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername("张三");
            userInfo.setPassword("");
            JSONObject json = JSONObject.fromObject(userInfo);
            XMLSerializer xmlSerializer = new XMLSerializer();
            String xml = xmlSerializer.write(json, "UTF-8");
            System.out.println("javabean--->xml \n" + xml);
        }

}

 

package org.xiaowu.json.demo;

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;
    }

}

 

 

以及项目所必须导入的jar包,注意版本信息

技术分享

 

jsonObject关于xml,json,bean之间的转换关系