jsonObject关于xml,json,bean之间的转换关系-阿里云开发者社区

开发者社区> 开发与运维> 正文
登录阅读全文

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

简介: 1.json转换为JAVA @Test public void jsonToJAVA() { System.out.println("json字符串转java代码"); String jsonStr = "{\"password\":...

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包,注意版本信息

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

分享:
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

其他文章
最新文章
相关文章