CN112711419A - Data comparison method and device - Google Patents
Data comparison method and device Download PDFInfo
- Publication number
- CN112711419A CN112711419A CN201911017739.2A CN201911017739A CN112711419A CN 112711419 A CN112711419 A CN 112711419A CN 201911017739 A CN201911017739 A CN 201911017739A CN 112711419 A CN112711419 A CN 112711419A
- Authority
- CN
- China
- Prior art keywords
- json
- key
- data
- key name
- json data
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 55
- 238000004590 computer program Methods 0.000 claims description 19
- 238000003491 array Methods 0.000 abstract description 4
- 238000006243 chemical reaction Methods 0.000 description 6
- 238000010586 diagram Methods 0.000 description 4
- 230000000694 effects Effects 0.000 description 2
- 230000003287 optical effect Effects 0.000 description 2
- 230000005540 biological transmission Effects 0.000 description 1
- 238000011161 development Methods 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 238000001914 filtration Methods 0.000 description 1
- 230000010365 information processing Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 238000012545 processing Methods 0.000 description 1
- 238000012360 testing method Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
Landscapes
- Engineering & Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Software Systems (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
The invention provides a data comparison method and a device, wherein the method comprises the following steps: under the condition that first JSON data to be compared is a JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different; determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared; and respectively comparing each object in the first JSON data with the corresponding target object. By the data comparison method provided by the invention, the comparison objects corresponding to the JSON data of the two JSON array types can be accurately determined, so that the accuracy of the comparison result of the unordered JSON arrays can be improved.
Description
Technical Field
The present invention relates to the field of information processing technologies, and in particular, to a data comparison method and apparatus.
Background
JSON (JSON Object Notation) is a lightweight data exchange format, and is commonly used in daily software development. The data in this format needs to be compared and checked frequently by developers and testers. There are currently some comparison tools, such as Beyond company, Diff, etc., that can do simple comparison work. However, for some more complex JSON data, for example, unordered JSON arrays (i.e., JSON array), there is no suitable comparison logic in the prior art, which results in poor accuracy of the comparison result of the unordered JSON arrays.
Disclosure of Invention
The embodiment of the invention provides a data comparison method and device, and aims to solve the problem that the accuracy of a comparison result of an unordered JSON array in the prior art is poor.
In order to solve the technical problem, the invention is realized as follows:
in a first aspect, an embodiment of the present invention provides a data comparison method. The method comprises the following steps:
under the condition that first JSON data to be compared is a JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared;
and respectively comparing each object in the first JSON data with the corresponding target object.
In a second aspect, an embodiment of the present invention further provides a data comparison apparatus. The data comparison device comprises:
the device comprises a first determining module, a second determining module and a judging module, wherein the first determining module is used for determining the unique key name of first JSON data under the condition that the first JSON data to be compared is a JSON array type; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
a second determining module, configured to determine, according to the unique key name, a target object corresponding to each object in the first JSON data; the target object is an object in second JSON data to be compared;
and the first comparison module is used for respectively comparing each object in the first JSON data with the corresponding target object.
In a third aspect, an embodiment of the present invention further provides a data comparison apparatus, including a processor, a memory, and a computer program stored in the memory and capable of running on the processor, where the computer program, when executed by the processor, implements the steps of the data comparison method.
In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the data comparison method are implemented.
In the embodiment of the invention, under the condition that the first JSON data to be compared is a JSON array type, the unique key name of the first JSON data is determined; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different; determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared; and respectively comparing each object in the first JSON data with the corresponding target object. Because the comparison objects corresponding to the JSON data of the two JSON array types can be accurately determined based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings needed to be used in the description of the embodiments of the present invention will be briefly introduced below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and it is obvious for those skilled in the art that other drawings can be obtained according to these drawings without inventive exercise.
FIG. 1 is a flowchart of a data comparison method according to an embodiment of the present invention;
FIG. 2 is a flowchart of a data comparison method according to another embodiment of the present invention;
FIG. 3 is a flowchart of a data comparison method according to another embodiment of the present invention;
FIG. 4 is a block diagram of a data comparison apparatus according to an embodiment of the present invention;
FIG. 5 is a block diagram of a data alignment apparatus according to another embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
The embodiment of the invention provides a data comparison method. Referring to fig. 1, fig. 1 is a flowchart of a data comparison method provided in an embodiment of the present invention, as shown in fig. 1, including the following steps:
In this embodiment, each object in the first JSON data includes the unique key name, and the key values corresponding to the unique key names of different objects in the first JSON data are different, so that different objects in the first JSON data can be identified by the unique key names.
For example, the first JSON data is as follows: [ { "key 1"; "v11", "key 2"; "v21", "key 3"; "v31" }, { "key 1"; v12"," key2 "; v21", "key 3"; v32"}, {" key1 "; v13", "key 2"; v21"," key3 "; v32" } ], since the key values of key1 of different objects in the first JSON data are different, different objects in the first JSON data can be identified by key1, so that the unique key name of the first JSON data can be key 1.
In this embodiment, the key value of the unique key name in each object in the first JSON data may be the same as the key value of the unique key name in the target object corresponding to the unique key name, that is, two objects having the same key value of the unique key name in the second JSON data and the first JSON data may be compared.
For example, the first JSON data is as follows: [ { "key1": "v11", "key2": "v21", "key3": "v31" }, { "key1": "v12", "key2": "v21", "key3": "v32" }, { "key1": "v13", "key2": "v21", "key3": "v32" }; the second JSON data is as follows: [ { "key1": "v12", "key2": "v21", "key3": "v31" }, { "key1": "v13", "key2": "v21", "key3": "v31" }, { "key1": "v11", "key2": "v22", "key3": "v31" }; two objects with the same key value of the key in the first JSON data and the second JSON data can be compared, that is, { "key": v "," key ": v", v ": v" } are compared, { "key": v "," key ": v" } are compared with { "key": v "," key ": v", "v": v "} are compared, and {" key ": v", "key": v ": can be compared.
It should be noted that each object in the first JSON data may be each array element in the first JSON data, and a target object corresponding to each object in the first JSON data may be each array element in the second JSON data; each object in the first JSON data may also be each JSON object obtained by converting the first JSON data, and a target object corresponding to each object in the first JSON data may also be each JSON object obtained by converting the second JSON data, which is not limited in this embodiment.
In the step, two objects which need to be compared in the first JSON data and the second JSON data can be determined more accurately through the unique key name.
And 103, comparing each object in the first JSON data with a corresponding target object.
For example, the key names of the objects in the first JSON data may be traversed, whether the same key name exists in the corresponding target object may be determined, and if yes, the key value corresponding to the key name in the object may be compared with the key value corresponding to the key name in the corresponding target object.
It should be noted that, if each object in the first JSON data is a JSON object, and a target object corresponding to each object in the first JSON data is also a JSON object, the comparison may be performed directly based on the granularity of the JSON object, or the JSON object may be further converted into a single-valued type object, and the comparison is performed based on the single-valued type object obtained through the conversion, which is not limited in this embodiment.
Optionally, if a key value of the JSON array type still exists in each object in the first JSON data, the key value of the JSON array type may be compared in the same manner as in steps 101 to 103, which is not described herein again.
It should be noted that the first JSON data may be input JSON data to be compared, or may be a key value in the input JSON data to be compared, and similarly, the second JSON data may be input JSON data to be compared, or may be a key value in the input JSON data to be compared, which is not limited in this embodiment.
In the embodiment of the invention, under the condition that the first JSON data to be compared is a JSON array type, the unique key name of the first JSON data is determined; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different; determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared; and respectively comparing each object in the first JSON data with the corresponding target object. Because the comparison objects corresponding to the JSON data of the two JSON array types can be accurately determined based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved.
Optionally, the step 102, that is, the determining the unique key name corresponding to the first JSON data, may include:
determining the unique key name of the first JSON data according to the key name in each object in the first JSON data and the corresponding key value; or
And receiving a unique key name of the first JSON data specified by a user.
In an embodiment, the key names and the corresponding key values in the objects in the first JSON data can be respectively obtained, and the key names which are included in the objects and have different corresponding key values in the objects are selected as unique key names, so that the flexibility of determining the unique key names corresponding to the JSON array can be improved.
In another embodiment, the unique key name corresponding to the JSON array may be specified by the user, for example, the user may pre-configure the unique key name corresponding to each JSON array included in the first JSON object before data comparison, which may improve the efficiency of data comparison.
Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and a key name corresponding to the first key value is the same as a key name corresponding to the second key value.
In this embodiment, the first key value and the second key value may be key values located at the same level and corresponding to the same key name in the first JSON object and the second JSON object, respectively. In practical cases, a JSON object may include at least one Key-Value pair, i.e., a Key name (i.e., Key) and a Key Value (i.e., Value), and the Key Value in each Key-Value pair may be a JSON array (i.e., JSON array), a JSON object (i.e., JSON object), and so on, so that there may be multiple levels in a JSON object, each level may include at least one node, and in the data comparison process, the Key values at the same level and corresponding to the same Key name in the comparison reference object and the compared object are usually compared.
Optionally, the first JSON object may be a JSON object obtained by converting a first JSON array (i.e., JSON array) to be compared, and the second JSON object may also be a JSON object obtained by converting a second JSON array to be compared, where the second JSON object may be a compared object of the first JSON object determined based on a unique key name corresponding to the first JSON array.
For example, in the case that both the input comparison reference object and the input object to be compared are JSON arrays, both the JSON array of the comparison reference object and the JSON array of the object to be compared may be converted into JSON objects, and two JSON objects to be compared in the comparison reference object and the object to be compared may be determined based on the unique key name corresponding to the JSON array of the comparison reference object.
Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a comparison object;
before determining the unique key name of the first JSON data under the condition that the first JSON data to be compared is a JSON array type, the method further comprises the following steps:
traversing each key name of the first JSON object, and judging whether a key name which is the same as the key name of the first JSON object exists in the second JSON object;
and if the key value exists, respectively acquiring a first key value in the first JSON object and a second key value in the second JSON object.
In this embodiment, a user may configure the first JSON object as a comparison reference object and the second JSON object as a compared object, and compared with the prior art in which an object with more elements in two JSON objects is used as a comparison reference object and an object with fewer elements is used as a compared object, the user can know the positions of the expected value and the actual value more clearly. In addition, because the comparison reference object can be configured by a user, the user can set elements or fields included by the comparison reference object according to actual requirements, so that fields which are not required to be compared can be filtered out conveniently, and compared with the field which is actively configured in the prior art, the convenience of field filtering operation and the efficiency of data comparison can be improved.
For example, a first JSON object may be designated in advance as a comparison reference object, a second JSON object may be designated as a compared object, and then the key name of the first JSON object may be traversed, and it may be determined whether a key name identical to the key name of the first JSON object exists in the second JSON object, and if the key name exists, key values corresponding to the identical key names in the first JSON object and the second JSON object may be acquired and compared, respectively.
Optionally, before the step 101, that is, before determining the unique key name of the first JSON data when the first JSON data to be compared is of a JSON array type, the method further includes:
acquiring a first JSON character string and a second JSON character string;
and respectively converting the first JSON character string and the second JSON character string into JSON object types to obtain the first JSON object and the second JSON object.
In this embodiment, when the object to be compared is a JSON string, the JSON string may be converted into a JSON object type and then compared.
Optionally, exception capture may be performed in the process of converting the first JSON character string and the second JSON character string into the JSON object type (for example, exception capture may be performed through try/catch), so as to determine whether the first JSON character string and the second JSON character string are successfully converted into the JSON object, that is, determine whether the first JSON character string and the second JSON character string are standard JSON character strings. Obtaining a first JSON object and a second JSON object under the condition that the first JSON character string and the second JSON character string are successfully converted, finishing the process under the condition that the first JSON character string and/or the second JSON character string are/is unsuccessfully converted,
in practical application, data of JSON character string types are often generated by a plurality of service interfaces, and the JSON character strings are converted into JSON object types and then compared, so that the convenience of JSON character string comparison can be improved.
Optionally, the method may further include:
comparing the first JSON data with the second JSON data under the condition that the data type of the first JSON data is a single-value type;
and if the first JSON data is different from the second JSON data, storing a target key name, the first JSON data and the second JSON data, wherein the target key name is a key name corresponding to the first JSON data or a key name corresponding to the second JSON data.
In this embodiment, when the data type of the first JSON data (i.e., the first key value) is a single-valued type, the first JSON data may be directly compared with the second JSON data (i.e., the second key value), and if the first JSON data and the second JSON data are different, the target key name, the first JSON data, and the second JSON data may be recorded, which is convenient for problem location.
Optionally, the method may further include:
under the condition that the data type of the first JSON data is a JSON object type, traversing each key name in the first JSON data, and judging whether the second JSON data has the key name same as the key name in the first JSON data;
if the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and a key value corresponding to the first key name;
if a second key name in the first JSON data exists in the second JSON data, respectively acquiring a third key value corresponding to the second key name in the first JSON data and a fourth key value corresponding to the second key name in the second JSON data;
and if the third key value is different from the fourth key value, storing the second key name, the third key value and the fourth key value.
In this embodiment, the first key name and the second key name may be any key name in the first JSON data (for example, the first key value). Specifically, for a key name that does not exist in the second JSON data (for example, the second key value) in the first JSON data, the key name and the key value corresponding to the key name may be stored; for a key name existing in the second JSON data in the first JSON data, a key value corresponding to the key name in the first JSON data and the second JSON data can be respectively obtained and compared, and if the key name and the key value are different, the key name and the key value corresponding to the key name in the first JSON data and the second JSON data can be stored, for example, the key name, the key value in the reference comparison object and the key value of the object to be compared can be stored in sequence; if both are the same, the storage may not be performed.
In this embodiment, each key name in the first JSON data is traversed for comparison, and compared with the prior art, the comparison is exited only by judging a mismatch item (for example, length mismatch), so that sufficient comparison of the JSON data can be realized, and comparison results of the JSON data are enriched. In addition, different key names and key values in the first JSON data and the second JSON data are recorded, so that problem location in the later period is facilitated.
Optionally, when the data type of the first JSON data is the JSON object type, the first JSON data may be converted into a single-value type, and then compared, and different contents in a comparison result may be recorded.
The following describes embodiments of the present invention with reference to examples:
referring to fig. 2, the data comparison method provided in the embodiment of the present invention may include the following steps:
The first JSON string may be a comparison reference object.
In this step, the first JSON object can be obtained when the first JSON string is successfully converted into the JSON object, otherwise, the process can be exited, and the result is returned.
And step 204, iteratively taking the key name of the first JSONObject.
In this step, each key name in the first jsonoobject may be traversed.
In this step, the second JSONObject is a compared object, and for each key name in the first JSONObject, it can be determined whether the second JSONObject includes the key name, and if so, step 206 is executed.
And step 206, key value is taken.
In this step, key values corresponding to the same key name are respectively taken from the first jsonoobject and the second jsonoobject, and split processing can be performed according to the type of the key value corresponding to the key name in the first jsonoobject.
In this step, if the key value corresponding to the key name in the first JSONObject is of the jsonoarray type, the unique key name corresponding to the key value may be determined, and the key value may be converted into the JSONObject type and then compared based on the unique key name of the key value, that is, the JSONObject objects in the compared objects corresponding to the JSONObject objects in the comparison reference object are determined and compared based on the determined unique key name.
And step 208, if the JSONObject type is adopted, converting the JSONObject type into a single-value type for comparison.
In this step, if the key value corresponding to the key name in the first jsonoobject is of the jsonoobject type, the key value corresponding to the key name in the first jsonoobject may be converted into a single-valued type for comparison.
It should be noted that, if the previous level of the key value is the jsonoarray type, the corresponding compared jsonoobject can be determined according to the unique key name corresponding to the previous level.
And step 209, if the type is a single-value type, directly comparing.
In this step, if the key value corresponding to the key name in the first jsonoobject is a single-valued type, the key value corresponding to the key name in the first jsonoobject and the key value corresponding to the key name in the second jsonoobject can be directly compared.
And step 210, caching different key names and key values after comparison.
For the comparison results obtained in the above steps 207 to 209, different key names and key values in the comparison results may be cached, for example, for the comparison results with different key values of the comparison reference object and the compared object, the key name, the key value of the comparison reference object, and the key value of the compared object may be cached; for a comparison result that does not include the key name of the comparison reference object in the compared object, the key name and the key value of the comparison reference object may be cached.
And step 211, acquiring a second JSON character string.
The second JSON string may be a compared object.
And step 212, converting the second JSON character string into a JSONObject object.
In this step, the second JSON object can be obtained when the second JSON string is successfully converted into the JSON object, otherwise, the process can be exited, and the result is returned.
Referring to fig. 3, the data comparison method provided in the embodiment of the present invention may include the following steps:
and 301, acquiring a first JSON character string.
The first JSON string may be a comparison reference object.
The target type may include a JSON object type or a JSON array type.
In this step, when the first JSON string is successfully converted into the JSON data of the target type, the first target JSON data of the target type, for example, the first JSON object, may be obtained, otherwise, the process may be exited, and a result may be returned.
Similarly, the embodiment may convert the second JSON string into the JSON data of the target type, and may obtain the first target JSON data of the target type, for example, the second JSON object, if the conversion of the second JSON string into the JSON data of the target type is successful, otherwise, the process may be exited, and a result may be returned. Wherein, the second JSON character string is a compared object.
It should be noted that, in this embodiment, the first JSON string may be specified as a comparison reference object, the second JSON string may be specified as a compared object, and the number of elements included in the first JSON string may be less than the number of elements included in the second JSON string.
In this step, it can be determined whether the input data is jsonoobject, if so, step 305 is performed, otherwise, step 310 is performed. For example, it is determined whether the first target JSON data obtained by converting the first JSON string or the key value in the acquired first target JSON data is jsonoobject, if so, step 305 is executed, otherwise, step 310 is executed.
It should be noted that, for the jsonoobject, the jsonoarray may be converted into a single-valued object before comparison.
Step 305, whether the current level has the key name which is not taken.
In this step, it may be determined whether the current level (e.g., the currently traversed level) of the input data has an un-fetched key name, if so, step 306 is executed, otherwise, step 307 may be executed.
For example, if the first target JSON data is jsonoobject, it may be determined whether the current level of the first target JSON data has a key name that is not fetched (e.g., not traversed); if the key value in the acquired first target JSON data is jsonoobject, it may be determined whether the current level of the key value has a key name that is not fetched (e.g., not traversed).
In practical application, the key names of the respective levels of the comparison reference object can be traversed, and whether the compared reference object comprises the corresponding key name in the same level can be judged.
In this step, key names that are not taken under the current hierarchy may be taken.
And 307, whether the key name which is not taken exists in the upper level.
In this step, it may be determined whether there is a key name that is not taken (if not traversed) in a previous level of the current level, if there is a key name that is not taken (if there is a key name that is not taken), step 306 is executed, otherwise, a cache result that uses the comparison reference object as a reference may be returned, where the cache result may include different key names and key values in the comparison result, for example, for the comparison result that the key values of the comparison reference object and the comparison object are different, the cache result may include the key name, the key value of the comparison reference object, and the key value of the comparison object; for a comparison result that does not include the key name of the comparison reference object in the compared object, the cached result may include the key name and the key value of the comparison reference object.
And 308, judging whether the key name is contained in the second target JSON data at the same level.
In this step, the second target JSON data is a compared object. If the second target JSON data contains the key name in the same hierarchy, step 309 is executed, otherwise, the comparison result (key name + key value) is added into the cache.
In this step, key values corresponding to the key name obtained in step 307 in the first target JSON data and the second target JSON data may be respectively obtained, and the type of the key value corresponding to the key name obtained in step 307 in the first target JSON data may be returned and determined.
In this step, it may be determined whether the input data is JSONArray, if so, step 311 is performed, otherwise, step 315 is performed. For example, it is determined whether the first target JSON data obtained by converting the first JSON character string or the key value in the acquired first target JSON data is JSONArray, if so, step 311 is executed, otherwise, step 315 is executed.
It should be noted that, for JSONArray, the JSONArray can be converted into JSONObject and then compared.
In this step, if the input data is JSONArray, the unique key name of the input data may be determined first, and used as a basis for acquiring the compared object before comparison.
Specifically, key names with all different key values of leaf nodes in all array elements (such as JSONObject) in the JSONArray can be automatically obtained and used as unique key names; a unique key name may also be specified by the user.
And step 312, iteratively taking the key name in the array element.
In this step, the key names of the respective data elements in the JSONArray of the comparison reference object may be iteratively taken.
And 313, judging whether the key name is contained in the second target JSON data in the same hierarchy based on the unique key name.
In this step, array elements in the second target JSON data in the same hierarchy corresponding to each data element in the JSONArray of the comparison reference object may be determined based on the unique key name determined in the step 311, and it may be determined whether the array elements in the second target JSON data in the same hierarchy include the key name obtained in the step 312. Specifically, if the array element in the second target JSON data at the same level includes the key name, step 314 is executed, otherwise, the comparison result (e.g., key name + key value) is added to the cache.
And step 314, key value fetching.
In this step, the key value corresponding to the key name obtained in step 312 in the two data elements to be compared may be obtained, and the type of the key value corresponding to the key name obtained in step 312 in the array element of the comparison reference object may be determined.
In this step, the other type object may be a single-value type object.
In this step, it may be determined whether the unique type object of the comparison reference object and the unique type object of the corresponding object to be compared are equal to each other. For example, if the key value of the comparison reference object obtained in step 309 is a single-value type object, the key value of the comparison reference object obtained in step 309 and the key value of the compared object may be compared. If not, the comparison result (e.g., key name + key value of the comparison reference object + key value of the compared object) may be added to the cache.
The data comparison method provided by the embodiment of the invention can locally compare disordered JSON character strings, is convenient for a user to mine unequal items of comparison objects, and is applied to development, debugging and test checking work. Positioning the items to be compared according to the reference object, so that a user can know the positions of the expected value and the actual value clearly; and the comparison reference object with the element number larger than, equal to or smaller than the element number of the compared object can be appointed, so that the user can flexibly manage the key name which does not need comparison and cannot be positioned. Compared with the prior art in which the comparison is carried out by depending on the self sequence iteration, the accuracy of the comparison result can be improved. In addition, key names and key values can be recorded for different parts in the dimension of a row, and quick problem location is facilitated.
Referring to fig. 4, fig. 4 is a structural diagram of a data comparison apparatus according to an embodiment of the present invention. As shown in fig. 4, the data alignment apparatus 400 includes:
a first determining module 401, configured to determine, when first JSON data to be compared is a JSON array type, a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
a second determining module 402, configured to determine, according to the unique key name, a target object corresponding to each object in the first JSON data; the target object is an object in second JSON data to be compared;
a first comparison module 403, configured to compare each object in the first JSON data with a corresponding target object.
Optionally, the first determining module is specifically configured to:
determining the unique key name of the first JSON data according to the key name in each object in the first JSON data and the corresponding key value; or
And receiving a unique key name of the first JSON data specified by a user.
Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and a key name corresponding to the first key value is the same as a key name corresponding to the second key value.
Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a comparison object;
the device further comprises:
the first traversal module is used for traversing each key name of the first JSON object before determining the unique key name of the first JSON data under the condition that the first JSON data to be compared is of a JSON array type, and judging whether the key name which is the same as the key name of the first JSON object exists in the second JSON object or not;
and the first obtaining module is used for respectively obtaining a first key value in the first JSON object and a second key value in the second JSON object if the first key value exists.
Optionally, the apparatus further comprises:
the second acquisition module is used for acquiring a first JSON character string and a second JSON character string before determining the unique key name of the first JSON data under the condition that the first JSON data to be compared is a JSON array type;
and the conversion module is used for converting the first JSON character string and the second JSON character string into JSON object types respectively to obtain the first JSON object and the second JSON object.
Optionally, the apparatus further comprises:
the second comparison module is used for comparing the first key value with the second key value under the condition that the data type of the first key value is a single-value type;
the first storage module is configured to store a target key name, the first key value, and the second key value if the first key value is different from the second key value, where the target key name is a key name corresponding to the first key value or a key name corresponding to the second key value.
Optionally, the apparatus further comprises:
the second traversal module is used for traversing each key name in the first JSON data under the condition that the data type of the first JSON data is a JSON object type, and judging whether the key name which is the same as the key name in the first JSON data exists in the second JSON data or not;
the second storage module is used for storing the first key name and a key value corresponding to the first key name if the first key name in the first JSON data does not exist in the second JSON data;
a third obtaining module, configured to, if a second key name in the first JSON data exists in the second JSON data, respectively obtain a third key value corresponding to the second key name in the first JSON data and a fourth key value corresponding to the second key name in the second JSON data;
and the third storage module is used for storing the second key name, the third key value and the fourth key value if the third key value is different from the fourth key value.
The data comparison apparatus 400 provided in the embodiment of the present invention can implement each process in the above method embodiments, and is not described herein again to avoid repetition.
In the data comparison device 400 of the embodiment of the present invention, the first determining module 401 is configured to determine, when first JSON data to be compared is a JSON array type, a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different; a second determining module 402, configured to determine, according to the unique key name, a target object corresponding to each object in the first JSON data; the target object is an object in second JSON data to be compared; a first comparison module 403, configured to compare each object in the first JSON data with a corresponding target object. Because the comparison objects corresponding to the JSON data of the two JSON array types can be accurately determined based on the unique key name, the accuracy of the comparison result of the unordered JSON array can be improved.
Referring to fig. 5, fig. 5 is a structural diagram of a data alignment apparatus according to another embodiment of the present invention, and as shown in fig. 5, the data alignment apparatus 500 includes: a processor 501, a memory 502 and a computer program stored on the memory 502 and executable on the processor, the various components in the data transmission device 500 being coupled together by a bus interface 503, the computer program realizing the following steps when executed by the processor 501:
under the condition that first JSON data to be compared is a JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared;
and respectively comparing each object in the first JSON data with the corresponding target object.
Optionally, the computer program, when executed by the processor 501, is further configured to:
determining the unique key name of the first JSON data according to the key name in each object in the first JSON data and the corresponding key value; or
And receiving a unique key name of the first JSON data specified by a user.
Optionally, the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and a key name corresponding to the first key value is the same as a key name corresponding to the second key value.
Optionally, the first JSON object is configured as a comparison reference object, and the second JSON object is configured as a comparison object;
the computer program, when executed by the processor 501, is further adapted to:
traversing each key name of the first JSON object before determining the unique key name of the first JSON data under the condition that the first JSON data to be compared is of a JSON array type, and judging whether the key name which is the same as the key name of the first JSON object exists in the second JSON object or not;
and if the key value exists, respectively acquiring a first key value in the first JSON object and a second key value in the second JSON object.
Optionally, the computer program, when executed by the processor 501, is further configured to:
under the condition that the first JSON data to be compared is a JSON array type, acquiring a first JSON character string and a second JSON character string before determining the unique key name of the first JSON data;
and respectively converting the first JSON character string and the second JSON character string into JSON object types to obtain the first JSON object and the second JSON object.
Optionally, the computer program, when executed by the processor 501, is further configured to:
comparing the first key value with the second key value under the condition that the data type of the first key value is a single-value type;
and if the first key value is different from the second key value, storing a target key name, the first key value and the second key value, wherein the target key name is a key name corresponding to the first key value or a key name corresponding to the second key value.
Optionally, the computer program, when executed by the processor 501, is further configured to:
under the condition that the data type of the first JSON data is a JSON object type, traversing each key name in the first JSON data, and judging whether the second JSON data has the key name same as the key name in the first JSON data;
if the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and a key value corresponding to the first key name;
if a second key name in the first JSON data exists in the second JSON data, respectively acquiring a third key value corresponding to the second key name in the first JSON data and a fourth key value corresponding to the second key name in the second JSON data;
and if the third key value is different from the fourth key value, storing the second key name, the third key value and the fourth key value.
The embodiment of the present invention further provides a data comparison apparatus, which includes a processor, a memory, and a computer program stored in the memory and capable of running on the processor, and when being executed by the processor, the computer program implements each process of the data comparison method embodiment, and can achieve the same technical effect, and is not described herein again to avoid repetition.
The embodiment of the present invention further provides a computer-readable storage medium, where a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the computer program implements each process of the data comparison method embodiment, and can achieve the same technical effect, and in order to avoid repetition, details are not repeated here. The computer-readable storage medium may be a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk.
It should be noted that, in this document, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
Through the above description of the embodiments, those skilled in the art will clearly understand that the method of the above embodiments can be implemented by software plus a necessary general hardware platform, and certainly can also be implemented by hardware, but in many cases, the former is a better implementation manner. Based on such understanding, the technical solutions of the present invention may be embodied in the form of a software product, which is stored in a storage medium (such as ROM/RAM, magnetic disk, optical disk) and includes instructions for enabling a terminal (such as a mobile phone, a computer, a server, an air conditioner, or a network device) to execute the method according to the embodiments of the present invention.
While the present invention has been described with reference to the embodiments shown in the drawings, the present invention is not limited to the embodiments, which are illustrative and not restrictive, and it will be apparent to those skilled in the art that various changes and modifications can be made therein without departing from the spirit and scope of the invention as defined in the appended claims.
Claims (10)
1. A method of data alignment, comprising:
under the condition that first JSON data to be compared is a JSON array type, determining a unique key name of the first JSON data; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
determining a target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in second JSON data to be compared;
and respectively comparing each object in the first JSON data with the corresponding target object.
2. The method of claim 1, wherein the determining the unique key name of the first JSON data comprises:
determining the unique key name of the first JSON data according to the key name in each object in the first JSON data and the corresponding key value; or
And receiving a unique key name of the first JSON data specified by a user.
3. The method of claim 1, wherein the first JSON data is a first key value in a first JSON object to be compared, the second JSON data is a second key value in a second JSON object to be compared, and a key name corresponding to the first key value is the same as a key name corresponding to the second key value.
4. The method of claim 3, wherein the first JSON object is configured as a comparison reference object and the second JSON object is configured as a comparison object;
before determining the unique key name of the first JSON data under the condition that the first JSON data to be compared is a JSON array type, the method further comprises the following steps:
traversing each key name of the first JSON object, and judging whether a key name which is the same as the key name of the first JSON object exists in the second JSON object;
and if the key value exists, respectively acquiring a first key value in the first JSON object and a second key value in the second JSON object.
5. The method according to claim 3, wherein before determining the unique key name of the first JSON data to be compared in the case that the first JSON data is of a JSON array type, the method further comprises:
acquiring a first JSON character string and a second JSON character string;
and respectively converting the first JSON character string and the second JSON character string into JSON object types to obtain the first JSON object and the second JSON object.
6. The method of claim 3, further comprising:
comparing the first key value with the second key value under the condition that the data type of the first key value is a single-value type;
and if the first key value is different from the second key value, storing a target key name, the first key value and the second key value, wherein the target key name is a key name corresponding to the first key value or a key name corresponding to the second key value.
7. The method of claim 1, further comprising:
under the condition that the data type of the first JSON data is a JSON object type, traversing each key name in the first JSON data, and judging whether the second JSON data has the key name same as the key name in the first JSON data;
if the first key name in the first JSON data does not exist in the second JSON data, storing the first key name and a key value corresponding to the first key name;
if a second key name in the first JSON data exists in the second JSON data, respectively acquiring a third key value corresponding to the second key name in the first JSON data and a fourth key value corresponding to the second key name in the second JSON data;
and if the third key value is different from the fourth key value, storing the second key name, the third key value and the fourth key value.
8. A data alignment apparatus, comprising:
the device comprises a first determining module, a second determining module and a judging module, wherein the first determining module is used for determining the unique key name of first JSON data under the condition that the first JSON data to be compared is a JSON array type; each object in the first JSON data comprises the unique key name, and key values corresponding to the unique key names of different objects in the first JSON data are different;
a second determining module, configured to determine, according to the unique key name, a target object corresponding to each object in the first JSON data; the target object is an object in second JSON data to be compared;
and the first comparison module is used for respectively comparing each object in the first JSON data with the corresponding target object.
9. A data alignment apparatus comprising a processor, a memory and a computer program stored on the memory and executable on the processor, the computer program when executed by the processor implementing the steps of the data alignment method of any one of claims 1 to 7.
10. A computer-readable storage medium, having stored thereon a computer program which, when being executed by a processor, carries out the steps of the data alignment method according to any one of claims 1 to 7.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911017739.2A CN112711419A (en) | 2019-10-24 | 2019-10-24 | Data comparison method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911017739.2A CN112711419A (en) | 2019-10-24 | 2019-10-24 | Data comparison method and device |
Publications (1)
Publication Number | Publication Date |
---|---|
CN112711419A true CN112711419A (en) | 2021-04-27 |
Family
ID=75541615
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911017739.2A Pending CN112711419A (en) | 2019-10-24 | 2019-10-24 | Data comparison method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112711419A (en) |
Cited By (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113806310A (en) * | 2021-09-13 | 2021-12-17 | 上海商汤科技开发有限公司 | Comparison method and related product |
CN117370398A (en) * | 2023-11-15 | 2024-01-09 | 航天信息(广东)有限公司 | Comparison method and device for changing data |
Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111506608A (en) * | 2020-04-16 | 2020-08-07 | 泰康保险集团股份有限公司 | Method and device for comparing structured texts |
-
2019
- 2019-10-24 CN CN201911017739.2A patent/CN112711419A/en active Pending
Patent Citations (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN111506608A (en) * | 2020-04-16 | 2020-08-07 | 泰康保险集团股份有限公司 | Method and device for comparing structured texts |
Non-Patent Citations (2)
Title |
---|
BLACK白先森: "关于两个json比较 是否相等", pages 1 - 4, Retrieved from the Internet <URL:https://www.jianshu.com/p/9b1288043280> * |
CERMAK-PETR: "act-json-compare", pages 1 - 10, Retrieved from the Internet <URL:https://github.com/cermak-petr/act-json-compare/tree/master> * |
Cited By (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113806310A (en) * | 2021-09-13 | 2021-12-17 | 上海商汤科技开发有限公司 | Comparison method and related product |
CN113806310B (en) * | 2021-09-13 | 2024-08-09 | 上海商汤科技开发有限公司 | Contrast method and related products |
CN117370398A (en) * | 2023-11-15 | 2024-01-09 | 航天信息(广东)有限公司 | Comparison method and device for changing data |
CN117370398B (en) * | 2023-11-15 | 2024-05-28 | 航天信息(广东)有限公司 | Comparison method and device for changing data |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN110427331B (en) | Method for automatically generating performance test script based on interface test tool | |
US9356966B2 (en) | System and method to provide management of test data at various lifecycle stages | |
US8407340B2 (en) | Page load performance analysis | |
CN108430051B (en) | Service opening method and equipment | |
CN110990447B (en) | Data exploration method, device, equipment and storage medium | |
CN110059003B (en) | Automatic test method, device, electronic equipment and readable storage medium | |
CN102665231A (en) | Method of automatically generating parameter configuration file for LTE (Long Term Evolution) system | |
CN112711419A (en) | Data comparison method and device | |
CN112711438A (en) | Dependent component information extraction method, dependent component information extraction device, and computer-readable storage medium | |
CN109308258A (en) | Building method, device, computer equipment and the storage medium of test data | |
CN111475402B (en) | Program function testing method and related device | |
CN110532774A (en) | Hook inspection method, device, server and readable storage medium storing program for executing | |
CN113190220A (en) | JSON file differentiation comparison method and device | |
CN108268468A (en) | The analysis method and system of a kind of big data | |
CN113568604B (en) | Method and device for updating wind control strategy and computer readable storage medium | |
CN112860581B (en) | Execution method, device, equipment and storage medium of test case | |
US9026988B2 (en) | Code conversion method, apparatus, storage medium and request remaining time response method | |
CN108733543A (en) | A kind of method, apparatus of log analysis, electronic equipment and readable storage medium storing program for executing | |
WO2021262283A1 (en) | Reduced processing loads via selective validation specifications | |
CN113641742A (en) | Data extraction method, device, equipment and storage medium | |
CN112434831A (en) | Troubleshooting method and device, storage medium and computer equipment | |
CN110147313A (en) | A kind of log-output method and device | |
CN111026629A (en) | Method and device for automatically generating test script | |
CN115291762A (en) | Service item triggering method and device, storage medium and computer equipment | |
CN113992664B (en) | Cluster communication method, related device and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination |