xml数据转Json

2021-04-19 22:27

阅读:368

标签:图片   eval   getname   lap   div   hide   weight   proc   event   

 

技术图片技术图片
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 = "吴建波31北京市昌平区域沙河在南一村118168";
        User userObject = xmlMapper.readValue(temp2, User.class);
        System.out.println(userObject);
        System.out.println(JSON.toJSONString(userObject));
        // xml转Map转Json
        String temp3 = "1Google Maps1.0";
        temp3 = "" + temp3 + "";
        Map map = xmlMapper.readValue(temp3, HashMap.class);
        System.out.println(map);
        System.out.println(JSON.toJSONString(map));
        
    }

}
View Code

 

xml数据转Json

标签:图片   eval   getname   lap   div   hide   weight   proc   event   

原文地址:https://www.cnblogs.com/wujianbo123/p/12267061.html


评论


亲,登录后才可以留言!