XML轉JSON,java對象互轉JSON

來源:互聯網
上載者:User

標籤:

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,java對象互轉JSON

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.