Java json/xml serialization and deserialization tools: Jsontools and Simpleframework "incidental instances" __java

Source: Internet
Author: User
Tags serialization

JSON serialization: Converts a Java bean into a JSON string; JSON deserialization: Converts a JSON string into a Java bean.

XML serialization: Converting Java beans to XML files; XML deserialization: Converting an XML file into a Java bean.

1. XML Serialization deserialization tool: Simple framework features are very powerful and easy to use. Here you can download it: http://sourceforge.net/projects/simple/

How to: First build Java beans from an XML file, which can be a very complex Java bean. The corresponding XML file and Java bean are shown at the end of the article.

Import:

Import Org.simpleframework.xml.Serializer;
Import Org.simpleframework.xml.core.Persister;

Direct use:

File File = new file ("C://mystd.xml");
  Serializer serializer = new Persister ();
Deserialize
  the Bean rule=serializer.read (bean.class, file);
Serialization of
serializer.write (rule, file);

The following is a description of the serialization deserialization tool for JSON

2.Json Tool Json Serialization deserialization tool: Jsontools Jsontools is a good JSON serialization and deserialization feature Pack, where it can be downloaded to: http://sourceforge.net/projects/jontools/. Use introduction:

Serialization of
 Jsonvalue Jsonvalue = Jsonmapper.tojson (bean);   
 String jsonstr = Jsonvalue.render (true);   Whether to format
 System.out.println (JSONSTR);  

 Deserialization 
 jsonparser parser = new Jsonparser (new StringReader (JSONSTR));   
 L2rule bean = (l2rule) Jsonmapper.tojava (Parser.nextvalue (), l2rule.class);  

Raw XML file:

<L2Rule>
<sheet type= "BS" > 
 <Table> 
  <row itemname= "total assets" usid= "USGB123450" Level= "0"/> 
  <row itemname= "Total Current Assets" usid= "USGB123451"  level= "1"/> 
  <row Itemname= "Cash and Cash equivalents" usid= "USGB123452" level= "2"/> <row itemname= 
  "Accounts receivable, net of Allowance for ill debts of "usid=" USGB123453 "level=" 2 "/> <row itemname= 
  " Current Assets "usid=" USGB123454 "Lev El= "2"/> 
    </Table> 
</Sheet>
</L2Rule>


Parsed JSON: {"sheet": [{"Tables": {"Rows": [{"ItemName": "Total Assets", "level": "0", "usid": "USGB123450"},{"ItemName": " Total current Assets ", Level": "1", "Usid": "USGB123451"},{"ItemName": "Cash and Cash equivalents", "Level": "2", "Usid": " USGB123452 "},{" "ItemName": "Accounts receivable, net of allowance for bad debts of", "Level": "2", "Usid": "USGB123453"},{" ItemName ":" Assets "," Level ":" 2 "," Usid ":" USGB123454 "}}," type ":" BS "}]}
Add Format: {
"Sheet":
[
{
"Tables":
{
"Rows":
[
{
"ItemName": "Total assets",
"Level": "0",
"Usid": "USGB123450"
},
{
"ItemName": "Total current Assets",
"Level": "1",
"Usid": "USGB123451"
},
{
"ItemName": "Cash and Cash equivalents",
"Level": "2",
"Usid": "USGB123452"
},
{
"ItemName": "Accounts receivable, net of allowance for bad debts of",
"Level": "2",
"Usid": "USGB123453"
},
{
"ItemName": "Current Assets",
"Level": "2",
"Usid": "USGB123454"
}
]
},
' type ': ' BS '
}
]
}
corresponding Bean:
L2rule.java
Import org.simpleframework.xml.ElementList;
Import Org.simpleframework.xml.Root;

@Root (name= "L2rule") Public
class L2rule
{
	@ElementList (name= "Sheet", Required=false,inline=true) Public
	list<sheet> Sheet = new linkedlist<sheet> ();

	Public list<sheet> Getsheet ()
    {return
    	Sheet;
    }

	public void Setsheet (list<sheet> Sheet)
    {
    	this.sheet = Sheet;
    }

	
}

Sheet.java
Import Org.simpleframework.xml.Attribute;
Import org.simpleframework.xml.Element;
Import Org.simpleframework.xml.Root;

@Root (name= "Sheet") Public
class Sheet
{
	@Element (name= "table", Required=false) public
	Table tables = new Table ();
	
	@Attribute (name= "type", required=false) public
	String Type;

	

	Public Table gettables ()
    {return
    	tables;
    }

	public void Settables (Table tables)
    {
    	this.tables = tables;
    }

	Public String GetType ()
    {return
    	type;
    }

	public void SetType (String type)
    {
    	this.type = type;
    }
}

Table.java
Import java.util.LinkedList;
Import java.util.List;

Import org.simpleframework.xml.ElementList;
Import Org.simpleframework.xml.Root;

@Root (name= "table") Public
class Table
{
	@ElementList (name= "Row", Required=false,inline=true)
	Public list<row> rows = new linkedlist<row> ();

	Public list<row> getRows ()
    {return
    	rows;
    }

	public void Setrows (list<row> rows)
    {
    	this.rows = rows;
    }
}

Row.java
Import Org.simpleframework.xml.Attribute;
Import Org.simpleframework.xml.Root;

@Root (name= "Row") Public
class Row
{
	@Attribute (name= "ItemName", required=true) public
	String ItemName;
	@Attribute (name= "Usid", required=true) public
	String Usid;
	@Attribute (name= "level", required=true) public
	String level;
	Public String getitemname ()
    {return
    	itemname;
    }
	public void Setitemname (String itemname)
    {
    	this.itemname = itemname;
    }
	Public String Getusid ()
    {return
    	usid;
    }
	public void Setusid (String usid)
    {
    	this.usid = usid;
    }
	Public String Getlevel ()
    {return level
    	;
    }
	public void Setlevel (String level)
    {
    	this.level = level;
    }
	
}


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.