Common data exchange format

What is a data exchange format?

In the company's web project, client and server exchange common data formats JSON, XMLand so on ftl template engine. At present most of the company's project server response to client requests are returned JSON data format.

Generally divided clients fall into two categories: mobile terminal , PC terminal .
iosAnd 安卓, using the means of communication is httpagreement + jsona restful style, is currently the most Internet companies to take the way of the project.

xmlIs used less and less, since the transmission resource consuming (broadband), heavier-weight. Therefore, under normal circumstances, the bank projects using httpprotocol + xmlmajority.

JSON

First of all we need to know what is JSON?

JSON( JavaScript Object Notation, The JS objects notation) is a lightweight data-interchange format . It is based on a subset of ECMAScript (European Computer Society norms established by js), using completely independent of the programming language of the text format to store and represent data . Simple and clear hierarchy make JSON an ideal data-interchange language. Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission .

In simple terms: JSONis a lightweight data interchange format, structure clear and easy to use, compared to XMLheavyweight data exchange format is more easy to use, so the client and server side using JSONthe way of data exchange format to communicate.

JSON data structure as follows:

{
    "fruit": [
        {
            "name": "苹果",
            "address": "烟台",
            "price": "12.9",
            "unit": "kg"
        },
        {
            "name": "香蕉",
            "address": "海南",
            "price": "6.5",
            "unit": "kg"
        }
    ]
}

jsonData presented in two formats, the object / array .
In "{"the beginning "}"end, "{}"wrapped as a jsontarget object to which a value key valueis stored format. "[]"It represents jsonan array, which is loaded with a number of jsonobjects.

Commonly used JSON parsing framework
fastjson (Ali), gson(Google), jackson(SpringMVC own)

For example fastJson of API
Here Insert Picture DescriptionHere Insert Picture Description

XML

First of all we need to know what XML is?

Extensible markup language subset, standard generalized markup language ( Extensible Markup Languageabbreviated XML). Is a marker for an electronic document to have a structured markup language. The main role is to (describe) to store some data, or used for configuration files, such as springprojects on the use of the xmlfile.

Use xmlshould pay attention to the file header structure, need to declare the version number, encoding. xmlPresented by way tag, nested layers, it can be seen as a tree structure.

XML data structure as follows:

<?xml version="1.0" encoding="UTF-8"?>  
<students>  
    <student id="1">  
        <name>小明</name>  
        <address>深圳</address>  
        <sex>1</sex>  
    </student>
	... ...    
</students>  

XML parsing mode
Dom4j , Sax, Pull, generally these types.

dom4jNot suitable for parsing large files, because it is at once the file is loaded into memory, so there may be out of memory, saxit is based on the event to parse the xml, so he can parse xml large files, it is precisely because of this so dom4jwe can be flexible xml CRUD and navigation, but not so strong sax flexibility, so saxoften used to parse large xmlfiles, but to to xmlfile some flexible ( crud) operation to use dom4j.

The company projects parsing xmlI use dom4j, using dom4jmainly operating Documentobject. Creating SAXReaderthe object read by the target file, get Documentafter the object is very convenient, and then get root, that rootnode, the operation is after some iterations to obtain operational attributes / text and the like.

Published 13 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/QingHe97/article/details/103826844