• 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
     username=" + username
                        + "	 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 
    " + 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 
    " + 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 
    " + 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 
    " + 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 
    " + 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
     username=" + username
                        + "	 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 
    " + 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 
    " + 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 
    " + 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 
    " + 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 
    " + 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包,注意版本信息

  • 相关阅读:
    机器学习中的贝叶斯方法---当后验分布无法计算时如何求得预测模型?
    机器学习中的贝叶斯方法---先验概率、似然函数、后验概率的理解及如何使用贝叶斯进行模型预测(2)
    机器学习中的贝叶斯方法---先验概率、似然函数、后验概率的理解及如何使用贝叶斯进行模型预测(1)
    使用最大似然法来求解线性模型(4)-最大化似然函数背后的数学原理
    使用最大似然法来求解线性模型(3)-求解似然函数
    使用最大似然法来求解线性模型(2)-为什么是最大化似然函数?
    使用最大似然法来求解线性模型(1)
    关于CPU的User、Nice、System、Wait、Idle各个参数的解释
    Redhat Linux FTP配置
    基于at91rm9200的i2c分析(DS1307实时时钟芯片)
  • 原文地址:https://www.cnblogs.com/mr-wuxiansheng/p/6883233.html
Copyright © 2020-2023  润新知