Java Json/xml 序列化和反序列化: 工具JsonTools 和 simpleframework
1. xml 序列化反序列化工具:simpleframework 功能非常强大,使用起来非常简单,自己可以百度一下
使用方法:首先根据xml文件建好java bean ,可以是非常复杂的java bean; 对应的xml文件和java bean 见文章尾部
导入:
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
直接使用:
File file = new File("c://mystd.xml");
Serializer serializer = new Persister();
//反序列化
Bean rule=serializer.read(Bean.class, file);
//序列化
serializer.write(rule, file);
下面主要介绍json的序列化反序列化 工具
2.Json 工具
xml 序列化反序列化工具:JsonTools
JsonTools是一个不错的JSON序列化和反序列化功能包,在这里可以下载到它:
http://jsontools.berlios.de/。
使用介绍:
//序列化
JSONValue jsonValue = JSONMapper.toJSON( bean);
String jsonStr = jsonValue.render(true); //是否格式化
System.out.println(jsonStr);
//反序列化
JSONParser parser = new JSONParser(new StringReader(jsonStr));
L2Rule bean = (L2Rule) JSONMapper.toJava(parser.nextValue(), L2Rule.class);
原始xml文件:
<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 bad debts of " USID="USGB123453" Level="2" />
<Row ItemName="Current assets" USID="USGB123454" Level="2" />
</Table>
</Sheet>
</L2Rule>
解析后的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":"Current assets","level":"2","usid":"USGB123454"}]},"type":"BS"}]}
加格式:
{
"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"
}
]
}
对应的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; } }
下一遍将介绍将树形数据读取成json格式
Sunny liu
2009-11-19