A JSON object parsing method, device, electronic device and storage medium

By judging the first call of the target attribute in the subclass of the JSON object, and adding a general parsing function for parsing it during the first call, the invalid parsing problem caused by redundant fields and unnecessary fields in JSON object parsing is solved, and the performance loss is reduced and the parsing efficiency is improved.

CN113971029BActive Publication Date: 2025-06-03BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111257546.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-10-27
Publication Date
2025-06-03
Estimated Expiration
2041-10-27

AI Technical Summary

Technical Problem

During the JSON object parsing process, since the JSON object may contain redundant fields and unnecessary fields, the result is invalid parsing and performance loss.

Method used

By judging whether the target attribute is called for the first time in the subclass corresponding to the JSON object, if so, the base class will add the general parsing function as the acquisition function of the target attribute and parse it; if it is not the first time, the target attribute obtaining function is directly used to obtain the parsed value.

Benefits of technology

Avoid parsing of redundant fields and unnecessary fields, reduces the performance loss caused by JSON object parsing, and improves the accuracy and efficiency of the analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113971029B_ABST
    Figure CN113971029B_ABST
Patent Text Reader

Abstract

An embodiment of the present invention provides a JSON object parsing method, apparatus, electronic device, and storage medium, which relate to the field of computer technology and can reduce the performance loss caused by parsing JSON objects. The method includes: when a target attribute in a subclass corresponding to a JSON object is called, determining whether the target attribute is called for the first time. Attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating a getter function and a setter function for the target attribute. If it is called for the first time, the base class is called to add a general parsing function as the getter function corresponding to the target attribute, and the general parsing function is used to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and the value to be parsed is parsed. If it is not called for the first time, the parsed value corresponding to the target attribute is obtained from the private attribute corresponding to the target attribute by using the getter function corresponding to the target attribute.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technologies, and particularly to a method, apparatus, electronic device, and storage medium for parsing JSON objects. Background Art

[0002] JavaScript Object Notation (JSON) is a lightweight data interchange format that can be applied to data interaction between devices. For example, data in JSON format can be transmitted between a terminal and a server.

[0003] To transmit JSON format data, it is necessary to pre-write the fields included in the JSON object through code. As the business develops, the number of fields included in the JSON object will also increase. Since the code is completed by multiple people in collaboration during the program iteration of the business and the completion time span is large. To avoid the program from not running due to accidentally deleting the fields of the data object, programmers usually choose to only add new fields that need to be used to the JSON object, rather than deleting fields. In this way, redundant fields will appear during the long-term iterative update process of the JSON object.

[0004] When parsing a JSON object, usually all the fields included in the JSON object are parsed at one time. However, the JSON object may include redundant fields and may also include fields that are not used in the current business scenario. Therefore, the current parsing of the JSON object includes some invalid parses, which will cause unnecessary performance loss. Summary of the Invention

[0005] The purpose of the embodiments of the present application is to provide a method, apparatus, electronic device, and storage medium for parsing JSON objects to reduce the performance loss caused by parsing JSON objects. The specific technical solutions are as follows:

[0006] In the first aspect implemented in the present application, a method for parsing a JSON object is provided, including:

[0007] When a target attribute in a subclass corresponding to the JSON object is called, determine whether the target attribute is called for the first time; wherein, the attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating a getter function and a setter function for the target attribute;

[0008] If the target attribute is called for the first time, call the base class to add a general parsing function as the getter function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed;

[0009] If the target attribute is not called for the first time, the parsed value corresponding to the target attribute is obtained from the private attribute corresponding to the target attribute by using the acquisition function corresponding to the target attribute.

[0010] In a second aspect of the implementation of the present application, a JSON object parsing device is further provided. The device includes:

[0011] A judgment module, configured to judge whether the target attribute is called for the first time when the target attribute in the subclass corresponding to the JSON object is called; wherein, the attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating an acquisition function and an assignment function for the target attribute;

[0012] An analysis module, configured to, if the target attribute is called for the first time, call the base class to add a general parsing function as the acquisition function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed;

[0013] An acquisition module, configured to, if the target attribute is not called for the first time, obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute by using the acquisition function corresponding to the target attribute.

[0014] In a third aspect of the implementation of the present application, an electronic device is further provided, including a processor, a communication interface, a memory, and a communication bus. Among them, the processor, the communication interface, and the memory complete communication with each other through the communication bus;

[0015] The memory is used to store a computer program;

[0016] The processor is configured to, when executing the program stored on the memory, implement the steps of any of the above-mentioned JSON object parsing methods.

[0017] In a fourth aspect of the implementation of the present application, a computer-readable storage medium is further provided. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements any of the above-mentioned JSON object parsing methods.

[0018] In a fifth aspect of the implementation of the present application, a computer program product including instructions is further provided. When it runs on a computer, it causes the computer to execute any of the above-mentioned JSON object parsing methods.

[0019] Advantages of the embodiments of the present application:

[0020] The JSON object parsing method, device, electronic device, and storage medium provided by the embodiments of the present application can, when a target attribute in a subclass corresponding to a JSON object is called, if the target attribute is called for the first time, call the base class to add a general parsing function as the acquisition function corresponding to the target attribute, and use the general parsing function for parsing; if the target attribute is not called for the first time, directly use the acquisition function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute. It can be seen that in the embodiments of the present application, the attribute value of a certain attribute is parsed only when it is first required to be called, and other attributes except the target attribute are not parsed during parsing. Therefore, redundant fields and unused fields are avoided from being parsed, that is, invalid parsing is avoided, so the performance loss caused by parsing the JSON object is reduced. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art.

[0022] Figure 1 It is a flowchart of a JSON object parsing method in an embodiment of the present invention;

[0023] Figure 2 It is a flowchart of another JSON object parsing method in an embodiment of the present invention;

[0024] Figure 3 It is a flowchart of another JSON object parsing method in an embodiment of the present invention;

[0025] Figure 4 It is a flowchart of another JSON object parsing method in an embodiment of the present invention;

[0026] Figure 5 It is a flowchart of setting an attribute in an embodiment of the present invention;

[0027] Figure 6 It is a flowchart of object parsing in an embodiment of the present invention;

[0028] Figure 7 It is a flowchart of array parsing in an embodiment of the present invention;

[0029] Figure 8 It is a schematic structural diagram of a JSON object parsing device in an embodiment of the present invention;

[0030] Figure 9 It is a schematic structural diagram of an electronic device in an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0031] The technical solutions in the embodiments of the present invention will be described below with reference to the accompanying drawings in the embodiments of the present invention.

[0032] To reduce the performance loss caused by parsing JavaScript Object Notation (JSON) objects, an embodiment of the present application provides a JSON object parsing method, apparatus, electronic device, and storage medium.

[0033] The JSON object parsing method will be described first below. This method can be applied to an electronic device, where the electronic device can be a server, mobile phone, computer, or tablet computer, etc., which can parse JSON objects. Refer to Figure 1 , the JSON object parsing method provided by the embodiment of the present application includes the following steps:

[0034] S101. When a target attribute in a subclass corresponding to a JSON object is called, determine whether the target attribute is called for the first time. If the target attribute is called for the first time, execute S102; if the target attribute is not called for the first time, execute S103.

[0035] Among them, the attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating a getter function and a setter function for the target attribute.

[0036] S102. Call the base class to add a general parsing function as the getter function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed.

[0037] S103. Use the getter function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute.

[0038] The JSON object parsing method provided by the embodiment of the present application can, when a target attribute in a subclass corresponding to a JSON object is called, if the target attribute is called for the first time, call the base class to add a general parsing function as the getter function corresponding to the target attribute, and use the general parsing function for parsing; if the target attribute is not called for the first time, directly use the getter function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute. It can be seen that in the embodiment of the present application, the attribute value of a certain attribute is parsed only when it is first required to be called, and other attributes other than the target attribute are not parsed during parsing. Therefore, redundant fields and unused fields are avoided from being parsed, that is, invalid parsing is avoided, so the performance loss caused by parsing JSON objects is reduced.

[0039] The following provides a detailed description of the JSON object parsing method provided by the embodiments of the present application.

[0040] Regarding the above S101, when the target attribute in the subclass corresponding to the JSON object is called, it is determined whether the target attribute is called for the first time.

[0041] The JSON object refers to the JSON object in the dictionary to be parsed. Each JSON object is recorded in the form of key-value pairs.

[0042] In the embodiments of the present application, the subclass can be a JSON model (jsonModel) created by deriving a subclass from the base class (jsonModelBase). The subclass includes a header file and a source file.

[0043] The header file of the subclass includes: the attributes corresponding to the JSON object.

[0044] Among them, the attributes corresponding to the JSON object in the subclass refer to: the attributes in the subclass with the same name as the key value in the dictionary to be parsed. Among them, the attributes corresponding to the JSON object in the subclass can be created through the @property decorator. The @property decorator can create a read-only attribute with the same name as the key value in the dictionary to be parsed, thereby preventing the compiler from automatically generating the corresponding implementation function to modify the attribute.

[0045] The source file of the subclass includes: the markers of the attributes corresponding to the JSON object in the subclass. Among them, the attributes corresponding to the JSON object in the subclass are all marked with the specified keyword.

[0046] Among them, the specified keyword is used to prohibit the compiler from automatically generating the get function and set function for the attribute. For example, the specified keyword is: @dynamic. Or the specified keyword can also be other keywords, and the embodiments of the present application do not make specific limitations on this.

[0047] Regarding the above S102, call the base class to add the general parsing function as the get function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed.

[0048] In an embodiment of the present application, the base class is used to implement the core work of parsing JSON objects. Therefore, in the case where the target attribute is called for the first time, since the target attribute is marked with a specified keyword, the get function and set function cannot be automatically generated. Therefore, at this time, the subclass does not implement the get function corresponding to the target attribute. At this time, the message forwarding mechanism of the Objective C language will be triggered. The base class can add a general parsing function for the target attribute of the subclass through the method +(BOOL)resolveInstanceMethod:(SEL)sel, which is equivalent to using this general parsing function as the get function of the target attribute of the subclass. Furthermore, the value to be parsed of the target attribute can be parsed through this general parsing function.

[0049] Regarding the above S103, use the acquisition function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute.

[0050] In an embodiment of the present application, the header file of the subclass further includes: the private attribute corresponding to each attribute in the subclass, and the type of the private attribute is the same as the type of the corresponding attribute. Optionally, the name of the private attribute can be the name after adding an underscore before the name of the corresponding attribute. The type of the attribute can be a dictionary type, an array type, or a non-data structure type (i.e., a string type).

[0051] The private attribute is used to store the parsed value of the corresponding attribute. That is, when the base class parses an attribute in the subclass for the first time and obtains the parsed value, the value is stored in the private attribute corresponding to the attribute. So that when the attribute is called subsequently, the parsed value can be obtained from the private attribute corresponding to the attribute through the acquisition function, avoiding repeated parsing of the attribute and reducing the performance loss caused by parsing the JSON object.

[0052] In another embodiment of the present application, in the above S101, determining whether the target attribute is called for the first time can be implemented as: determining whether the subclass has implemented the acquisition function corresponding to the target attribute; if so, determining that the target attribute is not called for the first time; if not, determining that the target attribute is called for the first time.

[0053] In one implementation, the electronic device can determine whether the subclass has implemented the get function corresponding to the target attribute, that is, whether the target attribute has also been parsed to obtain the parsed value corresponding to the target attribute.

[0054] In the embodiment of the present application, whether the target attribute is called for the first time is determined by whether the acquisition function of the target attribute has been implemented. Since if the acquisition function of the target attribute has been implemented, it indicates that the parsed value corresponding to the target attribute has been obtained previously. Therefore, the parsed value of the target attribute obtained during the previous parsing can be directly obtained, avoiding repeated parsing of the attribute and improving the efficiency of calling the target attribute.

[0055] In S102 above, refer to Figure 2 , in the case where the target attribute is called for the first time, the electronic device uses the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed, which can be implemented as the following steps:

[0056] S1021. Obtain the target attribute name of the target attribute through the function declaration of the calling function. Wherein, the calling function is the function that calls the target attribute.

[0057] Among them, the function declaration can also be called the function definition, which refers to the establishment of the function function. For example, the function declaration includes the function name, function value type, attribute type, and attribute name, etc.

[0058] S1022. Search for the value to be parsed corresponding to the target attribute name in the dictionary to be parsed.

[0059] Each JSON object in the dictionary to be parsed is recorded in the form of key-value, and the attribute name of the attribute corresponding to the JSON object in the subclass is the same as a key value in the dictionary to be parsed. Therefore, when executing S1022, the value corresponding to the key value with the same name as the target attribute name in the dictionary to be parsed can be searched as the value to be parsed.

[0060] S1023. If the value to be parsed is found, parse the value to be parsed according to the type of the found value to be parsed.

[0061] The type of the value to be parsed is the same as the type of the corresponding attribute. The type of the value to be parsed can be: dictionary type, array type, or non-data structure type (i.e., string type).

[0062] By adopting the above method, when the target attribute is called for the first time, the value corresponding to the key value with the same name as the target attribute name in the dictionary to be parsed can be searched, and then the found value can be parsed to obtain the parsed value.

[0063] Refer to Figure 2 , after the electronic device searches for the value to be parsed corresponding to the target attribute name in the dictionary to be parsed in S1022, the following two steps are further included:

[0064] S201. If the value to be parsed is not found, look up the correct attribute name corresponding to the target attribute name according to the mapping relationship dictionary.

[0065] If the value to be parsed corresponding to the target attribute name is not found in the dictionary to be parsed, it means that there is no key value in the dictionary to be parsed that is the same as the target attribute name. This is because in high-level languages, some predefined key values are reserved keywords, and users cannot use these reserved keywords as variable names or procedure names, that is, they cannot set attributes with the same name as the reserved keywords in subclasses.

[0066] In order to obtain the value corresponding to the reserved keyword in the dictionary to be parsed, in the embodiments of the present application, a mapping relationship dictionary can be set to record the corresponding relationship between the attribute name of the attribute in the subclass corresponding to the reserved keyword and the reserved keyword.

[0067] Therefore, if the value to be parsed is not found, it means that the target attribute name corresponds to a reserved keyword in the dictionary to be parsed. At this time, the base class calls a predefined empty method, and through this method, the mapping relationship dictionary is obtained. At the same time, the method is overridden in the subclass, so that through this method, based on the mapping relationship included in the mapping relationship dictionary, the correct attribute name corresponding to the target attribute name is obtained, and the correct attribute name is returned to the base class. This correct attribute name is the key value of the JSON object corresponding to the target attribute, and this key value is a reserved keyword.

[0068] S202. Look up the value to be parsed corresponding to the correct attribute name in the dictionary to be parsed, and parse the value to be parsed according to the type of the value to be parsed.

[0069] By using the above method, the embodiments of the present application can parse the JSON object to which the reserved keyword in the dictionary to be parsed belongs, so that the application scope of the embodiments of the present application is wider.

[0070] The above-mentioned way of parsing the value to be parsed according to the type of the value to be parsed in S202 has the following three situations:

[0071] Situation 1. If the type of the value to be parsed is a dictionary type, then parse the value to be parsed as an object.

[0072] Among them, the value to be parsed of the dictionary type is an object saved by using "{}", and the value to be parsed of the dictionary type can include JSON objects of string type, JSON objects of array type, and / or JSON objects of dictionary type.

[0073] For example, for the JSON object: "A": {"a": "xxx"}, where A is the key value and {"a": "xxx"} is the value to be parsed, and the type of this value to be parsed is a dictionary type.

[0074] Case 2: If the type of the value to be parsed is an array type, then parse the value to be parsed as an array.

[0075] Among them, the value to be parsed of the array type is an object saved using "[]", and the value to be parsed of the array type may include JSON objects of string type and / or JSON objects of dictionary type.

[0076] For example, for the JSON object: "B": ["b": "xx"], where B is the key value and ["b": "xx"] is the value to be parsed, and the type of this value to be parsed is an array type.

[0077] Case 3: If the type of the value to be parsed is a non-data structure type, then parse the value to be parsed as a string.

[0078] Among them, the non-data structure type is a string type. For example, for the JSON object: "a": "xxx", where a is the key value and xxx is the value to be parsed, and the type of this value to be parsed is a string type.

[0079] By adopting this method, the embodiments of the present application can parse values to be parsed of various types, expanding the application scope of the embodiments of the present application.

[0080] See Figure 3 , on the basis of Figure 1 , before determining whether the target attribute is called for the first time in the above S101, the following steps are further included:

[0081] S301: Determine whether the calling function is a getting function. If so, execute the above S101; if not, execute S302. Among them, the calling function is the function that calls the target attribute.

[0082] In one implementation, it is possible to determine whether the calling function is a getting function by the function name of the calling function. Optionally, when the function name of the calling function is the target attribute name, the calling function is a getting function; when the function name of the calling function is the concatenation result of "set", "target attribute name" and ":", the calling function is an assignment function.

[0083] Taking the target attribute name as Username as an example, when the function name of the calling function is Username, the calling function is a getting function; when the function name of the calling function is setUsername:, the calling function is an assignment function.

[0084] S302. Determine whether the subclass has implemented the assignment function corresponding to the target attribute. If the subclass has implemented the assignment function corresponding to the target attribute, execute S303; if the subclass has not implemented the assignment function corresponding to the target attribute, execute S304.

[0085] It can be understood that since the subclass does not automatically generate an assignment function for the attribute, when the function is called as the assignment function, it is possible to determine whether the target attribute is called by the assignment function for the first time, that is, to determine whether there is an assignment function corresponding to the target attribute in the subclass.

[0086] S303. Call the assignment function to perform an assignment operation on the private attribute corresponding to the target attribute.

[0087] In one implementation, the set function can be called to perform an assignment operation on the private attribute corresponding to the target attribute.

[0088] S304. Call the base class to add the general assignment function as the assignment function corresponding to the target attribute, and use the general assignment function to assign a value to the private attribute corresponding to the target attribute.

[0089] In one implementation, when the target attribute of the subclass is assigned a value for the first time, if the subclass has not implemented the assignment function for the target attribute, the message forwarding mechanism is triggered. The base class can add a general assignment function for the target attribute of the subclass through the method of "+(BOOL)resolveInstanceMethod:(SEL)sel", which is equivalent to using the general assignment function as the set function of the target attribute of the subclass. Furthermore, an assignment operation can be performed on the private attribute corresponding to the target attribute based on the general assignment function.

[0090] Since the embodiment of the present application performs the set method on the target attribute rather than on the private attribute of the target attribute, it is necessary to use the runtime method to associate the target attribute with its private attribute, so as to perform an assignment operation on the private attribute corresponding to the target attribute.

[0091] By using the above method, the embodiment of the present application can complete the assignment operation on the target attribute.

[0092] See Figure 4 , the overall process of the JSON object parsing method provided by the embodiment of the present application is described as follows:

[0093] S401. Call the target attribute in the subclass corresponding to the JSON object.

[0094] S402. When the function called is the acquisition function, determine whether the subclass has implemented the get function corresponding to the target attribute. If so, execute S403; if not, execute S404.

[0095] S403. Call the get function and execute S407.

[0096] S404. Call the base class to add the general parsing function as the acquisition function corresponding to the target attribute, and obtain the target attribute name of the target attribute through the function declaration of the called function.

[0097] S405. Determine whether the target attribute is an attribute in the subclass. If so, execute S406; if not, end without parsing.

[0098] Performing subsequent steps only when it is determined that the target attribute is an attribute in the subclass can improve security.

[0099] S406. Determine whether the called function is the get function. If so, execute S407.

[0100] Since a general parsing function and a general assignment function are added to the subclass of the target attribute in the embodiment of the present application, by determining whether the called function is the get function, when it is confirmed that the called function is the get function, the subsequent steps can be parsed through the general parsing function added for the target attribute.

[0101] S407. Determine whether the private attribute corresponding to the target attribute is empty. If so, execute S408; if not, return the parsed value in the private attribute.

[0102] S408. Find the value to be parsed corresponding to the target attribute name from the dictionary to be parsed. If the value to be parsed is found, execute S409; if the value to be parsed is not found, execute S411.

[0103] S409. Determine whether the type of the value to be parsed is a dictionary type; if so, enter the object parsing process; if not, execute S410.

[0104] S410. Determine whether the type of the value to be parsed is an array type; if so, enter the array parsing process; if not, enter the string parsing process.

[0105] S411. Find the correct attribute name corresponding to the target attribute name according to the mapping relationship dictionary. If found, execute S412; if not found, end without parsing.

[0106] S412. Find the value to be parsed corresponding to the correct attribute name from the dictionary to be parsed. If found, execute S409; if not found, end without parsing.

[0107] Among them, as Figure 5 shown, the attribute setting process includes the following steps:

[0108] S501. When the target property in the subclass corresponding to the JSON object is called, and in the case where the calling function is an assignment function, determine whether the subclass has implemented the assignment function corresponding to the target property. If so, execute S502; if not, execute S503.

[0109] S502. Call the set function and execute S506.

[0110] S503. Call the base class to add the general assignment function as the assignment function corresponding to the target property, and obtain the target property name of the target property through the function declaration of the calling function.

[0111] S504. Based on the target property name, determine whether the target property is a property in the subclass. If so, execute S505; if not, end and do not perform assignment.

[0112] S505. Determine whether the calling function is a set function. If so, execute S506.

[0113] Since the general parsing function and the general assignment function are added to the subclass of the target property in the embodiments of the present application, by determining whether the calling function is a set function, it can be confirmed that the calling function is a set function, and in the subsequent steps, the general assignment function added to the target property can be used for assignment.

[0114] S506. Use the runtime method to assign a value to the private property corresponding to the target property using the general assignment function.

[0115] Among them, as Figure 6 shown, the object parsing process includes the following steps:

[0116] S601. Obtain the property type of the value to be parsed through the runtime method.

[0117] S602. Determine whether the property type of the value to be parsed is an object type. If so, execute S603; if not, end and do not perform parsing.

[0118] S603. Create an object and perform object parsing on the value to be parsed.

[0119] S604. Assign the parsed value to the private property of the target property.

[0120] Among them, as Figure 7 shown, the array parsing process includes the following steps:

[0121] S701. Obtain the type of the array elements in the value to be parsed through the runtime method.

[0122] Then, for each array element, execute S702 to S706.

[0123] S702. For each array element, determine whether the type of the array element is null. If so, execute S703; if not, execute S704.

[0124] It should be noted that the array element can be marked with its belonging type. If the array element is not marked with its belonging type, the type of the array element is null.

[0125] For example, for the array element NSArray<Student*>*student, the * in <> represents the type of the array element, that is, the array element is marked with its belonging type. For the array element NSArray*student, its belonging type is not marked.

[0126] S703. Assign the array element to the private attribute of the target attribute.

[0127] S704. Determine whether the attribute type of the value to be parsed is an object type. If so, execute 705; if not, execute S703.

[0128] S705. Execute the object parsing process on the array element to obtain the parsed value. Among them, the object parsing process is as Figure 6 shown and will not be elaborated here.

[0129] S706. Assign the parsed value to the private attribute of the target attribute.

[0130] In the related art, after obtaining the dictionary to be parsed, all JSON objects in the dictionary to be parsed are directly parsed. However, the dictionary to be parsed contains redundant fields that are not actually used, fields that are not used in the current scenario, and fields that are not temporarily used in the current scenario. Parsing these invalid fields will cause unnecessary performance loss.

[0131] In the embodiments of the present application, when it is necessary to call the target attribute corresponding to the JSON object in the subclass and the target attribute is called for the first time, the target attribute will be parsed. That is, it avoids invalid parsing of fields not used in the current scenario and repeated parsing of fields that have been parsed, thus reducing the performance loss caused by parsing JSON objects.

[0132] At the same time, for the code level, since the embodiments of the present application can only parse the fields that need to be obtained during parsing, the accuracy of parsing and the response speed of the page related to the dictionary to be parsed are improved, and the user interaction experience can be improved.

[0133] For the project level, since the embodiments of the present application eliminate the performance loss caused by centralized parsing of JSON objects included in the dictionary to be parsed, the embodiments of the present application can improve the response fluency of parsing JSON objects and further enhance the user experience.

[0134] For the solution level, since the embodiments of the present application provide a general and efficient solution for delayed parsing of JSON objects, in the parallel services of the embodiments of the present application, the JSON object parsing method provided by the present application can be used to accurately parse JSON objects, further improving work efficiency.

[0135] Based on the same inventive concept, corresponding to the above method embodiments, the embodiments of the present application provide a JSON object parsing device, as Figure 8 shown, the device includes: a judgment module 801, a parsing module 802, and an acquisition module 803.

[0136] The judgment module 801 is configured to judge whether the target attribute is called for the first time when the target attribute in the subclass corresponding to the JSON object is called; wherein, the attributes corresponding to the JSON object in the subclass are all marked with specified keywords, and the specified keywords are used to prohibit the compiler from automatically generating a getter function and a setter function for the target attribute;

[0137] The parsing module 802 is configured to, if the target attribute is called for the first time, call the base class to add the general parsing function as the getter function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed;

[0138] The acquisition module 803 is configured to, if the target attribute is not called for the first time, obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute by using the getter function corresponding to the target attribute.

[0139] Optionally, the judgment module 801 is specifically configured to:

[0140] Judge whether the subclass has implemented the getter function corresponding to the target attribute;

[0141] If so, determine that the target attribute is not called for the first time;

[0142] If not, determine that the target attribute is called for the first time.

[0143] Optionally, the parsing module 802 is specifically configured to:

[0144] Obtain the target attribute name of the target attribute through the function declaration of the calling function, and the calling function is the function that calls the target attribute;

[0145] Find the value to be parsed corresponding to the target attribute name from the dictionary to be parsed;

[0146] If the value to be parsed is found, parse the value to be parsed according to the type of the found value to be parsed.

[0147] Optionally, the device may further include: a lookup module;

[0148] The lookup module is configured to, after looking up the value to be parsed corresponding to the target attribute name from the dictionary to be parsed, if the value to be parsed is not found, find the correct attribute name corresponding to the target attribute name according to the mapping dictionary;

[0149] The parsing module 802 is further configured to find the value to be parsed corresponding to the correct attribute name from the dictionary to be parsed, and parse the value to be parsed according to the type of the found value to be parsed.

[0150] Optionally, the device may further include: an assignment module;

[0151] The determination module 801 is further configured to, before determining whether the target attribute is called for the first time, determine whether the calling function is a obtaining function, where the calling function is the function that calls the target attribute; if so, execute the step of determining whether the target attribute is called for the first time; if not, determine whether the subclass has implemented the assignment function corresponding to the target attribute;

[0152] The assignment module is configured to, if the subclass has implemented the assignment function corresponding to the target attribute, call the assignment function to perform an assignment operation on the private attribute corresponding to the target attribute;

[0153] The assignment module is further configured to, if the subclass has not implemented the assignment function corresponding to the target attribute, call the base class to add the general assignment function as the assignment function corresponding to the target attribute, and use the general assignment function to perform an assignment on the private attribute corresponding to the target attribute.

[0154] Optionally, the parsing module 802 is specifically configured to:

[0155] If the type of the value to be parsed is a dictionary type, perform object parsing on the value to be parsed; or,

[0156] If the type of the value to be parsed is an array type, perform array parsing on the value to be parsed; or,

[0157] If the type of the value to be parsed is a non-data structure type, perform string parsing on the value to be parsed.

[0158] An embodiment of the present invention further provides an electronic device, such as Figure 9As shown, it includes a processor 901, a communication interface 902, a memory 903, and a communication bus 904. Among them, the processor 901, the communication interface 902, and the memory 903 complete mutual communication through the communication bus 904.

[0159] The memory 903 is used to store computer programs.

[0160] When the processor 901 is used to execute the program stored on the memory 903, it implements the method steps in the above method embodiments.

[0161] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For the sake of representation, only a thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.

[0162] The communication interface is used for communication between the above terminal and other devices.

[0163] The memory may include a Random Access Memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located far from the aforementioned processor.

[0164] The above-mentioned processor may be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP), etc.; it may also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.

[0165] In another embodiment provided by the present invention, there is also provided a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, it implements any one of the JSON object parsing methods in the above embodiments.

[0166] In another embodiment provided by the present invention, there is also provided a computer program product containing instructions, which when running on a computer, causes the computer to execute the JSON object parsing method described in any one of the above embodiments.

[0167] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present invention are generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center by wire (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that the computer can access, or a data storage device such as a server or data center that includes one or more integrated available media. The available medium can be a magnetic medium (such as a floppy disk, hard disk, magnetic tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)).

[0168] It should be noted that in this document, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including", or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article, or device including a series of elements not only includes those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article, or device. Without further limitation, an element defined by the statement "including one..." does not exclude the existence of additional identical elements in the process, method, article, or device including the element.

[0169] Each embodiment in this specification is described in a related manner. For the same or similar parts among the embodiments, reference can be made to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the apparatus embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and reference can be made to the relevant parts of the method embodiments for the related content.

[0170] The above description is only for the preferred embodiments of the present invention and is not intended to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.

Claims

1. A method for parsing JSON objects, characterized in that, it includes: When a target attribute in a subclass corresponding to a JSON object is called, determine whether the target attribute is called for the first time; wherein, the attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating a getter function and a setter function for the target attribute; If the target attribute is called for the first time, then call the base class to add a general parsing function as the getter function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed; If the target attribute is not called for the first time, then use the getter function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute; Before determining whether the target attribute is called for the first time, the method further includes: Determine whether the calling function is a getter function, where the calling function is the function that calls the target attribute; If so, perform the step of determining whether the target attribute is called for the first time; If not, then determine whether the subclass has implemented the setter function corresponding to the target attribute; If the subclass has implemented the setter function corresponding to the target attribute, then call the setter function to perform an assignment operation on the private attribute corresponding to the target attribute.

2. The method according to claim 1, characterized in that, Determining whether the target attribute is called for the first time includes: Determine whether the subclass has implemented the getter function corresponding to the target attribute; If so, determine that the target attribute is not called for the first time; If not, determine that the target attribute is called for the first time.

3. The method according to claim 1, characterized in that, Using the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object includes: Obtain the target attribute name of the target attribute through the function declaration of the calling function, where the calling function is the function that calls the target attribute; Search for the value to be parsed corresponding to the target attribute name in the dictionary to be parsed; If the value to be parsed is found, then parse the value to be parsed according to the type of the found value to be parsed.

4. The method according to claim 3, characterized in that, After searching for the value to be parsed corresponding to the target attribute name in the dictionary to be parsed, the method further includes: If the value to be parsed is not found, then search for the correct attribute name corresponding to the target attribute name according to the mapping relationship dictionary; Search for the value to be parsed corresponding to the correct attribute name in the dictionary to be parsed, and parse the value to be parsed according to the type of the found value to be parsed.

5. The method according to claim 1, characterized in that, After determining whether the subclass has implemented the setter function corresponding to the target attribute, the method further includes: If the subclass does not implement the assignment function corresponding to the target attribute, call the base class to add the general assignment function as the assignment function corresponding to the target attribute, and use the general assignment function to assign a value to the private attribute corresponding to the target attribute.

6. The method according to claim 3 or 4, wherein, the parsing of the value to be parsed according to the type of the found value to be parsed includes: if the type of the value to be parsed is a dictionary type, perform object parsing on the value to be parsed; or, if the type of the value to be parsed is an array type, perform array parsing on the value to be parsed; or, if the type of the value to be parsed is a non-data structure type, perform string parsing on the value to be parsed.

7. A JSON object parsing device, wherein, the device includes: a judgment module, configured to judge whether the target attribute is called for the first time when a target attribute in a subclass corresponding to a JSON object is called; wherein, the attributes corresponding to the JSON object in the subclass are all marked with a specified keyword, and the specified keyword is used to prohibit the compiler from automatically generating a getter function and an assignment function for the target attribute; a parsing module, configured to if the target attribute is called for the first time, call the base class to add the general parsing function as the getter function corresponding to the target attribute, and use the general parsing function to obtain the value to be parsed corresponding to the target attribute from the dictionary to be parsed of the JSON object, and parse the value to be parsed; a obtaining module, configured to if the target attribute is not called for the first time, use the getter function corresponding to the target attribute to obtain the parsed value corresponding to the target attribute from the private attribute corresponding to the target attribute; the device further includes: an assignment module; the judgment module is further configured to, before judging whether the target attribute is called for the first time, judge whether the calling function is a getter function, and the calling function is the function that calls the target attribute; if so, execute the step of judging whether the target attribute is called for the first time; if not, judge whether the subclass has implemented the assignment function corresponding to the target attribute; the assignment module is configured to if the subclass has implemented the assignment function corresponding to the target attribute, call the assignment function to perform an assignment operation on the private attribute corresponding to the target attribute.

8. The device according to claim 7, wherein, the judgment module is specifically configured to: judge whether the subclass has implemented the getter function corresponding to the target attribute; if so, determine that the target attribute is not called for the first time; if not, determine that the target attribute is called for the first time.

9. An electronic device, wherein, it includes a processor, a communication interface, a memory and a communication bus, wherein, the processor, the communication interface and the memory complete mutual communication through the communication bus; the memory is used for storing a computer program; the processor is configured to, when executing the program stored on the memory, implement the method steps according to any one of claims 1-6.

10. A computer-readable storage medium, wherein, The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method steps described in any one of claims 1-6 are implemented.