XML转JSON,java对象互转JSON

Test.java:

package com.xml.Test;



import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import net.sf.json.JSON;

import net.sf.json.JSONObject;

import net.sf.json.xml.XMLSerializer;



public class Test {



    public static void main(String[] args) throws IOException {

        

        //读取src下的xml文件

        File file=new File("src"+File.separator+"XJ.xml");

        

        //字符流输出

        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));

        String string=null;

        StringBuffer sb = new StringBuffer();

        while((string = reader.readLine())!=null){

            sb.append(string);

        }

        XMLSerializer xmlSerializer=new XMLSerializer();

        JSON json=xmlSerializer.read(sb.toString());

        

        //这一句的输出,也许你很快的就知道原理了,其实原理很简单的!

        System.out.println("重点处:\n"+json.toString(1)+"\n");

        

        //截取掉[],转化为JSONObject

        JSONObject jsonObject=JSONObject.fromObject((json.toString()).substring(1, json.toString().length()-1));    

        System.out.println("截取后:\n"+jsonObject.toString(1)+"\n");

        

        

        //实例化后封装

        Galaxy galaxy=new Galaxy();

        

        //例:jsonObject.get("PersonSay").toString()---->JsonObject转java对象

        galaxy.setExplain(jsonObject.get("PersonSay").toString());

        JSONObject j2=JSONObject.fromObject(jsonObject.get("Galactics"));

        galaxy.setContent(j2.get("Venus").toString()+"、"+j2.get("Mercury").toString()+"、"+j2.get("Earth").toString()+"、"

        +j2.get("Mars").toString()+"、"+j2.get("Jupiter").toString()+"、"+j2.get("Saturn").toString()+"、"

        +j2.get("Uranus").toString()+"、"+j2.get("Neptune").toString());

        

        System.out.println(galaxy.getExplain()+"----->"+galaxy.getContent());

        

        //把Galaxy实体类转JSONObject

        JSONObject jclose=JSONObject.fromObject(galaxy);

        System.out.println("JSONObject格式如下:\n"+jclose.toString(1));

    }



}

Galaxy.java:

package com.xml.Test;



public class Galaxy {

    private String explain;

    private String content;

    public String getExplain() {

        return explain;

    }

    public void setExplain(String explain) {

        this.explain = explain;

    }

    public String getContent() {

        return content;

    }

    public void setContent(String content) {

        this.content = content;

    }

}

XJ.xml:

<?xml version="1.0" encoding="UTF-8"?>

<Result>

    <Universe>

        <Galactics>

            <Venus>金星</Venus>

            <Mercury>水星</Mercury>

            <Earth>地球</Earth>

            <Mars>火星</Mars>

            <Jupiter>木星</Jupiter>

             <Saturn>土星</Saturn>

             <Uranus>天王星</Uranus>

             <Neptune>海王星</Neptune>

        </Galactics>

        <PersonSay>由太阳起往外的顺序是的八大行星是</PersonSay>

    </Universe>

</Result>

所用到的包:commons-beanutils-1.7.0.jar,commons-collections-3.2.jar,commons-io-1.3.2.jar,commons-lang-2.4.jar,commons-logging-1.0.4.jar,ezmorph-1.0.6.jar,json-lib-2.2.3-jdk15.jar,xom-1.1.jar。本来想上传jar包的,不知道怎么上传就只有写jar包的名字了!

你可能感兴趣的:(xml转json)