zoukankan      html  css  js  c++  java
  • xml数据转Json

    package com.baidu.com;
    
    public class User {
        String name = "";
        int age = 0;
        String adderss = "";
        String weight = "";
        String sex = "";
        String height = "";
        public User() {
            
        }
        public User(String name, int age, String adderss, String weight, String sex, String height) {
            super();
            this.name = name;
            this.age = age;
            this.adderss = adderss;
            this.weight = weight;
            this.sex = sex;
            this.height = height;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getAdderss() {
            return adderss;
        }
        public void setAdderss(String adderss) {
            this.adderss = adderss;
        }
        public String getWeight() {
            return weight;
        }
        public void setWeight(String weight) {
            this.weight = weight;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public String getHeight() {
            return height;
        }
        public void setHeight(String height) {
            this.height = height;
        }
        @Override
        public String toString() {
            return "User [name=" + name + ", age=" + age + ", adderss=" + adderss + ", weight=" + weight + ", sex=" + sex
                    + ", height=" + height + "]";
        }
        
        
        
    }
    View Code
    package com.baidu.com;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.alibaba.fastjson.JSON;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.dataformat.xml.XmlMapper;
    
    public class XmlDo {
    
        public static void main(String[] args) throws IOException {
            // TODO Auto-generated method stub
            XmlMapper xmlMapper = new XmlMapper();
            //对象转xml
            User user = new User("吴建波", 31, "北京市昌平区域沙河在南一村", "118", "男", "168");
            
            String temp = xmlMapper.writeValueAsString(user);
            System.out.println(temp);
            
            // xml转对象转json
            String temp2 = "<User><name>吴建波</name><age>31</age><adderss>北京市昌平区域沙河在南一村</adderss><weight>118</weight><sex>男</sex><height>168</height></User>";
            User userObject = xmlMapper.readValue(temp2, User.class);
            System.out.println(userObject);
            System.out.println(JSON.toJSONString(userObject));
            // xml转Map转Json
            String temp3 = "<apps><app><id>1</id><name>Google Maps</name><virsion>1.0</virsion></app></apps>";
            temp3 = "<appsii>" + temp3 + "</appsii>";
            Map map = xmlMapper.readValue(temp3, HashMap.class);
            System.out.println(map);
            System.out.println(JSON.toJSONString(map));
            
        }
    
    }
    View Code
  • 相关阅读:
    php面试题-2
    排序算法-1
    php面试题-1
    ES6基础-4
    ES6基础-3
    mysql优化-1
    ES6基础-2
    vuecli 项目进行gzip压缩 使页面加载速度更快
    vueCli3 项目创建时,git bash 箭头选择无效问题
    gulp4简单用法和问题总结
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/12267061.html
Copyright © 2011-2022 走看看