Summary of the invention
It is an object of the invention to overcome the deficiencies in the prior art, it is provided that a kind of mechanism based on JSON is supported
Old data interactive method and system, it can simplify data interactive mode, improve data transmission efficiency, it is ensured that number
According to communication security.
The technical scheme provided according to the present invention, described Aged caring institutions data interactive method based on JSON, bag
Include handheld terminal and the endowment server of endowment data interaction can be carried out with described handheld terminal;Described hand-held
Data interaction between terminal and endowment server uses JSON data form.
When using JSON data form to carry out endowment data interaction, determine the packet of endowment data to be exchanged
Protocol format, and the endowment data determining data pack protocol form are carried out packet encapsulation, to obtain
The encapsulated data packet of JSONObject, is compressed the most successively described encapsulated data packet and encrypts, to obtain
Compression encryption data bag, with wrapped in by compression encryption data carry out between handheld terminal and endowment server required
Data interaction.
The described data pack protocol form determining endowment data to be exchanged includes type codes, business datum, deletion
Data and more new data.
A kind of Aged caring institutions data interaction system based on JSON, including handheld terminal and with described hand-held end
End can carry out the endowment server of endowment data interaction;Data between described handheld terminal and endowment server are handed over
Use JSON data form mutually.
When using JSON data form to carry out endowment data interaction, determine the packet of endowment data to be exchanged
Protocol format, and the endowment data determining data pack protocol form are carried out packet encapsulation, to obtain
The encapsulated data packet of JSONObject, is compressed the most successively described encapsulated data packet and encrypts, to obtain
Compression encryption data bag, with wrapped in by compression encryption data carry out between handheld terminal and endowment server required
Data interaction.
The described data pack protocol form determining endowment data to be exchanged includes type codes, business datum, deletion
Data and more new data.
Advantages of the present invention: the data interaction between described handheld terminal and endowment server uses JSON data lattice
Formula, can simplify data interactive mode, improves data transmission efficiency, it is ensured that data communications security.
Detailed description of the invention
Below in conjunction with concrete drawings and Examples, the invention will be further described.
As shown in Figure 1: in order to be able to simplify data interactive mode, improve data transmission efficiency, the present invention based on
The Aged caring institutions data interaction system of JSON, can support parents including handheld terminal and with described handheld terminal
The endowment server of data interaction;Data interaction between described handheld terminal and endowment server uses JSON number
According to form.
Specifically, JavaScript Object Notation (hereinafter referred to as JSON) is the data of a kind of lightweight
DIF, it uses the text formatting being totally independent of language.JSON uses two kinds of structures: 1) " title
/ value " to set, " name/value " can represent the attribute of object, and object is actually the set of attribute, so
This structure can be used to represent object, uses " { } " to represent a JSON in typical JSON form
Object.2), the ordered list of value, the set of object can be represented, thus JSON can represent one multiple
The set of miscellaneous object.In typical JOSN form, " [] " is used to represent JSON array.Based on JAVA
Platform has the relevant class libraries for processing JSON data of comparative maturity, as org.json bag can be very
Generate easily and resolve JSON.
When using JSON data form to carry out endowment data interaction, determine the packet of endowment data to be exchanged
Protocol format, and the endowment data determining data pack protocol form are carried out packet encapsulation, to obtain
The encapsulated data packet of JSONObject, is compressed the most successively described encapsulated data packet and encrypts, to obtain
Compression encryption data bag, with wrapped in by compression encryption data carry out between handheld terminal and endowment server required
Data interaction.In the embodiment of the present invention, handheld terminal can be that existing conventional smart mobile phone, intelligence are flat
The terminal units such as plate, data interaction refers to that between handheld terminal and endowment server, data are mutually transmitted, the most hand-held
Data interaction between terminal and endowment server all uses JSON data form.At handheld terminal to endowment clothes
During business device transmission data, handheld terminal needs in compression encryption data bag transmission to endowment server;And support
Old server is when handheld terminal transmission data, and endowment server needs to transmit to hands compression encryption data bag
Hold terminal.And support parents server or after handheld terminal receives compression encryption data bag, need compression encryption
Packet carries out deciphering and the resolving of correspondence, is described in detail concrete implementation process below.
When determining the data pack protocol form of endowment data, described data pack protocol is a JSONObject
Object, comprises OperationCode (type codes), OperationData (business datum), DeleteData
(deletion data) and four fields of UpdateData (more new data), described field is according to handheld terminal
With the data syn-chronization requirement of endowment server, determine in conjunction with the business demand feature of geriatric nursing home.Concrete lattice
Formula is as shown in following table one.
Table one: data pack protocol form
OperationCode type codes is a value, shows the various operations in data interaction and state, is divided into
Upload conditional code and the big class of download state code two.Wherein, conditional code is uploaded for handheld terminal to server end
Send request, show that handheld terminal carries out business operation type, such as login authentication, data renewal etc.;Download
The conditional code server end that is used for supporting parents returns response data to handheld terminal, and the server that shows to support parents is carrying out industry
Various states during business operation, such as code error, Network Abnormal etc..Described type codes is by Aged caring institutions pipe
The business demand of reason system and determine, dynamically adjust along with the extension of business.Below will be with system login
As a example by process, introduce the data exchange process of this system.The type of service code used in login process such as table two
Shown in.
OperationData business datum is a JSONObject object, and different type codes correspond to difference
Attribute.Uploading type codes during as logged in is Login, the data included in business datum be user name,
Password and three attributes of mechanism coding, this just can uniquely determine the membership of this user and geriatric nursing home.
DeleteData deletes data and UpdateData more new data is a JSONArray object, uses
Data syn-chronization between handheld terminal and endowment server.In the embodiment of the present invention, use based on the time
The data synchronization mechanism of stamp, when handheld terminal to endowment server send data more newly requested time, corresponding class
Type code is Refresh, and business datum is the timestamp t comprising now handheld terminal;Endowment server is through resolving
Obtain this time stamp t afterwards, then according to the timestamp t of this time stamp t with endowment each traffic table of server
Relatively, if there is the traffic table bigger than this time stamp t, then by corresponding business datum according to protocol package lattice
Formula is encapsulated among UpdateData field or DeleteData field.Handheld terminal analysis protocol bag, if
Above-mentioned two field has the content of correspondence, then handheld terminal carries out data deletion or the data insertion behaviour of correspondence
Make to keep the concordance of data.
Data envelope is die-filling is to carry out data assembly according to above-mentioned data pack protocol form, and data pack protocol is exactly
One JSONObject, each field is the most nested JSONObject, JSONArray etc..Base
Org.json bag in java provides a series of simple Method and kit for for processing JSON data.Wherein
Org.json.JSONbject class provides multiple constructor to build JSONbject, uses put method to add
Name-value pair, get method obtain the value corresponding to field or object etc..Such as when handheld terminal logs in, now
Uploading type codes is Login, and operation data are user name, password and geriatric nursing home numbering these three data.By
In the data that data to be deleted are to be updated, so above-mentioned two field is empty, on
The specific implementation process stated is known to those skilled in the art, the most no longer describes in detail.
Data compression main purpose is to reduce handheld terminal flow and save server end broadband, and its inverse process is
Decompress.In geriatric nursing home management system actual application, on the one hand handheld terminal is first initialized
Need during data to the endowment a large amount of basic data of server request;On the other hand handheld terminal need frequently to
Endowment server uploads the business operation information such as old man's nursing situation.Consider to carry out data based on this kind of situation
Transmit after compression, reduce volume of transmitted data, save server broadband.
JAVA platform has the Deflater class in java.util.zip bag and Inflater class provide based on
The compression of DEFLATE algorithm and the correlation technique of decompression, they call ZLIB by JNI technology and compress storehouse
Support general compression requirements.Compress to be provided by Deflater class and support, mainly include two steps.First
The character string of compression is converted into byte arrays, in embodiments of the present invention, is completed by getBytes () method
Character string, to the conversion of byte arrays, arranges Compression Strategies and compression speed after changing and performs in compressor
Compression.Compression Strategies depends on the feature of compressed content, optionally has FILTERED, be best suited for mainly by
Little value forms and has the Compression Strategies of the arbitrarily data of distribution to a certain degree;HUFFMAN_ONLY, only
It is applicable to the Compression Strategies of Huffman coding;And general acquiescence Compression Strategies
DEFAULT_STRATEGY, uses this rank in embodiments of the present invention.Compression level has 0 to 90
Individual rank is optional, and 0 represents not compression, and 9 represent optimal compression effect, the most as shown in Figure 2.
The safety of data encryption principal security data communication, its inverse process is deciphering.Encryption and decryption is to liking warp
Cross the protocol packet data of encapsulation.In the present invention implements, use symmetric encipherment algorithm that byte stream is encrypted,
Symmetric cryptography refers to encrypt and uses identical key with deciphering.Encryption and decryption frame J CF based on java provides
A series of for encrypting and the instrument of deciphering, core classes is Cipher.Call getInstance (" DES ") to generate
Cipher object, it is intended that this encryption equipment uses des encryption algorithm, it is also possible to specify the encryption of other modes to calculate
Method such as AES/RSA;Then call init () method Cipher object is initialized, encryption is set close simultaneously
Key and encryption mode (encrypt or decipher);Finally call doFinal () and carry out encryption or the deciphering of reality, this
Depend on the encryption mode arranged when initializing.If initialization pattern is encryption, then doFinal is encrypted;
If initialized pattern is deciphering, then it is decrypted, as shown in Figure 4.
In the embodiment of the present invention, in data exchange, after receiving compression decrypted data packet, need to carry out
Deciphering, decompression and resolving, wherein, decrypting process is the inverse process of above-mentioned ciphering process, deciphering
Process is determined according to above-mentioned ciphering process, and detailed process is known to those skilled in the art, the most not
Repeat again.Decompression is the inverse process of compression, serves as decompression machine by Inflater class.First the word that will decompress
Symbol string is converted into byte arrays, is then enter in decompression machine, is output as original byte arrays, then
It is reduced to original character string by toString () method.It should be noted that the Compression Strategies when decompression
With compression level must and during compression the value of setting consistent, character turns byte and have to specify identical volume simultaneously
Code mode, as shown in Figure 3.
Data resolution module is the inverse operation to data package module, completes the parsing to protocol package.Resolving will
First protocol packet data must build JSONObject object, is then obtained by each field name in protocol package
Its concrete value or object, carry out next step business operation accordingly.Such as when obtaining above-mentioned protocol package,
Build JSONObject object first by JSONObject (String), call get (" OperationCode ")
Acquisition type codes is Login, learns as login authentication business operation;Then getJSONObject is called
(" OperationData ") obtains login authentication desired data, encodes according to user name password and geriatric nursing home
Obtain corresponding data.
If login authentication is out of joint, such as password or username bad, the most above-mentioned OperationCode
Corresponding value is LOGIN_ERROR, shows that log-on message is wrong.Other field is empty, then according to
JSON data encapsulation process, returns to handheld terminal using this content as logon error protocol package.Handheld terminal
According to above-mentioned JSON data parsing, it is thus achieved that logging status code is LOGIN_ERROR, then points out hand-held
Terminal user name or code error.
The work to the present invention is carried out below by a data exchange process of handheld terminal and endowment server end
Process illustrates.First, handheld terminal before the request is sent, needs wanting according to JSON package module
Ask and the business datum sent is encapsulated as JSONObject according to data pack protocol.Secondly through compression module pair
Packaged data are compressed, and are then passed through encrypting the data to after compression and are encrypted, finally will encryption
The data of output send.
After endowment server receives request, first have to it is decrypted, and by the data after deciphering
As the input of decompression, then obtained the field of protocol package definition by JSON parsing module, and sentence accordingly
Disconnected the need of carrying out Business Processing, the most then be compressed and encrypt rear send endowment to handheld terminal
The response of server, otherwise current communication process terminates.