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
  • 相关阅读:
    1.1.28 文字所在段落增加下划线
    Microsoft Project 2010基础使用方法
    16.3 将Win7文档的内容到复制Linux的vi中
    3.4 在Word中的公式和序号之间填充连续的点
    18.25 JLink调试程序步骤
    18.24 Ubuntu修改静态IP
    18.23 inline函数功能
    18.22 sprintf函数功能
    18.21 关键字extern
    18.20 频率单位转换
  • 原文地址:https://www.cnblogs.com/wujianbo123/p/12267061.html
Copyright © 2011-2022 走看看