Summary of the invention
The purpose of the present invention is overcoming the deficiencies in the prior art, a kind of Aged caring institutions data based on JSON are provided
Exchange method and system, can simplify data interactive mode, and improve data transfer efficiency guarantees data communications security.
According to technical solution provided by the invention, the Aged caring institutions data interactive method based on JSON, including it is hand-held
Terminal and the endowment server that can be carried out endowment data interaction with the handheld terminal;The handheld terminal and endowment server
Between data interaction use JSON data format.
When carrying out endowment data interaction using JSON data format, the data pack protocol lattice of endowment data to be exchanged are determined
Formula, and to the endowment data progress packet encapsulation for determining data pack protocol format, to obtain the encapsulation of data of JSONObject
Packet, is successively compressed and is encrypted again to the encapsulated data packet, to obtain compression encrypted packet, to pass through compression encryption number
Required data interaction is carried out between handheld terminal and endowment server according to packet.
The data pack protocol format of determination endowment data to be exchanged include type codes, business datum, delete data with
And more new data.
A kind of Aged caring institutions data interaction system based on JSON, including handheld terminal and can be into the handheld terminal
The endowment server of row endowment data interaction;Data interaction between the handheld terminal and endowment server uses JSON data lattice
Formula.
When carrying out endowment data interaction using JSON data format, the data pack protocol lattice of endowment data to be exchanged are determined
Formula, and to the endowment data progress packet encapsulation for determining data pack protocol format, to obtain the encapsulation of data of JSONObject
Packet, is successively compressed and is encrypted again to the encapsulated data packet, to obtain compression encrypted packet, to pass through compression encryption number
Required data interaction is carried out between handheld terminal and endowment server according to packet.
The data pack protocol format of determination endowment data to be exchanged include type codes, business datum, delete data with
And more new data.
Advantages of the present invention: the data interaction between the handheld terminal and endowment server uses JSON data format, energy
Simplify data interactive mode, improve data transfer efficiency guarantees data communications security.
Specific embodiment
Below with reference to specific drawings and examples, the invention will be further described.
As shown in Figure 1: in order to simplify data interactive mode, improve data transfer efficiency, the present invention is based on the machines of JSON
Structure endowment data interaction system, can be carried out the endowment service of endowment data interaction including handheld terminal and with the handheld terminal
Device;Data interaction between the handheld terminal and endowment server uses JSON data format.
Specifically, JavaScript Object Notation (hereinafter referred to as JSON) is a kind of data exchange of lightweight
Format, it is using the text formatting for being totally independent of language.JSON uses two kinds of structures: 1) set of " name/value " pair, " name
Title/value " can indicate that the attribute of object, object are actually the set of attribute, so this structure can be used to indicate object,
A JSON object is indicated using " { } " in typical JSON format.2), the ordered list being worth, can indicate the collection of object
It closes, so JSON can indicate the set of a complex object.In typical JOSN format, JSON is indicated using " [] "
Array.Based on the related class libraries for being used to handle JSON data for having had comparative maturity in JAVA platform, such as org.json packet
It very easily can generate and parse JSON.
When carrying out endowment data interaction using JSON data format, the data pack protocol lattice of endowment data to be exchanged are determined
Formula, and to the endowment data progress packet encapsulation for determining data pack protocol format, to obtain the encapsulation of data of JSONObject
Packet, is successively compressed and is encrypted again to the encapsulated data packet, to obtain compression encrypted packet, to pass through compression encryption number
Required data interaction is carried out between handheld terminal and endowment server according to packet.In the embodiment of the present invention, handheld terminal can be
The terminal devices such as existing common smart phone, Intelligent flat, data interaction refer to data between handheld terminal and endowment server
JSON data format is all made of in data interaction between mutually transmission, i.e. handheld terminal and endowment server.Handheld terminal to
When server transport data of supporting parents, handheld terminal needs for compression encrypted packet to be transmitted in endowment server;And clothes of supporting parents
When business device transmits data to handheld terminal, endowment server needs compression encrypted packet being transmitted to handheld terminal.And it supports parents
Server or handheld terminal receive compression encrypted packet after, need to compression encrypted packet carry out it is corresponding decryption and
Resolving is below described in detail specific implementation process.
When determining the data pack protocol format of endowment data, the data pack protocol is a JSONObject object,
Comprising OperationCode (type codes), OperationData (business datum), DeleteData (delete data) and
Four fields of UpdateData (more new data), the field be according to handheld terminal with endowment server data synchronize want
Ask, in conjunction with geriatric nursing home business demand feature and determine.Specific format is as shown in following table one.
Table one: data pack protocol format
OperationCode type codes are a value, show various operations and the state in data interaction, are divided into upload shape
State code and download state code two major classes.Wherein, it uploads status code and sends request to server end for handheld terminal, show to hold
Terminal carries out business operation type, such as login authentication, data update;Download state code is for server end of supporting parents to hand-held end
Returning response data are held, show various states of the server when carrying out business operation of supporting parents, such as password mistake, Network Abnormal
Deng.The type codes are determined by the business demand of Aged caring institutions management system, and with the extension of business, dynamic is adjusted.
Below the data exchange process of this system will be introduced by taking system login process as an example.The type of service code used in login process
As shown in Table 2.
Table two: type of service code
OperationData business datum is a JSONObject object, and different type codes correspond to different categories
Property.It is Login that type codes are uploaded when such as logging in, and data included in business datum are user name, password and mechanism coding three
A attribute, this can uniquely determine the membership of this user and geriatric nursing home.
DeleteData deletes data and UpdateData more new data is a JSONArray object, for holding
Data are synchronous between terminal and endowment server.In the embodiment of the present invention, using the data synchronization mechanism based on timestamp,
When handheld terminal to endowment server issue data update request when, corresponding type codes be Refresh, business datum be comprising
The timestamp t of handheld terminal at this time;Endowment server by parsing after obtains this time stamp t, then according to this time stab t with
The timestamp t of the endowment each traffic table of server compares, if in the presence of the traffic table also bigger than this time stamp t, then it will be corresponding
Business datum is encapsulated among UpdateData field or DeleteData field according to protocol package format.Handheld terminal parsing
Protocol package, if above-mentioned two field has corresponding content, handheld terminal carries out corresponding data deletion or data insertion behaviour
Make the consistency to keep data.
Die-filling data envelope is that data assembly is carried out according to above-mentioned data pack protocol format, and data pack protocol is exactly one
JSONObject, each field again nested according to demand JSONObject, JSONArray etc..Org.json based on java
Packet provides a series of simple Method and kit fors for handling JSON data.Wherein org.json.JSONbject class provides
A variety of constructors construct JSONbject, name-value pair is added using put method, get method obtains value corresponding to field or
Object etc..Such as when handheld terminal logs in, uploading type codes at this time is Login, and operation data is user name, password and endowment
Mechanism numbers these three data.Due to the data both not deleted at this time or the data not updated, so above-mentioned two
Field is sky, and above-mentioned specific implementation process is known to those skilled in the art, and and will not be described here in detail.
Data compression main purpose is to reduce handheld terminal flow and save server end broadband, and inverse process is decompression
Contracting.In geriatric nursing home management system actual application, one side handheld terminal is needed in first initialized data to feeding
Old server requests a large amount of basic datas;Another aspect handheld terminal, which needs frequently to upload old man to endowment server, nurses feelings
The business operations information such as condition.It is transmitted after being compressed data based on the consideration of such situation, reduces volume of transmitted data, save service
Device broadband.
There are Deflater class and Inflater class in java.util.zip packet to provide and be based in JAVA platform
The compression of DEFLATE algorithm and the correlation technique of decompression, they call ZLIB to compress library and support general compression by JNI technology
Demand.Compression provides support by Deflater class, mainly includes two steps.Byte is converted by the character string of compression first
Array completes the conversion of character string to byte arrays by getBytes () method, after changing in embodiments of the present invention
Setting Compression Strategies and compression speed execute compression in compressor.Compression Strategies depend on the feature of compressed content, optionally
There is FILTERED, is best suited for that the Compression Strategies for the data being arbitrarily distributed to a certain degree are mainly made of and had small value;
HUFFMAN_ONLY is only applicable to the Compression Strategies of Huffman coding;And general default Compression Strategies DEFAULT_
STRATEGY uses this rank in embodiments of the present invention.Compression level has 0 to 90 ranks optional, and 0 indicates not compress,
9 represent optimal compression effect, specific as shown in Figure 2.
The safety of data encryption principal security data communication, inverse process are decryption.Encryption and decryption object is by encapsulation
Protocol packet data.In the present invention is implemented, byte stream is encrypted using symmetric encipherment algorithm, symmetric cryptography refers to encryption
Identical key is used with decryption.Encryption and decryption frame J CF based on java provides a series of tools for encrypting and decrypting,
Core classes are Cipher.It calls getInstance (" DES ") to generate Cipher object, this encryption equipment is specified to calculate using des encryption
Method also can specify the Encryption Algorithm such as AES/RSA of other modes;Then init () method is called to carry out Cipher object
Initialization, while encryption key and encryption mode (encryption or decryption) are set;DoFinal () is finally called to carry out actual add
Close or decryption, this depends on the encryption mode being arranged when initialization.If initialization pattern is encryption, doFinal is encrypted;
If the mode of initialization is decryption, it is decrypted, as shown in Figure 4.
In the embodiment of the present invention, in data exchange, after receiving compression decrypted data packet, needs to be decrypted, solve
Compression and resolving, wherein decrypting process is the inverse process of above-mentioned ciphering process, and decrypting process is according to above-mentioned ciphering process
It is determined, detailed process is known to those skilled in the art, and details are not described herein again.Decompression is the inverse process of compression, is used
Inflater class serves as decompression machine.The character string that will be decompressed first is converted into byte arrays, is then enter into decompression machine
In, it exports as original byte arrays, then original character string is reduced to by toString () method.It should be noted that
Be that in decompression, Compression Strategies and compression level must be consistent with the value being arranged when compression, while character turns byte and has to refer to
Fixed identical coding mode, as shown in Figure 3.
Data resolution module is the inverse operation to data package module, completes the parsing to protocol package.It parses protocol package
Data must construct JSONObject object first, then obtain its specific value or right by each field name in protocol package
As carrying out the business operation of next step accordingly.Such as when obtaining above-mentioned protocol package, JSONObject is used first
(String) JSONObject object is constructed, calling get (" OperationCode ") acquisition type codes are Login, are learnt to step on
Record authentication business operation;Then getJSONObject (" OperationData ") is called to obtain data needed for login authentication, root
Corresponding data is obtained according to user name password and geriatric nursing home coding.
If login authentication is out of joint, such as password or username bad, then above-mentioned OperationCode is corresponding
Value is LOGIN_ERROR, shows that log-on message is wrong.Other fields are sky, then according to JSON data encapsulation process, by this
Content returns to handheld terminal as logon error protocol package.Handheld terminal is parsed according to above-mentioned JSON data, is obtained and is logged in shape
State code is LOGIN_ERROR, then prompts handheld terminal user name or password mistake.
A data exchange process below by handheld terminal and endowment server end is come to the course of work of the invention
It is illustrated.Firstly, handheld terminal before the request is sent, needs the requirement according to JSON package module the business number of transmission
JSONObject is encapsulated as according to according to data pack protocol.Secondly packaged data are compressed by compression module, then
Compressed data are encrypted by encryption, finally the data by encryption output are sent.
It after endowment server receives request, first has to that it is decrypted, and using the data after decryption as solution
Then the input of pressure obtains the field of agreement package definition by JSON parsing module, and accordingly to determine whether needing to carry out industry
Business processing, the rear response that endowment server is sent to handheld terminal if desired then compressed and encrypted, otherwise current communication
Process terminates.