XML to Json,java object Mutual transfer JSON

Source: Internet
Author: User

Test.java:

 Packagecom.xml.Test;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportNet.sf.json.JSON;ImportNet.sf.json.JSONObject;ImportNet.sf.json.xml.XMLSerializer; Public classTest { Public Static voidMain (string[] args)throwsIOException {//read the XML file under SRCFile file=NewFile ("src" +file.separator+ "xj.xml"); //character Stream OutputBufferedReader reader =NewBufferedReader (NewInputStreamReader (NewFileInputStream (file), "UTF-8")); String String=NULL; StringBuffer SB=NewStringBuffer ();  while(String = Reader.readline ())! =NULL) {sb.append (string); } XMLSerializer XMLSerializer=NewXMLSerializer (); JSON JSON=Xmlserializer.read (sb.tostring ()); //The output of this sentence, you may soon know the principle, in fact, the principle is very simple! System.out.println ("Key points: \ n" +json.tostring (1) + "\ n"); //Intercept [], convert to JsonobjectJsonobject Jsonobject=jsonobject.fromobject ((json.tostring ()). substring (1, json.tostring (). Length ()-1)); System.out.println ("After interception: \ n" +jsonobject.tostring (1) + "\ n"); //Instantiate post EncapsulationGalaxy galaxy=NewGalaxy (); //Example: Jsonobject.get ("Personsay"). ToString ()---->jsonobject to Java objectGalaxy.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 ()); //turn Galaxy entity class into JsonobjectJsonobject jclose=Jsonobject.fromobject (Galaxy); System.out.println (The Jsonobject format is as follows: \ n "+jclose.tostring (1)); }}

Galaxy.java:

 Packagecom.xml.Test; Public classGalaxy {PrivateString explain; PrivateString content;  PublicString Getexplain () {returnexplain; }     Public voidSetexplain (String explain) { This. explain =explain; }     PublicString getcontent () {returncontent; }     Public voidsetcontent (String content) { This. Content =content; }}

Xj.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Result>    <Universe>        <Galactics>            <Venus>Venus</Venus>            <Mercury>Mercury</Mercury>            <Earth>Earth</Earth>            <Mars>Mars</Mars>            <Jupiter>Jupiter</Jupiter>             <Saturn>Saturn</Saturn>             <Uranus>Planet Uranus</Uranus>             <Neptune>Neptune</Neptune>        </Galactics>        <Personsay>The order of the sun from the outside is the eight planets are</Personsay>    </Universe></Result>

Package used: 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. Originally want to upload jar package, do not know how to upload only the name of the jar package write!

XML to Json,java object Mutual transfer JSON

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.