Movatterモバイル変換


[0]ホーム

URL:


CN112711419B - Data comparison method and device - Google Patents

Data comparison method and device
Download PDF

Info

Publication number
CN112711419B
CN112711419BCN201911017739.2ACN201911017739ACN112711419BCN 112711419 BCN112711419 BCN 112711419BCN 201911017739 ACN201911017739 ACN 201911017739ACN 112711419 BCN112711419 BCN 112711419B
Authority
CN
China
Prior art keywords
json
key
data
json data
key name
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.)
Active
Application number
CN201911017739.2A
Other languages
Chinese (zh)
Other versions
CN112711419A (en
Inventor
王之文
尚清刚
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Mashang Consumer Finance Co Ltd
Original Assignee
Mashang Consumer Finance Co Ltd
Priority date (The priority date 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 date listed.)
Filing date
Publication date
Application filed by Mashang Consumer Finance Co LtdfiledCriticalMashang Consumer Finance Co Ltd
Priority to CN201911017739.2ApriorityCriticalpatent/CN112711419B/en
Publication of CN112711419ApublicationCriticalpatent/CN112711419A/en
Application grantedgrantedCritical
Publication of CN112711419BpublicationCriticalpatent/CN112711419B/en
Activelegal-statusCriticalCurrent
Anticipated expirationlegal-statusCritical

Links

Classifications

Landscapes

Abstract

Translated fromChinese

本发明提供一种数据比对方法及装置,该方法包括:在待比对的第一JSON数据为JSON数组类型的情况下,确定所述第一JSON数据的唯一键名;所述第一JSON数据中每个对象均包括所述唯一键名,且所述第一JSON数据中不同对象的所述唯一键名对应的键值不同;根据所述唯一键名确定所述第一JSON数据中各个对象对应的目标对象;所述目标对象为待比对的第二JSON数据中的对象;分别将所述第一JSON数据中各个对象和其对应的目标对象进行比对。通过本发明提供的数据比对方法,可以较为准确的确定两个JSON数组类型的JSON数据对应的比较对象,从而可以提高无序的JSON数组的比对结果的准确性。

The present invention provides a data comparison method and device, the method comprising: when the first JSON data to be compared is of JSON array type, determining the unique key name of the first JSON data; 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; determining the target object corresponding to each object in the first JSON data according to the unique key name; the target object is an object in the second JSON data to be compared; and comparing each object in the first JSON data with its corresponding target object respectively. Through the data comparison method provided by the present invention, the comparison objects corresponding to the JSON data of two JSON array types can be determined more accurately, thereby improving the accuracy of the comparison results of unordered JSON arrays.

Description

Data comparison method and device
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 (JavaScript Object Notation, JS object profile)) is a lightweight data exchange format, and is commonly applied in daily software development. Whether it is a developer or a tester, it is necessary to frequently compare and verify such data. There are some contrast tools such as Beyond computer, diff, etc. that can do simple contrast work. However, for some more complex JSON data, for example, an unordered JSON array (i.e., JSONArray), there is no suitable comparison logic in the prior art, which results in poor accuracy of the comparison result of the unordered JSON array.
Disclosure of Invention
The embodiment of the invention provides a data comparison method and device, which are used for solving the problem of poor accuracy of a comparison result of disordered JSON arrays in the prior art.
In order to solve the technical problems, 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:
Determining a unique key name of first JSON data under the condition that the first JSON data to be compared is of a JSON array type, wherein 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 target objects corresponding to all objects in the first JSON data according to the unique key names, wherein the target objects are objects 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, the embodiment of the invention further provides a data comparison device. The data comparison device comprises:
The first determining module is used for determining a 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, wherein 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;
The second determining module is used for determining target objects corresponding to all objects in the first JSON data according to the unique key names, wherein the target objects are objects 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 comparing device, 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 comparing method described above.
In a fourth aspect, embodiments of the present invention further provide a computer readable storage medium having a computer program stored thereon, the computer program implementing the steps of the data comparison method described above when executed by a processor.
In the embodiment of the invention, under the condition that the first JSON data to be compared is of a JSON array type, determining the unique key name of the first JSON data, wherein each object in the first JSON data comprises the unique key name, key values corresponding to the unique key names of different objects in the first JSON data are different, determining the target object corresponding to each object in the first JSON data according to the unique key name, wherein the target object is an object in the second JSON data to be compared, and 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 names, the accuracy of the comparison result of the unordered JSON arrays can be improved.
Drawings
In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings that are needed in the description of the embodiments of the present invention will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present invention, and other drawings may be obtained according to these drawings without inventive effort to a person of ordinary skill in the art.
FIG. 1 is a flow chart of a data comparison method provided by an embodiment of the invention;
FIG. 2 is a flow chart of a data alignment method according to another embodiment of the present invention;
FIG. 3 is a flow chart of a data alignment method according to another embodiment of the present invention;
FIG. 4 is a block diagram of a data comparison device 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 following description of the embodiments of the present invention will be made clearly and fully with reference to the accompanying drawings, in which it is evident that the embodiments described are some, but not all embodiments of the invention. All other embodiments, which can be made by those skilled in the art based on the embodiments of the invention without making any inventive effort, are intended to be within the scope of the 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 by an embodiment of the present invention, as shown in fig. 1, including the following steps:
And step 101, determining a 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, wherein 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.
In this embodiment, since each object in the first JSON data includes the unique key name, and the key values corresponding to the unique key names of the different objects in the first JSON data are different, the different objects in the first JSON data can be identified by the unique key names.
For example, since key1 key values of different objects in the first JSON data are all different as shown in :[{"key1":"v11","key2":"v21","key3":"v31"},{"key1":"v12","key2":"v21","key3":"v32"},{"key1":"v13","key2":"v21","key3":"v32"}], below, different objects in the first JSON data can be identified by key1, and thus the unique key name of the first JSON data can be key1.
And 102, determining target objects corresponding to the objects in the first JSON data according to the unique key names, wherein the target objects are objects in the second JSON data to be compared.
In this embodiment, the key value of the unique key name in each object in the first JSON data and the key value of the unique key name in the corresponding target object may be the same, that is, two objects with 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, such as shown below :[{"key1":"v11","key2":"v21","key3":"v31"},{"key1":"v12","key2":"v21","key3":"v32"},{"key1":"v13","key2":"v21","key3":"v32"}];, and the second JSON data, such as shown below :[{"key1":"v12","key2":"v21","key3":"v31"},{"key1":"v13","key2":"v21","key3":"v31"},{"key1":"v11","key2":"v22","key3":"v31"}];, may then compare two objects in the first JSON data and the second JSON data for which key values of key1 are the same, i.e., may compare { "key1": "v11", "key2": "v21", "key3": "v31" } with { "key1": "v11", "key2": "v22", "key3": "v31" }, and { "key1": "v12", "key2": "v21", "key3": "v32" } with { "key1": "v12", "key2": "v21", "key3": "v31" }, and { "key1": "v13", "key2": "v21", "key3": "v 32": "" and { key1":" v13 ":" v31 ":" and } "key1": "v 31".
It should be noted that, each object in the first JSON data may be each array element in the first JSON data, each 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, each object corresponding to each object in the first JSON data may also be each JSON object obtained by converting the second JSON data, and this embodiment is not limited thereto.
In the step, two objects to be compared in the first JSON data and the second JSON data can be accurately determined through the unique key name.
And step 103, respectively comparing each object in the first JSON data with the corresponding target object.
For example, the key names of the objects in the first JSON data may be traversed respectively, and it is determined whether the same key name exists in the corresponding target object, and if so, the key value corresponding to the key name in the object is compared with the key value corresponding to the key name in the corresponding target object.
Note that, if each object in the first JSON data is a JSON object, the target object corresponding to each object in the first JSON data is also a JSON object, and the comparison may be directly performed based on the granularity of the JSON object, or the comparison may be performed by further converting the JSON object into a single-value type object and comparing based on the single-value type object obtained by the conversion, which is not limited in this embodiment.
Alternatively, if the 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.
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 of a JSON array type, determining the unique key name of the first JSON data, wherein each object in the first JSON data comprises the unique key name, key values corresponding to the unique key names of different objects in the first JSON data are different, determining the target object corresponding to each object in the first JSON data according to the unique key name, wherein the target object is an object in the second JSON data to be compared, and 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 names, the accuracy of the comparison result of the unordered JSON arrays can be improved.
Optionally, step 102, that is, 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 names and the corresponding key values in each object in the first JSON data, or
And receiving a unique key name of the first JSON data specified by a user.
In an embodiment, key names and corresponding key values in each object in the first JSON data can be obtained respectively, and key names which are included in each object and have different corresponding key values in each object and are different in key value can be selected from the key names as unique key names, so that flexibility of determining the unique key names corresponding to the JSON arrays 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 the data comparison, so that the efficiency of the data comparison may be improved.
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 under the same hierarchy and corresponding to the same key name in the first JSON object and the second JSON object, respectively. In practice, 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., JSONArray), a JSON object (i.e., JSONObject), and so on, so that there may be multiple levels of a JSON object, each level may include at least one node, and in the process of data comparison, the Key values in the same level and corresponding to the same Key name in the reference object and the object to be compared are typically compared.
Optionally, the first JSON object may be a JSON object obtained by converting a first JSON array to be compared (i.e. JSONArray), 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 where the input comparison reference object and the object to be compared are both JSON arrays, it is possible to convert both the JSON arrays of the comparison reference object and the JSON arrays of the object to be compared into JSON objects, and determine two JSON objects to be compared among the comparison reference object and the object to be compared based on unique key names corresponding to the JSON arrays 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 compared object;
the method further comprises the steps of, 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:
traversing each key name of the first JSON object, 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 so, respectively acquiring a first key value in the first JSON object and a second key value in the second JSON object.
In this embodiment, the user may configure the first JSON object as a comparison reference object and configure the second JSON object as a compared object, so that compared with the prior art that objects with more elements in the two JSON objects are used as comparison reference objects and objects with fewer elements are used as compared objects, the user can more clearly know the positions of the expected value and the actual value. In addition, as the comparison reference object can be configured by the user, the user can set the elements or fields included in the comparison reference object according to the actual demands, so that some fields which are not required to be compared can be filtered more conveniently, and compared with the prior art, the method has the advantages that the filtering fields are actively configured, and the convenience of field filtering operation and the data comparison efficiency 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 a compared object, and then a key name of the first JSON object may be traversed, and whether the second JSON object has the same key name as the first JSON object may be determined, and if so, key values corresponding to the same key names in the first JSON object and the second JSON object may be obtained and compared, respectively.
Optionally, before the step 101, that is, before determining the unique key name of the first JSON data in the case where 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 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.
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, in the process of converting the first JSON string and the second JSON string into JSON object types, exception capturing (for example, exception capturing through try/catch) may be performed to determine whether the first JSON string and the second JSON string are successfully converted into JSON objects, that is, determine whether the first JSON string and the second JSON string are standard JSON strings. In case the conversion of the first JSON string and the second JSON string is successful, a first JSON object and the second JSON object may be obtained, in case the conversion of the first JSON string and/or the second JSON string fails, the flow may be ended,
In practical application, data of JSON character string types are often generated by a plurality of service interfaces, and the JSON character string comparison convenience can be improved by converting the JSON character string into the JSON object type and then comparing the JSON character string.
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 value type, the first JSON data and the second JSON data (i.e., the second key value) may be directly compared, and if the two are different, the target key name, the first JSON data and the second JSON data may be recorded, so that the positioning problem is facilitated.
Optionally, the method may further include:
Traversing each key name in the first JSON data and judging whether the second JSON data has the same key name as the first JSON data or not under the condition that the data type of the first JSON data is a JSON object type;
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, the key name and the key value corresponding to the key name may be stored for the key name not existing in the second JSON data (for example, the second key value) in the first JSON data, the key value corresponding to the key name in the first JSON data and the key value corresponding to the key name in the second JSON data may be obtained and compared respectively for the key name existing in the first JSON data and the key name existing in the second JSON data, if the key name and the key value corresponding to the key name in the first JSON data and the key name in the second JSON data are different, for example, the key name, the key value in the reference comparison object and the key value of the compared object may be stored in order, and if the key name and the key value are the same, the key name and the key value corresponding to the key name in the first JSON data and the key value in the reference comparison object may not be stored.
Compared with the prior art, when only one unmatched item (for example, length unmatched item) is judged, the embodiment exits the comparison, so that the comparison of the sufficient quantity of the JSON data can be realized, and the comparison result of the JSON data is enriched. In addition, different key names and key values in the first JSON data and the second JSON data are recorded, so that problem positioning is facilitated in the later stage.
Optionally, when the data type of the first JSON data is a JSON object type, the first JSON data may be converted into a single value type and then compared, and different contents in the comparison result may be recorded.
Embodiments of the present invention are described below with reference to examples:
Referring to fig. 2, the data comparison method provided by the embodiment of the present invention may include the following steps:
step 201, acquiring a first JSON string.
The first JSON string may be a comparison reference object.
Step 202, converting the first JSON string into JSONObject objects.
Step 203, whether the conversion is successful.
In this step, if the conversion of the first JSON string into the JSONObject object is successful, the first JSONObject may be obtained, otherwise, the flow may be exited, and the result may be returned.
Step 204, iterating to obtain the key name of the first JSONObject.
In this step, each key name in the first JSONObject may be traversed.
Step 205, determine whether the second JSONObject contains the key name.
In this step, the second JSONObject is a compared object, and for each key name in the first JSONObject, it may be determined whether the second JSONObject includes the key name, and if so, step 206 is performed.
Step 206, taking the key value.
In this step, the key values corresponding to the same key name are taken from the first JSONObject and the second JSONObject, respectively, and the splitting process may be performed according to the type of the key value corresponding to the key name in the first JSONObject.
Step 207, if the type is JSONArray, converting to JSONObject type comparison.
In this step, if the key name corresponding to the key name in the first JSONObject is JSONArray type, the unique key name corresponding to the key name may be determined, and after the key name is converted into JSONObject type, the unique key names of the key values may be compared, that is, the JSONObject objects in the compared objects corresponding to the JSONObject objects in the comparison reference object are determined based on the determined unique key names.
Step 208, if the type is JSONObject, converting into a single-value type comparison.
In this step, if the key value corresponding to the key name in the first JSONObject is JSONObject type, the key value corresponding to the key name in the first JSONObject may be converted into a single value type for comparison.
It should be noted that, if the previous level of the key value is JSONArray types, the corresponding compared JSONObject may be determined according to the unique key name corresponding to the previous level.
Step 209, if the type is single value type, directly comparing.
In this step, if the key value corresponding to the key name in the first JSONObject is of a single value type, the key value corresponding to the key name in the first JSONObject may be directly compared with the key value corresponding to the key name in the second JSONObject.
Step 210, different key names and key values after comparison are cached.
For the comparison results obtained in steps 207 to 209, different key names and key values in the comparison results may be cached, for example, for the comparison results of different key values of the comparison reference object and the comparison target, the key names, the key values of the comparison reference object and the key values of the comparison target may be cached, and for the comparison results of the comparison target not including the key names of the comparison reference object, the key names and the key values of the comparison reference object may be cached.
Step 211, obtaining a second JSON string.
The second JSON string may be a compared object.
Step 212, converting the second JSON string into JSONObject objects.
Step 213, whether the conversion is successful.
In this step, if the second JSON string is successfully converted into JSONObject objects, the second JSONObject may be obtained, otherwise, the process may be exited, and the result may be returned.
Referring to fig. 3, the data comparison method provided by the embodiment of the present invention may include the following steps:
step 301, obtaining a first JSON string.
The first JSON string may be a comparison reference object.
Step 302, converting the first JSON string into JSON data of the target type.
The object type may include a JSON object type or a JSON array type, etc.
Step 303, whether the conversion is successful.
In this step, under the condition that the conversion of the first JSON string into the JSON data of the target type is successful, the first target JSON data of the target type, for example, the first JSONObject, may be obtained, otherwise, the flow may be exited, and the result may be returned.
Similarly, the embodiment can convert the second JSON string into the JSON data of the target type, and can obtain the first target JSON data of the target type, for example, the second JSONObject, if the second JSON string is successfully converted into the JSON data of the target type, otherwise, the process can be exited, and the result is returned. Wherein the second JSON string is the object to be compared.
It should be noted that, in this embodiment, the first JSON string may be designated as a comparison reference object, the second JSON string is a compared object, and the number of elements included in the first JSON string may be smaller than the number of elements included in the second JSON string.
Step 304, whether it is JSONObject.
In this step, it may be determined whether the input data is JSONObject, if yes, step 305 is performed, otherwise step 310 is performed. For example, it is determined whether the key value in the first target JSON data obtained by converting the first JSON string or the obtained first target JSON data is JSONObject, if yes, step 305 is executed, otherwise step 310 is executed.
Note that for JSONObject, JSONArray may be converted into a single-value type object before comparison.
Step 305, whether the current hierarchy has an un-fetched key name.
In this step, it may be determined whether the current level of the input data (e.g., the level currently traversed) has an unread key name, if so, step 306 is performed, otherwise step 307 may be performed.
For example, if the first target JSON data is JSONObject, 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), and if the obtained key value in the first target JSON data is JSONObject, 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 each level of the comparison reference object can be traversed, and whether the compared reference object comprises the corresponding key name under the same level or not is judged.
Step 306, taking the key name.
In this step, key names that are not fetched under the current hierarchy may be fetched.
Step 307, whether the previous hierarchy has an un-fetched key name.
In this step, it may be determined whether there is an unremoved (e.g., not traversed) key name in the previous level of the current level, if so, step 306 may be performed, otherwise, a cache result with 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 a comparison result with different key values of the comparison reference object and the compared object, the cache result may include the key name, the key value of the comparison reference object, and the key value of the compared object, and for a comparison result with no key name of the comparison reference object included in the compared object, the cache result may include the key name and the key value of the comparison reference object.
Step 308, determining whether the second target JSON data contains the key name under the same hierarchy.
In this step, the second target JSON data is the object to be compared. 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 to the cache.
Step 309, taking a key value.
In this step, the key values corresponding to the key names 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 determined in a return manner.
Step 310, JSONArray.
In this step, it may be determined whether the input data is JSONArray, if yes, step 311 is performed, and if not, step 315 is performed. For example, it is determined whether the key value in the first target JSON data obtained by converting the first JSON string or the obtained first target JSON data is JSONArray, if yes, step 311 is executed, otherwise step 315 is executed.
Note that for JSONArray, JSONArray may be converted to JSONObject and then aligned.
Step 311, the unique key name is confirmed.
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 obtaining the compared object before the comparison.
Specifically, key names with all different key values of leaf nodes in all array elements (such as JSONObject) in JSONArray can be automatically obtained and used as unique key names, and the unique key names can be designated by a user.
Step 312, iterating to obtain key names in the array elements.
In this step, key names of the individual data elements in JSONArray of the comparison reference objects may be iterated.
Step 313, judging whether the second target JSON data contains the key name under the same hierarchy based on the unique key name.
In this step, it may be determined, based on the unique key name determined in the above step 311, whether the array element in the second target JSON data under the same level corresponding to each data element in the JSONArray of the comparison reference object includes the key name taken in the above step 312. Specifically, if the array element in the second target JSON data under the same hierarchy includes the key name, step 314 is executed, otherwise, the comparison result (e.g., key name+key value) is added to the cache.
Step 314, taking the key value.
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 respectively 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 returned to be judged.
Step 315, whether it is another type of object.
In this step, the other type object may be a single value type object.
Step 316, whether or not they are equal.
In this step, it may be determined whether or not the single-value type object of the comparison reference object and the single-value type object in the corresponding compared object are equal. 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 may be compared with the key value of the object to be compared. If not, the comparison result (such as a key name, a key value of a comparison reference object and a key value of a compared object) can be added into the cache.
The data comparison method provided by the embodiment of the invention can be used for locally comparing unordered JSON character strings, is convenient for users to mine unequal items of comparison objects, and is applied to development, debugging and test and verification work. And the comparison reference object comprising elements with the number greater than, equal to or less than that of the compared object elements can be designated, so that the user can conveniently and flexibly manage key names which do not need to be compared and cannot be positioned. For unordered JSONArray comparison, the compared object can be accurately obtained, and compared with the prior art which relies on self-sequential iteration for comparison, the accuracy of the comparison result can be improved. In addition, key names and key values can be recorded in the dimension of rows for different parts, so that quick problem positioning is facilitated.
Referring to fig. 4, fig. 4 is a block diagram of a data comparison device according to an embodiment of the present invention. As shown in fig. 4, the data comparison apparatus 400 includes:
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, where each object in the first JSON data includes 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, where the target object is an object in the second JSON data to be compared;
And a first comparison module 403, configured to compare each object in the first JSON data with its 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 names and the corresponding key values in each object in the first JSON data, 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 compared object;
the apparatus further comprises:
The first traversing 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 acquisition module is used for respectively acquiring 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 includes:
the second obtaining module is configured to obtain a first JSON string and a second JSON string before determining a unique key name of the first JSON data in the case where the first JSON data to be compared is of a JSON array type;
And the conversion module is used for 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 apparatus further includes:
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;
and the first storage module is used for storing 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, wherein the target key name is the key name corresponding to the first key value or the key name corresponding to the second key value.
Optionally, the apparatus further includes:
The second traversing module is used for traversing each key name in the first JSON data 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 under the condition that the data type of the first JSON data is a JSON object type;
the second storage module is used for storing the first key name and the 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 device 400 provided in the embodiment of the present invention can implement each process in the above method embodiment, and in order to avoid repetition, a description is omitted here.
The data comparison device 400 of the embodiment of the invention is used for determining a unique key name of first JSON data under the condition that the first JSON data to be compared is of a JSON array type, wherein each object in the first JSON data comprises the unique key name, key values corresponding to the unique key names of different objects in the first JSON data are different, a second determination module 402 is used for 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 a first comparison module 403 is used for 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 names, the accuracy of the comparison result of the unordered JSON arrays can be improved.
Referring to fig. 5, fig. 5 is a block diagram of a data alignment apparatus according to another embodiment of the present invention, and as shown in fig. 5, a data alignment apparatus 500 includes a processor 501, a memory 502, and a computer program stored in the memory 502 and capable of running on the processor, where each component in the data transmission apparatus 500 is coupled together through a bus interface 503, and when executed by the processor 501, the computer program implements the following steps:
Determining a unique key name of first JSON data under the condition that the first JSON data to be compared is of a JSON array type, wherein 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 target objects corresponding to all objects in the first JSON data according to the unique key names, wherein the target objects are objects 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 names and the corresponding key values in each object in the first JSON data, 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 compared object;
The computer program, when executed by the processor 501, is further adapted to:
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, traversing each key name of the first JSON object, and judging whether the key name identical to the key name of the first JSON object exists in the second JSON object;
and if so, 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 of a JSON array type, before determining the unique key name of the first JSON data, acquiring a first JSON character string and a second JSON character string;
And 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 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 the key name corresponding to the first key value or the key name corresponding to the second key value.
Optionally, the computer program when executed by the processor 501 is further configured to:
Traversing each key name in the first JSON data and judging whether the second JSON data has the same key name as the first JSON data or not under the condition that the data type of the first JSON data is a JSON object type;
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 invention also provides a data comparison device, which comprises a processor, a memory and a computer program stored in the memory and capable of running on the processor, wherein the computer program realizes the processes of the data comparison method embodiment when being executed by the processor, and can achieve the same technical effect, and the repetition is avoided, so that the description is omitted.
The embodiment of the invention also provides a computer readable storage medium, on which a computer program is stored, which when executed by a processor, implements the respective processes of the above data comparison method embodiment, and can achieve the same technical effects, so that repetition is avoided, and no further description is provided herein. The computer readable storage medium is, for example, a Read-Only Memory (ROM), a random access Memory (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 one does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
From the above description of the embodiments, it will be clear to those skilled in the art that the above-described embodiment method may be implemented by means of software plus a necessary general hardware platform, but of course may also be implemented by means of hardware, but in many cases the former is a preferred embodiment. Based on such understanding, the technical solution of the present invention may be embodied essentially or in a part contributing to the prior art in the form of a software product stored in a storage medium (e.g. ROM/RAM, magnetic disk, optical disk) comprising instructions for causing a terminal (which may be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to perform the method according to the embodiments of the present invention.
The embodiments of the present invention have been described above with reference to the accompanying drawings, but the present invention is not limited to the above-described embodiments, which are merely illustrative and not restrictive, and many forms may be made by those having ordinary skill in the art without departing from the spirit of the present invention and the scope of the claims, which are to be protected by the present invention.

Claims (10)

CN201911017739.2A2019-10-242019-10-24 Data comparison method and deviceActiveCN112711419B (en)

Priority Applications (1)

Application NumberPriority DateFiling DateTitle
CN201911017739.2ACN112711419B (en)2019-10-242019-10-24 Data comparison method and device

Applications Claiming Priority (1)

Application NumberPriority DateFiling DateTitle
CN201911017739.2ACN112711419B (en)2019-10-242019-10-24 Data comparison method and device

Publications (2)

Publication NumberPublication Date
CN112711419A CN112711419A (en)2021-04-27
CN112711419Btrue CN112711419B (en)2025-02-14

Family

ID=75541615

Family Applications (1)

Application NumberTitlePriority DateFiling Date
CN201911017739.2AActiveCN112711419B (en)2019-10-242019-10-24 Data comparison method and device

Country Status (1)

CountryLink
CN (1)CN112711419B (en)

Families Citing this family (2)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN113806310B (en)*2021-09-132024-08-09上海商汤科技开发有限公司Contrast method and related products
CN117370398B (en)*2023-11-152024-05-28航天信息(广东)有限公司Comparison method and device for changing data

Citations (1)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN111506608A (en)*2020-04-162020-08-07泰康保险集团股份有限公司Method and device for comparing structured texts

Patent Citations (1)

* Cited by examiner, † Cited by third party
Publication numberPriority datePublication dateAssigneeTitle
CN111506608A (en)*2020-04-162020-08-07泰康保险集团股份有限公司Method and device for comparing structured texts

Also Published As

Publication numberPublication date
CN112711419A (en)2021-04-27

Similar Documents

PublicationPublication DateTitle
CN108595157B (en)Block chain data processing method, device, equipment and storage medium
US8359297B2 (en)Multiple source data management using a conflict rule
US9400962B2 (en)Architecture for distributed, parallel crawling of interactive client-server applications
US20200065160A1 (en)Automated api evaluation based on api parameter resolution
US10216608B1 (en)Load testing with automated service dependency discovery
US9026988B2 (en)Code conversion method, apparatus, storage medium and request remaining time response method
US20120109931A1 (en)Technique for compression of state information in the crawling of interactive client-server applications
CN110059003B (en)Automatic test method, device, electronic equipment and readable storage medium
EP3974960B1 (en)Method and system for automated testing of web service apis
CN111475402B (en)Program function testing method and related device
CN112711419B (en) Data comparison method and device
CN117992295A (en)Service test data construction method and device
CN108733543B (en)Log analysis method and device, electronic equipment and readable storage medium
CN112287039A (en)Group partner identification method and related device
JP6523799B2 (en) Information analysis system, information analysis method
Cheng et al.An efficient FPRAS type group testing procedure to approximate the number of defectives
US20140337069A1 (en)Deriving business transactions from web logs
Nidd et al.Automated business application discovery
CN112434831A (en)Troubleshooting method and device, storage medium and computer equipment
CN113342647A (en)Test data generation method and device
CN112311679B (en) State detection method, device, electronic device and readable storage medium
CN111884932B (en)Link determining method, device, equipment and computer readable storage medium
CN113992664B (en)Cluster communication method, related device and storage medium
CN117493213B (en)Method, device, equipment and medium for detecting test coverage rate of financial business system
CN113965489B (en)Link timeout detection method, device, computer equipment and storage medium

Legal Events

DateCodeTitleDescription
PB01Publication
PB01Publication
SE01Entry into force of request for substantive examination
SE01Entry into force of request for substantive examination
GR01Patent grant
GR01Patent grant

[8]ページ先頭

©2009-2025 Movatter.jp