Json data analysis method, computer equipment and storage medium
Through the preliminary analysis of JSON data and the path rule extraction method, the problem of redundancy and inefficiency in JSON data processing is solved, and the target fields in JSON data are quickly positioned and extracted, which is suitable for complex data structures and improves development efficiency and accuracy.
Patent Information
- Application Number
- CN202411751504.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-02
- Publication Date
- 2024-12-31
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
When dealing with large or complex JSON data structures, developers need to write a large amount of code for layer-by-layer traversal analysis, resulting in redundant, inefficient code, and difficulty in quickly locate and extract specific fields.
By obtaining the JSON data to be parsed for preliminary analysis, the input parsing path is obtained, and the parsing results are extracted based on the path and target JSON data, the target fields are accurately positioned and extracted using preset path rules, supporting the indication of object level and array element position. , suitable for various complex data structures.
It improves the efficiency and accuracy of JSON data processing, reduces code complexity and redundancy, simplifies the analysis process, adapts to the needs of different data structures, and enhances the flexibility and adaptability of the method.
Smart Images

Figure CN119225737A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of big data technology, and in particular to a Json data analysis, computer device and storage medium. Background Art
[0002] In today's information technology era, backend project development is the foundation for building modern network applications and services. In these projects, the JSON (JavaScript Object Notation) data format has become one of the mainstream formats for data exchange because of its lightweight, easy to read and write, and easy to machine parse and generate. In daily development work, developers frequently deal with JSON data, whether it is reading configuration files, processing response data of API interfaces, or serializing database records, JSON plays an indispensable role.
[0003] However, when dealing with large or complex JSON data structures, developers usually need to write a lot of code to traverse and parse the data layer by layer until they reach the target field. This layer-by-layer parsing method is particularly inefficient and cumbersome when faced with deeply nested JSON structures. For example, when developers only care about a specific field in a deep structure, they have to write a series of loops and conditional statements, which not only increases the amount of code but also reduces the readability and maintainability of the code. In addition, this traditional layer-by-layer parsing method is not ideal in terms of efficiency. For large JSON data, layer-by-layer traversal consumes a lot of computing resources, resulting in slow parsing, which is unacceptable in performance-sensitive application scenarios. At the same time, this method cannot intuitively reflect the data structure path that developers want to obtain, making it difficult to quickly locate and extract specific fields. Summary of the invention
[0004] An object of an embodiment of the present invention is to provide a Json data parser, a computer device and a storage medium, which are used to solve the technical problems of code redundancy and low efficiency that occur in existing JSON data processing technologies when dealing with deep path field acquisition.
[0005] In a first aspect, an embodiment of the present invention provides a method for parsing Json data, the method comprising: Get the Json data to be parsed; Perform a preliminary analysis on the Json data to be analyzed to obtain target Json data that can be recognized by a computer; The input parsing path is obtained, and the parsing result is extracted based on the parsing path and the target Json data. The parsing path is used to indicate the location of the parsing result.
[0006] In a possible example, the parsing result is obtained based on the parsing path and the target Json data, and the parsing path is used to indicate the location of the parsing result, including: parsing the parsing path according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path; extracting the corresponding parsing result from the target Json data based on the object hierarchy and / or array and array element position contained in the parsing path.
[0007] It can be seen that in this embodiment, the preset path rules can accurately locate and extract the user-specified fields in the Json data, even if these fields are located in a deeply nested structure, thereby improving the efficiency and accuracy of data processing and reducing the complexity and redundancy of the code.
[0008] In a possible example, the parsing path includes a first identifier indicating an object level, and / or a second identifier indicating an array and a third identifier indicating a position of an array element.
[0009] It can be seen that in this embodiment, multiple identifiers are used to provide an intuitive way to specify data locations. Correct use and processing of identifiers is the key to achieving efficient JSON data parsing.
[0010] In one possible example, the parsing path is parsed according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path, including: determining whether a first identifier exists in the parsing path; if so, segmenting the parsing path based on the first identifier to obtain multiple hierarchies; determining whether a second identifier exists in each hierarchical level; if so, it indicates that the value of the current hierarchical level is an array; and determining the array element position based on a third identifier associated with the second identifier.
[0011] It can be seen that in this embodiment, the preset path rules can accurately parse the complex Json structure and extract the data required by the user.
[0012] In a possible example, the preset path rule is as follows: when the object hierarchy contains multiple levels, adjacent levels are separated by a first identifier; an array is indicated by a second identifier, and a position of an element in the array is indicated by a third identifier.
[0013] It can be seen that in this embodiment, the preset path rules can accurately parse the path and extract the corresponding value from the Json data.
[0014] In a possible example, the extracting corresponding parsing results from the target Json data based on the object hierarchy and / or array and array element positions contained in the parsing path includes: determining the data structure type to be parsed based on the object hierarchy and / or array and array element positions contained in the parsing path, the data structure types including: array structure, object structure, nested object structure and mixed structure; determining parsing rules based on the data structure type, and extracting corresponding parsing results from the target Json data based on the parsing rules and the object hierarchy and / or array and array element positions contained in the parsing path.
[0015] It can be seen that in this embodiment, the information required by the user can be accurately extracted from the Json data according to the parsing path and the data structure type, whether it is a simple key-value pair, an array element, or data nested in a multi-layer structure.
[0016] In one possible example, when the target Json data is parsed according to the object hierarchy and / or array and array element position contained in the parsing path, if it is found that the parsing path does not exist, a prompt of a path error is returned, and the prompt includes the type of error.
[0017] It can be seen that in this embodiment, not only can the occurrence of errors be pointed out according to the error mechanism, but also the user can be helped to quickly locate the problem, thereby improving the accuracy of data analysis and user experience.
[0018] In a possible example, the method further includes: when it is detected that the parsing path or the target Json data is empty, returning corresponding prompt information.
[0019] It can be seen that the prompt information in this embodiment can help users identify input errors and guide them on how to use the parsing tool correctly. The error handling mechanism further improves the robustness of the method and ensures that users can obtain clear feedback during use, thereby improving the reliability of the parsing process and user experience.
[0020] In a second aspect, an embodiment of the present invention provides a Json data parsing device, the device comprising: The acquisition unit is used to obtain the Json data to be parsed; A parsing unit, used for preliminarily parsing the Json data to be parsed to obtain target Json data recognizable by a computer; The extraction unit is used to obtain an input parsing path, and extract a parsing result based on the parsing path and the target Json data, wherein the parsing path is used to indicate the location of the parsing result.
[0021] In a third aspect, an embodiment of the present invention provides a computer device, including: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to the first aspect.
[0022] In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor executes the method as described in the first aspect.
[0023] In the solution implemented by the above-mentioned Json data parsing, computer equipment and storage medium, the method first obtains the Json data to be parsed; secondly, the Json data to be parsed is preliminarily parsed to obtain the target Json data that can be recognized by the computer; finally, the input parsing path is obtained, and the parsing result is extracted based on the parsing path and the target Json data, and the parsing path is used to indicate the location of the parsing result. The method first obtains the Json data to be parsed and performs a preliminary parsing, so as to quickly obtain the target Json data that can be recognized by the computer, thereby improving the efficiency of data processing; using the input parsing path, the target field in the Json data can be quickly located and extracted, simplifying the parsing process of the Json data, avoiding traversing the entire Json data structure layer by layer, and reducing the complexity and redundancy of the code; further, the parsing path can indicate the position of the object hierarchy and array elements, and is suitable for various complex data structures, including nested objects and mixed structures, enhancing the flexibility and adaptability of the method, so it can adapt to different parsing requirements, whether it is a simple key-value pair or a complex nested structure, it can obtain data by adjusting the parsing path, improving development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.
[0025] Figure 1 It is a flowchart of a Json data parsing method in one embodiment of the present invention; Figure 2It is a structural schematic diagram of a Json data parsing device in one embodiment of the present invention; Figure 3 It is a schematic diagram of the structure of a computer device in one embodiment of the present invention. DETAILED DESCRIPTION
[0026] In order to make the purpose, technical scheme and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention.
[0027] It should be noted that, if there is no conflict, the various features in the embodiments of the present invention can be combined with each other, all within the scope of protection of the present invention. In addition, although the functional modules are divided in the device schematic diagram and the logical order is shown in the flow chart, in some cases, the steps shown or described can be performed in a different order from the module division in the device or the flow chart. Furthermore, the words "first", "second", "third", etc. used in the present invention do not limit the data and execution order, but only distinguish the same items or similar items with basically the same functions and effects.
[0028] The technical solution of the present application can be applied to various electrode detachment detection scenarios.
[0029] In view of this, this application proposes a Json data parsing method to solve the above problems, which is introduced in detail below.
[0030] See also Figure 1 , Figure 1 A flow chart of a Json data parsing method provided by an embodiment of the present invention, the method comprises the following steps: S10. Get the Json data to be parsed.
[0031] Among them, Json (JavaScript Object Notation) is a lightweight data exchange format that is easy to read, write and parse. It is widely used in scenarios such as front-end and back-end data transmission, configuration files, and API responses.
[0032] Among them, obtaining the Json data to be parsed refers to extracting the Json format string or object that needs to be processed from the data source.
[0033] Specifically, the Json data to be parsed can come from a variety of different data sources, which are not limited here. For example: response data obtained from the network API interface; Json file content read from the file system; Json format records queried from the database; Json objects already existing in the memory; Json data entered by the user through the front-end interface, which needs to be parsed and processed in the back-end, etc.
[0034] Therefore, the Json data to be parsed can be obtained in the following ways: by sending HTTP requests (such as GET, POST, etc.) to obtain the Json data returned by the server; reading Json format files from local or remote file systems; the Json data queried from the database needs to be parsed for further processing; the Json messages received from the message queue (such as Kafka, RabbitMQ, etc.) need to be parsed to process business logic.
[0035] Among them, Json data usually exists in two forms: string form, that is, Json data exists in the form of a text string, which needs to be converted into a data structure that can be operated by the program through a parser; object form. In some programming languages, such as JavaScript, Json data can be directly parsed into objects (for example, using the JSON.parse method), so that it can be directly accessed in the program.
[0036] Among them, after obtaining the Json data, it also includes: ensuring that the obtained data is in a valid Json format and has no syntax errors; handling possible character encoding problems to ensure that the data does not appear garbled during the parsing process; verifying the reliability of the data source to prevent potential security risks, such as Json injection attacks, etc., which are not the only restrictions here.
[0037] It can be seen that this embodiment ensures that correct, secure and well-formatted Json data is obtained, laying a solid foundation for subsequent parsing steps.
[0038] S20, preliminarily parsing the Json data to be parsed to obtain target Json data that can be recognized by a computer.
[0039] Among them, the preliminary parsing of the Json data to be parsed refers to converting the original Json string into a data structure that can be operated by a computer program, such as an object, array, key-value pair, etc. This preliminary parsing usually involves parsing the Json string into data types in a programming language (such as dictionaries and lists in Python, objects and arrays in JavaScript, etc.).
[0040] Specifically, preliminary parsing usually includes the following steps: lexical analysis, decomposing the Json string into a series of tokens, which represent the basic components of the Json data structure, such as curly brackets, square brackets, commas, colons, strings, numbers, Boolean values, and null; syntax analysis: according to the grammatical rules of Json, these tokens are organized into meaningful structures, such as objects ({}), arrays ([]), and key-value pairs (key:value).
[0041] Among them, after preliminary parsing, Json data is converted into the following computer-recognizable data structures: objects. In many programming languages, Json objects are converted into corresponding dictionaries, hash tables or objects; arrays. Json arrays are converted into arrays or lists in programming languages; basic data types. Strings, numbers, Boolean values and nulls in Json are converted into corresponding types in programming languages.
[0042] Among them, preliminary parsing usually relies on specialized parsing libraries or functions. For example, in JavaScript, you can use the JSON.parse() method; in Python, you can use the loads() function of the json module; in Java, you can use the org.json library or other third-party libraries such as Jackson or Gson.
[0043] Furthermore, during the initial parsing process, if malformed Json data is encountered, the parser will usually throw an exception or error. These errors need to be caught and handled appropriately to avoid program crashes.
[0044] The target Json data refers to the Json data that has been parsed into the internal data structure of the programming language, which can be directly operated and processed by the computer. The target Json data is usually structured data, including objects, arrays, nested objects, etc., which can be easily accessed and operated. The target Json data can be added, deleted, modified, searched, traversed, filtered, etc. through various operation methods provided by the programming language, which provides convenience for subsequent data processing and analysis.
[0045] It can be seen that in this embodiment, by converting the original Json string into a data structure that can be easily operated by the program, a foundation is laid for subsequent path-based accurate data extraction.
[0046] S30. Obtain an input parsing path, and extract a parsing result based on the parsing path and the target Json data. The parsing path is used to indicate the location of the parsing result.
[0047] The parsing path is an expression used to indicate the location of the target data in the Json structure. It is usually a hierarchical path separated by dots (.) or slashes ( / ). For example, user.name means accessing the name field in the user object. The parsing path is designed to provide an intuitive and fast way to locate and extract specific data in the Json data structure, which allows developers to directly access deeply nested data without writing complex traversal code.
[0048] The parsing path can be obtained through user input, configuration files, API parameters, etc. Users can specify a specific path as needed to extract specific fields in the Json data, and there is no unique restriction here.
[0049] For example, the parsing path is decomposed into multiple steps or components, starting from the root, and accessing the corresponding object key or array index according to each step; if all steps successfully find the corresponding field, they will eventually reach the location specified by the parsing path, at which the data of the target field is extracted to obtain the parsing result.
[0050] Optionally, if a field cannot be found at some point during the parsing process (for example, the key does not exist or the array index is out of bounds), an error message should be returned to inform the user that the parsed path is invalid or part of the path does not exist.
[0051] The parsed result may be of different data types, such as strings, numbers, Boolean values, objects, or arrays.
[0052] It can be seen that in this embodiment, by parsing the path, the data of interest to the user can be accurately located in a complex Json structure, thereby avoiding the need to traverse the entire Json structure, thereby improving the flexibility and efficiency of data processing.
[0053] This method first obtains the Json data to be parsed and performs preliminary parsing, so as to quickly obtain the target Json data that can be recognized by the computer, thereby improving the efficiency of data processing; using the input parsing path, the target field in the Json data can be quickly located and extracted, which simplifies the parsing process of the Json data, avoids traversing the entire Json data structure layer by layer, and reduces the complexity and redundancy of the code; further, the parsing path can indicate the position of the object hierarchy and array elements, and is suitable for various complex data structures, including nested objects and mixed structures, which enhances the flexibility and adaptability of the method, so that it can adapt to different parsing requirements. Whether it is a simple key-value pair or a complex nested structure, data can be obtained by adjusting the parsing path, thereby improving development efficiency.
[0054] In one embodiment, the parsing result is obtained based on the parsing path and the target Json data, and the parsing path is used to indicate the location of the parsing result, including: parsing the parsing path according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path; extracting the corresponding parsing result from the target Json data based on the object hierarchy and / or array and array element position contained in the parsing path.
[0055] Among them, the preset path rules are used to guide how to parse the input parsing path so as to accurately locate the specific fields or elements in the target JSON data, that is, to locate the specific values in the data structure. The preset path rules are applicable to object hierarchies and array structures. The specific rules are as follows: Object hierarchy path, each level in the object hierarchy is separated by ".". For example, if the data is a nested object structure, the hierarchical relationship between the upper level and the lower level is connected by "."; Array path: For array structures, "[]" is used in the path to represent the array subscript, and the content in "[]" can be filled with the value to be obtained or the specific location of the object.
[0056] For example, by resolving the object hierarchy in the path, you can access the fields in the nested objects one level at a time. For example, user.address.city means accessing the address object in the user object, and then accessing the city field in the address object.
[0057] Among them, in the process of extracting the parsing results based on the object hierarchy and / or array and array element positions contained in the parsing path, according to the steps in the parsing path, starting from the root of the target Json data, the object keys or array indexes are accessed step by step until the final position specified by the parsing path is reached; at the final position, the corresponding data value is extracted, which is the parsing result required by the user.
[0058] It can be seen that in this embodiment, the preset path rules can accurately locate and extract the user-specified fields in the Json data, even if these fields are located in a deeply nested structure, thereby improving the efficiency and accuracy of data processing and reducing the complexity and redundancy of the code.
[0059] In one embodiment, the parsing path includes a first identifier indicating an object level, and / or a second identifier indicating an array and a third identifier indicating a position of an array element.
[0060] The first identifier is used to indicate the object level, usually represented by a dot (.). For example, in the path data[0].userId, the dot (.) is used to access the fields of the nested object, such as the userId field in the data object.
[0061] The second identifier is used to indicate an array, usually represented by square brackets ([]). For example, in the path data[0].userId, ([]) is used to access array elements, such as the first element in the data array.
[0062] The third identifier is used to indicate the position of the array element, which is usually the index value of the array. For example, in the path data[0].userId, 0 means accessing the first element in the data array.
[0063] For example, given a set of Json data and a parsing path: data: {"data":[{"userId":"001"}]}; path: data[0].userId. Parse according to the above identifier rules: the first identifier (.): indicates the object level, accesses the userId field in the data object; the second identifier ([]): indicates the array, accesses the element in the data array; the third identifier (0): indicates the position of the array element, accesses the first element in the data array, therefore, by parsing the path data[0].userId, the value "001" can be extracted from the Json data.
[0064] Optionally, identifiers can be used in combination to access complex data structures nested in arrays and objects. For example, data[0].userId not only accesses the array, but also accesses the properties of the objects in the array. Identifiers should be designed to be flexible enough to adapt to possible changes in data structures. For example, they should be able to handle multi-dimensional arrays and deeply nested objects.
[0065] It is important to note that if the identifier in the path refers to a non-existent level or the array index is out of range, a clear error message should be provided. For example, if there are no elements in the data array, trying to access data[0] should return an error.
[0066] It can be seen that in this embodiment, multiple identifiers are used to provide an intuitive way to specify data locations. Correct use and processing of identifiers are the key to achieving efficient Json data parsing.
[0067] In one embodiment, the parsing path is parsed according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path, including: judging whether there is a first identifier in the parsing path, if so, segmenting the parsing path based on the first identifier to obtain multiple hierarchies, judging whether there is a second identifier in each hierarchical level, if so, it indicates that the value of the current hierarchical level is an array, and determining the array element position according to a third identifier associated with the second identifier.
[0068] Specifically, in the parsing path, the first identifier is used to separate different levels of the Json object. Therefore, this solution first checks whether the parsing path contains the first identifier; if the first identifier exists, it will be used to split the parsing path to obtain a list of levels. Each level represents a key name in the Json object. For example, if the parsing path is "ab[0].c", the list of levels obtained after segmentation will be ["a", "b[0]", "c"]; after segmentation, each level will be traversed to prepare for further data extraction.
[0069] Specifically, when traversing each level, check whether the second identifier exists, which indicates that the current level is an array; if the second identifier exists, a specific element needs to be extracted from the array. The number in the second identifier (i.e., the third identifier) indicates the position of the element to be accessed in the array. For example, in the level "b[0]", the second identifier is "[]", and the third identifier is "0", which tells you to extract the first element in the array "b".
[0070] For example, assuming the parsing path is "data[0].userId", first split the path to get ["data[0]", "userId"]; in the first level "data[0]", identify the second identifier "[]" and the third identifier "0", which means that "data" is an array and the first element of the array needs to be extracted; in the second level "userId", there is no second identifier, so it is known that this is an object key name, and it will extract the value corresponding to the "userId" key from this object.
[0071] It can be seen that in this embodiment, the preset path rules can accurately parse the complex Json structure and extract the data required by the user. In one embodiment, the preset path rule is as follows: when the object hierarchy contains multiple levels, adjacent levels are separated by a first identifier; an array is indicated by a second identifier, and an element position in the array is indicated by a third identifier.
[0072] Specifically, when a Json data structure contains multiple levels, each level represents a deeper data node. The first identifier (usually a dot ".") is used to separate different object levels in the parsing path. For example, if a Json object has multiple nested levels, such as {"a": {"b": {"c": "value"}}}, then to access the innermost value, the path will be "abc", where each dot "." is the first identifier, used to separate adjacent levels.
[0073] Specifically, the second identifier (usually square brackets "[]") is used to indicate that the current level is an array. When the second identifier is encountered in the path, data needs to be extracted from the array rather than from the object.
[0074] Specifically, the third identifier is a number within the second identifier that indicates the position of the element to be accessed in the array. For example, in the path "ab[2].c", the second identifier "[]" indicates that "b" is an array, and the third identifier "2" indicates that the third element in the array needs to be accessed (note that array indexes usually start at 0).
[0075] It can be seen that in this embodiment, the preset path rules can accurately parse the path and extract the corresponding value from the Json data.
[0076] In one embodiment, the extracting corresponding parsing results from the target Json data based on the object hierarchy and / or array and array element positions contained in the parsing path includes: determining the data structure type to be parsed based on the object hierarchy and / or array and array element positions contained in the parsing path, the data structure types including: array structure, object structure, nested object structure and mixed structure; determining parsing rules based on the data structure type, and extracting corresponding parsing results from the target Json data based on the parsing rules and the object hierarchy and / or array and array element positions contained in the parsing path.
[0077] The data structure types include: array structure, object structure, nested object structure and mixed structure.
[0078] Specifically, the array structure is indicated by the second identifier ([]), and the third identifier contains the position of the array element. For example, the path data[0] indicates that data is an array, and 0 represents the first element in the array. For arrays, the element position is specified according to the subscript in the path. If the array is nested in an object, the object is parsed first, and then the array element is obtained according to the path. For example: { "users": [ {"id": "001"}, {"id": "002"} ]}; the path: "users[1].id" will get the "id" field value of the second object in the array, that is, "002".
[0079] Specifically, the object structure is when the parsed path contains only the first identifier (.), indicating that the data is a simple key-value pair object.
[0080] Specifically, a nested object structure refers to an object that contains other objects or arrays, for example {"user": {"userId": "001", "profile": {"name": "Alice"}}}.
[0081] Specifically, a mixed structure is when the parsing path contains both the first identifier and the second identifier, indicating that the data structure contains both objects and arrays, that is, objects may be nested in arrays, or arrays may be nested in objects. For example, {"orders": [{"product": {"name": "Phone", "price": 699}},{"product": {"name": "Laptop", "price": 1299}}]}; the path: "orders[1].product.name" will get the value of the "name" field of the "product" object in the second order, "Laptop".
[0082] The parsing rules define how to traverse and access elements in the Json data structure according to the instructions of the path.
[0083] For example, based on the data structure type, the corresponding parsing rules are determined: the parsing rule for the array structure is to use the second identifier ([]) and the third identifier (array index) to access array elements; the parsing rule for the object structure is to use the first identifier (.) to access object fields; the parsing rule for the nested object structure is to use multiple first identifiers (.) in sequence to access the fields of the nested object; the parsing rule for the mixed structure is to use the first identifier (.) and the second identifier ([]) in combination to access nested objects and array elements.
[0084] For example, given a set of Json data and parsing path: data: {"data":[{"userId":"001"}]}; path: data[0].userId, parse according to the above data structure type and parsing rules. The path data[0].userId contains array structure and object structure, which is a mixed structure; use the second identifier ([]) and the third identifier (array index) to access array elements; use the first identifier (.) to access object fields; use (.) to split the path into two levels: data[0] and userId; for the level data[0], there is a second identifier ([]), indicating that data is an array, and 0 means accessing the first element in the data array; for the level userId, there is no second identifier, indicating that userId is an object field; by parsing the path data[0].userId, the value "001" can be extracted from the Json data.
[0085] It can be seen that in this embodiment, the information required by the user can be accurately extracted from the Json data according to the parsing path and the data structure type, whether it is a simple key-value pair, an array element, or data nested in a multi-layer structure.
[0086] In one embodiment, the method further includes: when the target Json data is parsed according to the object hierarchy and / or array and array element position contained in the parsing path, if it is found that the parsing path does not exist, a prompt of a path error is returned, and the prompt includes the type of error.
[0087] The situation where the parsed path does not exist can be that when trying to access Json data according to the parsed path, if a certain level or array element cannot be found in the data, the path does not exist. This may be due to the key name error in the path, the array index is out of range, the path depth exceeds the actual data structure depth, etc. If the parsed path is found to be non-existent during the parsing process, a prompt of the path error is returned, and the prompt should include the type of error so that the user can understand the specific problem.
[0088] The error types may include but are not limited to "path does not exist", "array index out of bounds" or "data type does not match".
[0089] Optionally, when the object level does not exist, that is, when an object level in the parsing path does not exist, a prompt is returned: "Path error: object level 'xxx' does not exist"; when the array index is out of bounds, that is, when the array index in the parsing path exceeds the array length, a prompt is returned: "Path error: array index 'xxx' is out of bounds"; when the path format is wrong, that is, when the format of the parsing path is incorrect, a prompt is returned: "Path error: the parsing path format is incorrect".
[0090] For example, if the specified path does not exist in the data, a default error message or preset value will be returned. For example, if a level in the path does not exist: data: {"user": {"id": "001"}}; path: "user.name" (the "name" attribute does not exist), an error message "Error: Path 'user.name' does not exist" will be returned, and the error log will be recorded for subsequent analysis. The error log can include the error type, timestamp, affected data, etc., to facilitate problem tracking and analysis.
[0091] For example, if the array index specified in the path exceeds the actual range of the array, an index out of bounds error will be triggered. For example: data: {"users": [{"id": "001"}]}; path: "users[1].id" (the array has only one element, and the element with index 1 does not exist), an index out of bounds exception "Error: Array index out ofbounds at 'users[1]'" will be thrown, and a null value or default value will be returned.
[0092] For example, if the data type expected by the path does not match the actual type, a type mismatch error will be returned. For example: data: {"user": "001"}; path: "user.id" ("user" is a string, not an object), so a type mismatch error "Error: Expected object but found string at 'user'" is returned.
[0093] It can be seen that in this embodiment, not only can the occurrence of errors be pointed out according to the error mechanism, but also the user can be helped to quickly locate the problem, thereby improving the accuracy of data analysis and user experience.
[0094] In one embodiment, the method further includes: when it is detected that the parsing path or the target Json data is empty, returning corresponding prompt information.
[0095] Among them, the parsing path is empty means that the user does not provide any path information, or the provided path is an empty string. In this case, it is impossible to know which part of the Json data the user wants to access, so effective parsing operations cannot be performed. When it is detected that the parsing path is empty, a prompt message should be returned to inform the user that a valid parsing path needs to be provided. For example, if the parsing path is empty, the prompt message is returned: "Path error: parsing path is empty".
[0096] The target Json data being empty may mean that the data itself is an empty object ({}) or an empty array ([]), or that no Json data is provided at all. If the target Json data is empty, no data can be extracted from it even if the parsing path is valid. When it is detected that the target Json data is empty, a prompt message is returned to inform the user that there is no data available for parsing. For example, if the target Json data is empty, the prompt message is returned: "Data error: target Json data is empty".
[0097] For example, when the input data is empty or null, it detects and returns a null value or prompt. For example: data: null; path: "user.id"; detects null data and returns "Error: Input data is null".
[0098] Or, if the path is empty or undefined, an empty path error will be returned, prompting the user to provide a valid path. For example: data: {"user": {"id": "001"}}; path: ""; returns "Error: Path cannot be empty".
[0099] Alternatively, when the path is too long and exceeds the preset parsing depth, a depth limit error will be triggered to prevent infinite recursion. For example: data: {"a": {"b": {"c": {...}}}}; path: "abcde...z"; returns "Error: Path exceeds maximum allowed depth".
[0100] It can be seen that the prompt information in this embodiment can help users identify input errors and guide them on how to use the parsing tool correctly. The error handling mechanism further improves the robustness of the method and ensures that users can obtain clear feedback during use, thereby improving the reliability of the parsing process and user experience.
[0101] It should be noted that, in each of the above-mentioned embodiments, there is not necessarily a certain order between the above-mentioned steps. A person skilled in the art can understand, based on the description of the embodiments of the present application, that in different embodiments, the above-mentioned steps may have different execution orders, that is, they may be executed in parallel, may be executed interchangeably, and so on.
[0102] As another aspect of the embodiment of the present application, the embodiment of the present application provides a Json data parsing device. The Json data parsing device can be a software module, and the software module includes a number of instructions, which are stored in a memory. The processor can access the memory and call the instructions for execution to complete the Json data parsing method described in the above-mentioned various embodiments.
[0103] See also Figure 2 , Figure 2 Schematic diagram of a Json data parsing device provided in an embodiment of the present application. Figure 2 As shown, the Json data parsing device 200 includes: The acquisition unit 201 is used to acquire the Json data to be parsed; The parsing unit 202 is used to perform preliminary parsing on the Json data to be parsed to obtain target Json data that can be recognized by a computer; The extraction unit 203 is used to obtain the input parsing path, and extract the parsing result based on the parsing path and the target Json data. The parsing path is used to indicate the location of the parsing result.
[0104] This method first obtains the Json data to be parsed and performs preliminary parsing, so as to quickly obtain the target Json data that can be recognized by the computer, thereby improving the efficiency of data processing; using the input parsing path, the target field in the Json data can be quickly located and extracted, which simplifies the parsing process of the Json data, avoids traversing the entire Json data structure layer by layer, and reduces the complexity and redundancy of the code; further, the parsing path can indicate the position of the object hierarchy and array elements, and is suitable for various complex data structures, including nested objects and mixed structures, which enhances the flexibility and adaptability of the method, so that it can adapt to different parsing requirements. Whether it is a simple key-value pair or a complex nested structure, data can be obtained by adjusting the parsing path, thereby improving development efficiency.
[0105] In one embodiment, in the extraction of the parsing result based on the parsing path and the target Json data, the parsing path is used to indicate the location of the parsing result, and the parsing unit 202 is further used to: parse the parsing path according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path; extract the corresponding parsing result from the target Json data based on the object hierarchy and / or array and array element position contained in the parsing path.
[0106] In one embodiment, the parsing path includes a first identifier indicating an object level, and / or a second identifier indicating an array and a third identifier indicating a position of an array element.
[0107] In one embodiment, in parsing the parsing path according to a preset path rule to determine the object hierarchy and / or array and array element position contained in the parsing path, the parsing unit 202 is further used to: determine whether there is a first identifier in the parsing path, if so, segment the parsing path based on the first identifier to obtain multiple levels, determine whether there is a second identifier in each level, if so, it means that the value of the current level is an array, and determine the array element position according to a third identifier associated with the second identifier.
[0108] In one embodiment, the preset path rule is as follows: when the object hierarchy contains multiple levels, adjacent levels are separated by a first identifier; an array is indicated by a second identifier, and an element position in the array is indicated by a third identifier.
[0109] In one embodiment, in extracting the corresponding parsing result from the target Json data based on the object hierarchy and / or array and array element position included in the parsing path, the parsing unit 202 is further used to: determine the data structure type to be parsed based on the object hierarchy and / or array and array element position included in the parsing path, the data structure type including: array structure, object structure, nested object structure and mixed structure; determine the parsing rule based on the data structure type, and extract the corresponding parsing result from the target Json data based on the parsing rule and the object hierarchy and / or array and array element position included in the parsing path.
[0110] In one embodiment, the parsing unit 202 is also used for: when the target Json data is parsed according to the object hierarchy and / or array and array element position contained in the parsing path, if it is found that the parsing path does not exist, a prompt of a path error is returned, and the prompt includes the type of error.
[0111] In one embodiment, the parsing unit 202 is further used to return corresponding prompt information when it is detected that the parsing path or the target Json data is empty.
[0112] It should be noted that the above-mentioned Json data parsing device can execute the Json data parsing method provided in the embodiment of the present application, and has the corresponding functional modules and beneficial effects of the execution method. For technical details not fully described in the embodiment of the Json data parsing device, please refer to the Json data parsing method provided in the embodiment of the present application.
[0113] See also Figure 3 , Figure 3 3 is a schematic diagram of the structure of a computer device provided in an embodiment of the present application. The computer device includes one or more processors 31 and a memory 32. The memory 32 is connected to the one or more processors 31, for example, connected to the processor 31 via a bus.
[0114] The processor 31 is configured to support the computer device to perform the corresponding functions in the method in the above method embodiment. The processor 31 can be a central processing unit (CPU), a network processor (NP), a hardware chip or any combination thereof. The above hardware chip can be an application specific integrated circuit (ASIC), a programmable logic device (PLD) or a combination thereof. The above PLD can be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL) or any combination thereof.
[0115] The memory 32 is used to store program codes, etc. The memory 32 may include a volatile memory (VM), such as a random access memory (RAM); the memory 32 may also include a non-volatile memory (NVM), such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD) or a solid-state drive (SSD); the memory 32 may also include a combination of the above-mentioned types of memories.
[0116] The memory 32 can be used to store non-volatile software programs, non-volatile computer executable programs and modules, such as program instructions / modules corresponding to the Json data parsing method in the embodiment of the present application. The processor 31 executes the various functional applications and data processing of the Json data parsing method and the Json data parsing device by running the non-volatile software programs, instructions and modules stored in the memory 32, that is, realizes the functions of each module or unit of the Json data parsing method and the Json data parsing device provided in the above method embodiment.
[0117] The memory 32 may include a program storage area and a data storage area, wherein the program storage area may store an operating system and an application required for at least one function. The data storage area may store data created according to the use of the parsing device for Json data, etc. In some embodiments, the memory 32 may optionally include a memory 32 remotely arranged relative to the processor 31, and these remote memories 32 may be connected to the parsing device for Json data via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0118] The one or more modules are stored in the memory 32, and when executed by the one or more processors 31, the Json data parsing method in any of the above method embodiments is executed, for example, the method steps described in the above method embodiments are executed to realize the functions of the modules described in the above device embodiments.
[0119] An embodiment of the present application further provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes the method described in the above embodiment.
[0120] A person skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium can be a disk, an optical disk, a read-only memory (ROM) or a random access memory (RAM), etc.
[0121] The above disclosure is only the preferred embodiment of the present application, which certainly cannot be used to limit the scope of rights of the present application. Therefore, equivalent changes made according to the claims of the present application are still within the scope covered by the present application.
Claims
1. A method for parsing Json data, characterized in that: include: Get the Json data to be parsed; Perform a preliminary analysis on the Json data to be analyzed to obtain target Json data that can be recognized by a computer; The input parsing path is obtained, and the parsing result is extracted based on the parsing path and the target Json data. The parsing path is used to indicate the location of the parsing result.
2. The method according to claim 1, characterized in that The extracting and obtaining a parsing result based on the parsing path and the target Json data, wherein the parsing path is used to indicate the location of the parsing result, includes: Parsing the parsing path according to a preset path rule to determine the object hierarchy and / or array and array element positions contained in the parsing path; The corresponding parsing result is extracted from the target Json data based on the object hierarchy and / or array and array element position contained in the parsing path.
3. The method according to claim 1, characterized in that The parsing path includes a first identifier indicating an object level, and / or a second identifier indicating an array and a third identifier indicating a position of an array element.
4. The method according to claim 2, characterized in that: The parsing path according to the preset path rule is parsed to determine the object hierarchy and / or array and array element position contained in the parsing path, including: Determine whether there is a first identifier in the resolution path. If so, segment the resolution path based on the first identifier to obtain multiple levels. Determine whether there is a second identifier in each level. If so, it means that the value of the current level is an array, and determine the position of the array element according to the third identifier associated with the second identifier.
5. The method according to claim 2, characterized in that: The preset path rules are as follows: When the object hierarchy contains multiple levels, the adjacent levels are separated by the first identifier; The second identifier is used to indicate an array, and the third identifier is used to indicate a position of an element in the array.
6. The method according to claim 2, characterized in that The extracting corresponding parsing results from the target Json data based on the object hierarchy and / or array and array element position contained in the parsing path includes: Determine the type of data structure to be parsed based on the object hierarchy and / or array and array element position contained in the parsing path, the data structure type including: array structure, object structure, nested object structure and mixed structure; A parsing rule is determined based on the data structure type, and a corresponding parsing result is extracted from the target Json data based on the parsing rule and the object hierarchy and / or array and array element positions included in the parsing path.
7. The method according to claim 2, characterized in that The method further comprises: When the target Json data is parsed according to the object hierarchy and / or array and array element position contained in the parsing path, if it is found that the parsing path does not exist, a prompt of a path error is returned, and the prompt includes the type of error.
8. The method according to claim 1, characterized in that The method further comprises: When it is detected that the parsing path or the target Json data is empty, the corresponding prompt information is returned.
9. A computer device, comprising a memory and a processor, wherein the memory is connected to the processor, and the processor is used to execute one or more computer programs stored in the memory, and when the processor executes the one or more computer programs, the computer device implements the method according to any one of claims 1 to 8.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the processor is caused to perform the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Analysis method and device for parameters in character string, storage medium and electronic equipment
CN111061482A
Strong type static programming language JSON analysis method
CN113126972A
Data analysis method and device, electronic equipment and storage medium
CN116306571A
Data processing method and device, storage medium and electronic equipment
CN116361333A
Data processing method, computer equipment and readable storage medium
CN117521610A
