A JSON data parsing method, system, device and medium
Patent Information
- Application Number
- CN202610776232.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-01
- Publication Date
- 2026-10-23
AI Technical Summary
[0004]本申请实施例提供了一种JSON数据解析方法、系统、装置和介质,以至少解决相关技术中现有JSON数据解析存在的解析效率低的问题
[0015]相比于相关技术,本申请实施例提供的一种JSON数据解析方法、系统、装置和介质,其中,该方法通过配置JSON数据与目标结构体之间的三层映射描述结构,以无需人工编写定制化解析代码;基于三层映射描述结构对全类型JSON数据进行递归式分层解析,以转换至目标结构体,其中,全类型JSON数据包含匿名结构体和匿名结构体数组,实现了无需编写定制化解析代码的JSON解析,大幅提升了开发效率,减少了重复编码量,覆盖了匿名结构体、匿名结构体数组等各类复杂 JSON 数据类型的解析,提升了类型适配能力和接口复用性,解决了现有JSON数据解析存在的解析效率低的问题。
Smart Images

Figure CN122965430A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer data processing and computing, and in particular to a JSON data parsing method, system, apparatus, and medium. Background Technology
[0002] JSON, as a lightweight data exchange format, is widely used in embedded systems, server communication, and other scenarios. The main pain points of existing JSON parsing technologies are: Customized development is costly, specifically because different business structures / arrays require manual writing of parsing logic. Customized parsing code is needed to map different JSON structures to the target structure and parse JSON data, resulting in a large amount of repetitive coding and low development efficiency.
[0003] Currently, no effective solution has been proposed to address the low parsing efficiency of existing JSON data parsing technologies. Summary of the Invention
[0004] This application provides a JSON data parsing method, system, apparatus, and medium to at least solve the problem of low parsing efficiency in existing JSON data parsing technologies.
[0005] In a first aspect, embodiments of this application provide a JSON data parsing method, the method comprising: Configure a three-layer mapping description structure between JSON data and the target structure to eliminate the need for manually writing custom parsing code; Based on the three-layer mapping description structure, recursive hierarchical parsing is performed on all types of JSON data to transform it into the target structure, wherein the all types of JSON data include anonymous structures and arrays of anonymous structures.
[0006] In some embodiments, the three-layer mapping description structure for configuring JSON data and the target structure includes: Define a data parsing type system for JSON data and target structures, wherein the data parsing type system covers basic data types, structure data types, and array data types; Define a standardized configuration for parsing array-type data in JSON data; Define a mapping table between structure fields in JSON data and structure fields in the target structure; Based on the data parsing type system, the standardized configuration, and the mapping relationship table, the configuration of the three-layer mapping description structure between the JSON structure and the target structure is completed.
[0007] In some embodiments, recursive hierarchical parsing of all types of JSON data based on the three-layer mapping description structure to transform it into the target structure includes: The anonymous structs and arrays of anonymous structs contained in the full-type JSON data have no keys, and the array elements have heterogeneous types and a fixed number of elements; Based on the three-layer mapping description structure, the heterogeneous elements in the anonymous structure and the anonymous structure array are parsed one by one, and the structure field offset of the target structure is matched according to the array index to convert to the target structure.
[0008] In some embodiments, recursive hierarchical parsing of all types of JSON data based on the three-layer mapping description structure to transform it into the target structure further includes: For the basic data type, based on the three-layer mapping description structure, the JSON field value of the basic data type is directly written to the corresponding offset position of the target structure.
[0009] In some embodiments, recursive hierarchical parsing of all types of JSON data based on the three-layer mapping description structure to transform it into the target structure further includes: For the structure type data, based on the three-layer mapping description structure, the isomorphic elements of the non-anonymous structures in the structure type data are recursively parsed one by one to transform them into the target structure.
[0010] In some embodiments, recursive hierarchical parsing of all types of JSON data based on the three-layer mapping description structure to transform it into the target structure further includes: For the array type data, based on the three-layer mapping description structure, memory is automatically allocated for the non-anonymous array in the array type data according to the array descriptor, and isomorphic elements in the non-anonymous array are traversed to convert to the target structure.
[0011] In some embodiments, the method further includes: Based on the three-layer mapping description structure, the memory allocated during the parsing process for the basic type data, structure type data, and array type data is automatically released.
[0012] Secondly, embodiments of this application provide a JSON data parsing system, the system being used to execute the method described in the first aspect above, the system comprising a configuration module and a parsing module; The configuration module is used to configure the three-layer mapping description structure between JSON data and the target structure, so that there is no need to manually write customized parsing code; The parsing module is used to perform recursive hierarchical parsing of all types of JSON data according to the three-layer mapping description structure, so as to transform it into the target structure, wherein the all types of JSON data includes anonymous structures and an array of anonymous structures.
[0013] Thirdly, embodiments of this application provide an electronic device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the method described in the first aspect above.
[0014] Fourthly, embodiments of this application provide a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect above.
[0015] Compared to related technologies, the present application provides a JSON data parsing method, system, apparatus, and medium. This method configures a three-layer mapping description structure between JSON data and a target structure, eliminating the need for manually writing customized parsing code. Based on the three-layer mapping description structure, it recursively parses all types of JSON data to convert them into the target structure. The all types of JSON data include anonymous structures and arrays of anonymous structures. This achieves JSON parsing without writing customized parsing code, significantly improving development efficiency, reducing repetitive coding, covering the parsing of various complex JSON data types such as anonymous structures and arrays of anonymous structures, improving type adaptability and interface reusability, and solving the problem of low parsing efficiency in existing JSON data parsing methods. Attached Figure Description
[0016] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings: Figure 1 This is a flowchart of the steps of a JSON data parsing method according to an embodiment of this application; Figure 2 This is a schematic diagram of the internal structure of an electronic device according to an embodiment of this application. Detailed Implementation
[0017] To make the objectives, technical solutions, and advantages of this application clearer, the application is described and illustrated below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application. All other embodiments obtained by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application.
[0018] Obviously, the accompanying drawings described below are merely some examples or embodiments of this application. Those skilled in the art can apply this application to other similar scenarios based on these drawings without any inventive effort. Furthermore, it is understood that although the efforts made in this development process may be complex and lengthy, for those skilled in the art related to the content disclosed in this application, any changes to design, manufacturing, or production based on the technical content disclosed in this application are merely conventional technical means and should not be construed as insufficient disclosure of the content of this application.
[0019] In this application, the reference to "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment that is mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0020] Unless otherwise defined, the technical or scientific terms used in this application shall have the ordinary meaning understood by one of ordinary skill in the art to which this application pertains. The terms “a,” “an,” “an,” “the,” and similar words used in this application do not indicate quantity limitation and may indicate singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units not listed, or may include other steps or units inherent to these processes, methods, products, or devices. The terms “connected,” “linked,” “coupled,” and similar words used in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. “Multiple” used in this application refers to two or more. “And / or” describes the relationship between related objects, indicating that three relationships may exist; for example, “A and / or B” can represent: A alone, A and B simultaneously, and B alone. The character " / " generally indicates that the preceding and following objects are in an "or" relationship. The terms "first," "second," and "third" used in this application are merely to distinguish similar objects and do not represent a specific ordering of the objects.
[0021] This application provides a JSON data parsing method. Figure 1This is a flowchart of the steps of the JSON data parsing method according to an embodiment of this application, as follows: Figure 1 As shown, the method includes the following steps: Step S102: Configure a three-layer mapping description structure between JSON data and the target structure to eliminate the need for manually writing customized parsing code; Step S102 specifically includes the following steps: Step S1021 defines the data parsing type system for JSON data and target structure, wherein the data parsing type system covers basic data types, structure data types, and array data types; Step S1021 preferably defines a data parsing type system (basic type enumeration DDM_Type): covering basic types (int / double / bool / string), structure types (non-anonymous structure / anonymous structure), and array types (basic type array / structure array / anonymous structure array).
[0022] Step S1022: Define the standardized configuration for parsing array-type data in JSON data; Step S1022 preferably implements a standardized configuration for array parsing (array descriptor DDM_ArrayDesc): defining array element type, element memory size, and nested structure mapping table.
[0023] Step S1023: Define a mapping table between structure fields in the JSON data and structure fields in the target structure; Preferably, step S1023 involves achieving a precise mapping between "JSON key → structure field" (field mapping table DDM_FieldMap): associating JSON key name, target structure field offset, field type, array length offset, and nested mapping table.
[0024] Step S1024, based on the data parsing type system, standardized configuration and mapping relationship table, completes the configuration of the three-layer mapping description structure between the JSON structure and the target structure.
[0025] Step S104: Based on the three-layer mapping description structure, recursively parse all types of JSON data to transform it into the target structure. The all types of JSON data include anonymous structures and arrays of anonymous structures.
[0026] Step S104 specifically includes the following steps: Step S1041: The anonymous structs and arrays of anonymous structs contained in the full-type JSON data have no keys, and the array elements are heterogeneous in type and have a fixed number of elements. Based on the three-layer mapping description structure, the heterogeneous elements in the anonymous structs and arrays of anonymous structs are parsed one by one, and the offset of the struct field of the target struct is matched according to the array index to convert to the target struct.
[0027] Preferably, in step S1041, for parsing anonymous structures: for JSON arrays without keys (with heterogeneous element types and a fixed number of elements), the parsing is completed by matching the structure field offsets according to the array index; for parsing anonymous structure arrays: for the scenario of anonymous structure arrays with "JSON arrays without keys, heterogeneous element types but a fixed number of elements", the heterogeneous elements are parsed one by one by matching the structure field offsets according to the array index (ddm_parse_anonymous / ddm_parse_array_struct_anonymous dedicated parsing process).
[0028] Step S1042: For basic data types, based on the three-layer mapping description structure, the JSON field values of the basic data types are directly written to the corresponding offset positions of the target structure.
[0029] Step S1042 preferably involves basic type parsing: for int / double / bool / string types, the JSON field value is directly written to the target structure at the specified offset position; Step S1043: For structure type data, based on the three-layer mapping description structure, recursively parse the isomorphic elements of non-anonymous structures in the structure type data one by one to transform them into the target structure.
[0030] Step S1043 preferably involves parsing a struct (non-anonymous struct): recursively calling the parsing logic to parse the JSON object into a nested struct based on the nested field mapping table; Step S1044: For array type data, based on the three-level mapping description structure, memory is automatically allocated for the non-anonymous array in the array type data according to the array descriptor, and isomorphic elements in the non-anonymous array are traversed to convert to the target structure.
[0031] Step S1044 preferably involves parsing the array (non-anonymous array): for int / double / bool / string arrays, memory is automatically allocated based on the array descriptor, and the JSON array is traversed to complete element filling.
[0032] It should be noted that for the automated JSON parsing in steps S1041 to S1044 above, the ddm_parse_object (JSON object parsing) and ddm_parse_file (JSON file parsing) interfaces are provided. Users only need to pass in the target structure pointer and the mapping table to complete the "one-click parsing".
[0033] In addition to steps S102 and S104 above, the method also includes: step S105, automatically releasing the memory allocated during the parsing process for basic type data, structure type data and array type data based on the three-layer mapping description structure.
[0034] Step S105 preferably includes an automated memory management mechanism for parsing. A corresponding `ddm_free_object` memory release logic is implemented, which automatically releases memory allocated during parsing by reversing the structure fields based on the mapping table in the three-layer mapping description structure. Specifically: String / String Array: Releases the memory allocated by strdup for the string and the array container; Array of structs / anonymous array of structs: recursively release the nested memory of each struct element, and then release the memory of the array container; Arrays of primitive types: directly release the memory of the array container; Nested structs: Recursively release the memory of fields in nested structs.
[0035] It should be noted that the method provided in this application implements a very simple JSON parsing method, requiring a small amount of data for parsing. By establishing a correspondence between JSON and the target structure (programming languages such as C and C++), the mapping relationship is configurable, eliminating the need to write customized parsing code, significantly improving development efficiency and reducing repetitive coding. It covers the parsing of various complex JSON data types such as nested structures and anonymous structure arrays, improving type adaptability and interface reusability, and achieving "one-click" parsing. Through an automated memory management mechanism for parsing, it solves the risks of memory leaks and dangling pointers caused by manual memory management, improving the stability of the parsing process. The dedicated anonymous structure array parsing logic fills the gap in the existing technology and provides a general solution for parsing special JSON structures.
[0036] It should be further noted that in the prior art, the patent with publication number CN121118838A recursively parses JSON using a mapping table. However, the purpose of this prior art is to convert JSON into XML, which is completely different from the internal data structure of the JSON parsing in this application, which is a programming language (such as C). Therefore, this prior art cannot solve the many problems faced by this application in the parsing process. Specifically: This existing patent does not support anonymous arrays and anonymous structs. Generally, JSON structures contain several structure types: basic data types (such as int / string / bool / double, etc.), struct types ({ "field1":"value1", "field2":"value2"}), array types (for example, string arrays, such as [ "name1", "name2", "name3"]), and anonymous arrays (such as [ "", 1, true, 2.1 ], where the element types within the array are inconsistent). The handling of anonymous arrays and anonymous structs differs from that of general structs and arrays. Furthermore, this application takes into account automatic memory allocation and automatic memory release during the parsing process. Automatic memory allocation only requires a mapping table and specifying the data type; the data is automatically allocated during the parsing process without any additional processing. Automatic memory release also only requires a mapping table and specifying the data type; the address of the mapping table is passed in when releasing the data, and it can be handled with a single line of code (e.g., ddm_json_free_object(ptr, jsonMap)).
[0037] This application provides a JSON data parsing system for executing the methods provided in the above embodiments. The system includes a configuration module and a parsing module. The configuration module is used to configure the three-layer mapping description structure between JSON data and the target structure, so that no custom parsing code needs to be written manually; The parsing module is used to recursively parse all types of JSON data according to the three-layer mapping description structure to transform it into the target structure. The all types of JSON data include anonymous structures and arrays of anonymous structures.
[0038] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.
[0039] This embodiment provides an electronic device including a memory and a processor. The memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.
[0040] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.
[0041] Optionally, the electronic device may further include a processor, memory, network interface, display screen, and input device connected via a system bus. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The network interface is used to communicate with external terminals via a network connection. When the computer program is executed by the processor, it implements a JSON data parsing method. The display screen may be an LCD screen or an e-ink display screen. The input device may be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the device's casing, or an external keyboard, touchpad, or mouse.
[0042] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated here.
[0043] Furthermore, in conjunction with the JSON data parsing methods in the above embodiments, this application embodiment can provide a storage medium for implementation. This storage medium stores a computer program; when executed by a processor, the computer program implements any of the JSON data parsing methods in the above embodiments.
[0044] In one embodiment, Figure 2 This is a schematic diagram of the internal structure of an electronic device according to an embodiment of this application, such as... Figure 2 As shown, an electronic device is provided, which can be a server, and its internal structure diagram can be as follows. Figure 2 As shown, the electronic device includes a processor, a network interface, internal memory, and non-volatile memory connected via an internal bus. The non-volatile memory stores an operating system, computer programs, and a database. The processor provides computing and control capabilities, the network interface communicates with external terminals via a network connection, the internal memory provides an environment for the operation of the operating system and computer programs, the computer programs are executed by the processor to implement a JSON data parsing method, and the database stores data.
[0045] Those skilled in the art will understand that Figure 2 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the electronic device to which the present application is applied. A specific electronic device may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0046] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. This computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0047] Those skilled in the art should understand that the technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments have been described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0048] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the invention patent. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this patent application should be determined by the appended claims.
Claims
1. A JSON data parsing method, characterized in that, The method includes: Configure a three-layer mapping description structure between JSON data and the target structure to eliminate the need for manually writing custom parsing code; Based on the three-layer mapping description structure, recursive hierarchical parsing is performed on all types of JSON data to transform it into the target structure, wherein the all types of JSON data include anonymous structures and arrays of anonymous structures.
2. The method according to claim 1, characterized in that, The three-layer mapping description structure between the configured JSON data and the target struct includes: Define a data parsing type system for JSON data and target structures, wherein the data parsing type system covers basic data types, structure data types, and array data types; Define a standardized configuration for parsing array-type data in JSON data; Define a mapping table between structure fields in JSON data and structure fields in the target structure; Based on the data parsing type system, the standardized configuration, and the mapping relationship table, the configuration of the three-layer mapping description structure between the JSON structure and the target structure is completed.
3. The method according to claim 2, characterized in that, Based on the aforementioned three-layer mapping description structure, recursive hierarchical parsing of all types of JSON data is performed to transform it into the target structure, including: The anonymous structs and arrays of anonymous structs contained in the full-type JSON data have no keys, and the array elements have heterogeneous types and a fixed number of elements; Based on the three-layer mapping description structure, the heterogeneous elements in the anonymous structure and the anonymous structure array are parsed one by one, and the structure field offset of the target structure is matched according to the array index to convert to the target structure.
4. The method according to claim 2, characterized in that, The process of recursively parsing all types of JSON data and converting it to the target structure based on the three-layer mapping description structure also includes: For the basic data type, based on the three-layer mapping description structure, the JSON field value of the basic data type is directly written to the corresponding offset position of the target structure.
5. The method according to claim 2, characterized in that, The process of recursively parsing all types of JSON data and converting it to the target structure based on the three-layer mapping description structure also includes: For the structure type data, based on the three-layer mapping description structure, the isomorphic elements of the non-anonymous structures in the structure type data are recursively parsed one by one to transform them into the target structure.
6. The method according to claim 2, characterized in that, The process of recursively parsing all types of JSON data and converting it to the target structure based on the three-layer mapping description structure also includes: For the array type data, based on the three-layer mapping description structure, memory is automatically allocated for the non-anonymous array in the array type data according to the array descriptor, and isomorphic elements in the non-anonymous array are traversed to convert to the target structure.
7. The method according to claim 2, characterized in that, The method further includes: Based on the three-layer mapping description structure, the memory allocated during the parsing process for the basic type data, structure type data, and array type data is automatically released.
8. A JSON data parsing system, characterized in that, The system is used to perform the method according to any one of claims 1 to 7, and the system includes a configuration module and a parsing module; The configuration module is used to configure the three-layer mapping description structure between JSON data and the target structure, so that there is no need to manually write customized parsing code; The parsing module is used to perform recursive hierarchical parsing of all types of JSON data according to the three-layer mapping description structure, so as to transform it into the target structure, wherein the all types of JSON data includes anonymous structures and an array of anonymous structures.
9. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to perform the method of any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Templated format conversion method and system for hierarchical metadata
CN121118838A