Java Json/xml 序列化和還原序列化工具:JsonTools 和 simpleframework【附帶執行個體】__Java

來源:互聯網
上載者:User

Json序列化:將java bean轉換成Json字串;Json還原序列化:將Json字串轉換成java bean。

xml序列化:將java bean轉換成xml檔案;xml還原序列化:將xml檔案轉換成java bean。

1. xml 序列化還原序列化工具:simple framework  功能非常強大,使用起來非常簡單。在這裡可以下載到它:http://sourceforge.net/projects/simple/

使用方法:首先根據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 工具 Json序列化還原序列化工具:JsonTools JsonTools是一個不錯的JSON序列化和還原序列化功能包,在這裡可以下載到它: http://sourceforge.net/projects/jontools/。 使用介紹:

 //序列化 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;    }}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.