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

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值