xml数据转Json

Posted 18513757531

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于xml数据转Json的主要内容,如果未能解决你的问题,请参考以下文章

PHP接口数据的封装XML和JSON(转)

如何从片段中的 JSON 响应中的对象获取数据

imglab .xml 标签格式转coco .json格式

java XML转JSON格式

php xml 和json转成Array(数组)格式和数组转成xml和json

php xml 和json转成Array(数组)格式和数组转成xml和json