A method and device for converting between model object and JSON object

Through the recursive algorithm and array element marking mechanism, it solves the problem that the conversion between model objects and JSON objects in the iOS system does not support nesting and model objects in arrays, and achieves an efficient and concise interconversion process.

CN110764768BActive Publication Date: 2024-09-20JINGDONG TECH HLDG CO LTD
View PDF 13 Cites 0 Cited by

Patent Information

Application Number
CN201810833719.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2018-07-26
Publication Date
2024-09-20
Estimated Expiration
2038-07-26

AI Technical Summary

Technical Problem

The conversion between model objects and JSON objects in the existing iOS system has the problem that nesting and model objects in arrays are not supported, and the existing implementation algorithm is complex and has a large amount of code, making it difficult to use.

Method used

By obtaining the attribute value of the model object and determining its type, if it is a predefined tool class instance, a recursive algorithm is used to generate a JSON object, which supports the conversion of nested model objects; for array elements, it is marked as JSONizable and converted into a JSON object. Implement the conversion of model objects within the array.

Benefits of technology

It achieves efficient mutual conversion between model objects and JSON objects, supports nesting and conversion of model objects within arrays, simplifies code logic and improves ease of use.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN110764768B
    Figure CN110764768B
Patent Text Reader

Abstract

The present invention discloses a method and device for converting between a model object and a JSON object, and relates to the field of computer technology. A specific implementation of the method includes: obtaining a predefined attribute of a model object to obtain an attribute name corresponding to the attribute; obtaining an attribute value corresponding to the attribute name from the instantiated model object; judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is an instance of the tool class, generating a JSON object by a recursive algorithm, wherein the tool class is the parent class of the model object. After obtaining the attribute value of the model object, the method judges the type of the attribute value, and if the attribute value is an instance of a predefined tool class, generates a JSON object by a recursive algorithm, thereby realizing the conversion of a model object with other model objects nested therein to a JSON object.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computers, and in particular to a method and device for converting between a model object and a JSON object. Background Art

[0002] In the existing iOS system, the conversion between model objects and JSON objects is achieved by dynamically obtaining all the attributes of the model objects through the API (Application Programming Interface) and combining some algorithms. There are two specific implementation methods: Method 1: After obtaining all the attributes, use the attribute name as the Key, call the -[NSObjectvalueForKey:] interface or call the Get method to obtain the attribute value, and store it in the dictionary (NSDictionary) in the form of Key-Value (key-value pair), and then call the +[NSJSONSerializationdataWithJSONObject:options:error:] interface to convert it to JSON data. Method 2: Use the JSONModel model framework to realize the conversion between model objects and JSON objects. Among them, the iOS system is a mobile operating system developed by Apple; JSON is a lightweight data exchange format.

[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:

[0004] Method 1 only supports the conversion of NSString attribute types, but does not support nested model objects or model objects in arrays (NSArray). Method 2 can support the above conversions, but the implementation algorithm and interface are complex, the amount of code is large, and it is not easy to use. Summary of the invention

[0005] In view of this, an embodiment of the present invention provides a method and device for converting between a model object and a JSON object, including a method and device for converting a model object into a JSON object, and a method and device for converting a JSON object into a model object. After obtaining the attribute value of the model object, the embodiment determines the type of the attribute value. If the attribute value is an instance of a predefined tool class, a recursive algorithm is used to generate a JSON object, thereby realizing the conversion of a model object with other model objects nested therein to a JSON object.

[0006] To achieve the above objective, according to one aspect of an embodiment of the present invention, a method for converting a model object into a JSON object is provided.

[0007] A method for converting a model object into a JSON object in an embodiment of the present invention includes: obtaining a predefined attribute of a model object to obtain an attribute name corresponding to the attribute; obtaining an attribute value corresponding to the attribute name from the instantiated model object; judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is an instance of the tool class, generating a JSON object through a recursive algorithm, wherein the tool class is a parent class of the model object.

[0008] Optionally, after the step of obtaining the attribute value corresponding to the attribute name from the instantiated model object, it also includes: determining whether the attribute value is an array; if the attribute value is an array and the array element of the array is marked as a JSON-able model object, determining whether the array element is an instance of the model object; when the array element of the array is an instance of the model object, converting the array element into the JSON object.

[0009] Optionally, the method further includes: marking the array elements as JSON-capable model objects; using the class name of the model object and the property name as keys and storing the array elements as values ​​to determine whether the array elements of the array are marked as JSON-capable model objects.

[0010] Optionally, obtaining the attribute name corresponding to the attribute includes: obtaining the attribute type corresponding to the attribute, and determining whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, determining whether the class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, obtaining the class name corresponding to the attribute type, and determining whether the class name corresponding to the attribute type is a subclass of the tool class; if the class name corresponding to the attribute type is not a subclass of the tool class, determining whether the class name corresponding to the attribute type is an Object-C base class; when the class of the attribute type is not an Object-C class, or the class name corresponding to the attribute type is a subclass of the tool class, or the class name corresponding to the attribute type is the Object-C base class, obtaining the attribute name corresponding to the attribute.

[0011] To achieve the above objective, according to another aspect of an embodiment of the present invention, a method for converting a JSON object into a model object is provided.

[0012] A method for converting a JSON object into a model object in an embodiment of the present invention includes: obtaining the type of a key value of the JSON object, and determining whether the type of the key value is a dictionary; if the type of the key value is a dictionary, obtaining a predefined attribute of a model object to find the attribute type associated with the key name of the JSON object; obtaining an original key value of the JSON object, and if the original key value is empty, creating an instance of a class corresponding to the attribute type, setting the attribute name of the instance to the key name, and setting the attribute value corresponding to the attribute name to a preset key value.

[0013] Optionally, the method further includes: determining whether the type of the key value is an array; if the type of the key value is an array and the array element of the array is marked as a JSON-able model object, creating an instance of the model object; setting the property name of the instance to the key name, and setting the property value of the instance to the value of the array element.

[0014] Optionally, obtaining the original key value of the JSON object includes: determining whether the attribute type is a subclass of the model object, and if the attribute type is a subclass of the model object, obtaining the original key value of the JSON object; the method also includes: determining whether the original key value is empty, and if the original key value is not empty, updating the original key value to a preset key value; setting the attribute name to the key name, and setting the attribute value to the original key value.

[0015] To achieve the above objective, according to another aspect of an embodiment of the present invention, a device for converting a model object into a JSON object is provided.

[0016] An apparatus for converting a model object into a JSON object according to an embodiment of the present invention comprises: a first acquisition module, used for acquiring a predefined attribute of a model object to obtain an attribute name corresponding to the attribute; a second acquisition module, used for acquiring an attribute value corresponding to the attribute name from the instantiated model object; and a generation module, used for determining whether the attribute value is an instance of a predefined tool class, and if the attribute value is an instance of the tool class, generating a JSON object by a recursive algorithm, wherein the tool class is a parent class of the model object.

[0017] Optionally, the device also includes: a first processing module, used to determine whether the attribute value is an array, and if the attribute value is an array and the array element of the array is marked as a JSON-convertible model object, determine whether the array element is an instance of the model object; and when the array element of the array is an instance of the model object, convert the array element into the JSON object.

[0018] Optionally, the device also includes: a marking judgment module, used to mark the array elements as JSON-convertible model objects; and using the class name of the model object and the attribute name as keys and storing the array elements as values ​​to determine whether the array elements of the array are marked as JSON-convertible model objects.

[0019] Optionally, the first acquisition module is further used to: obtain the attribute type corresponding to the attribute, and determine whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, determine whether the class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, obtain the class name corresponding to the attribute type, and determine whether the class name corresponding to the attribute type is a subclass of the tool class; if the class name corresponding to the attribute type is not a subclass of the tool class, determine whether the class name corresponding to the attribute type is an Object-C base class; and when the class of the attribute type is not an Object-C class, or the class name corresponding to the attribute type is a subclass of the tool class, or the class name corresponding to the attribute type is the Object-C base class, obtain the attribute name corresponding to the attribute.

[0020] To achieve the above objective, according to another aspect of an embodiment of the present invention, a device for converting a JSON object into a model object is provided.

[0021] An apparatus for converting a JSON object into a model object according to an embodiment of the present invention comprises: a judgment module, used to obtain the type of a key value of a JSON object, and judge whether the type of the key value is a dictionary; a search module, used to obtain the attributes of a predefined model object if the type of the key value is a dictionary, so as to find the attribute type associated with the key name of the JSON object; and a first setting module, used to obtain the original key value of the JSON object, and if the original key value is empty, create an instance of a class corresponding to the attribute type, so as to set the attribute name of the instance to the key name, and set the attribute value corresponding to the attribute name to the preset key value.

[0022] Optionally, the device also includes: a second processing module, used to determine whether the type of the key value is an array, if the type of the key value is an array and the array element of the array is marked as a JSON-able model object, then creating an instance of the model object; and setting the attribute name of the instance to the key name, and setting the attribute value of the instance to the value of the array element.

[0023] Optionally, the first setting module is also used to: determine whether the attribute type is a subclass of the model object, and if the attribute type is a subclass of the model object, obtain the original key value of the JSON object; the device also includes: a second setting module, used to determine whether the original key value is empty, and if the original key value is not empty, update the original key value to a preset key value; and set the attribute name to the key name, and set the attribute value to the original key value.

[0024] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.

[0025] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a method for converting a model object into a JSON object according to an embodiment of the present invention.

[0026] To achieve the above objective, according to another aspect of the embodiments of the present invention, a computer readable medium is provided.

[0027] A computer-readable medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, a method for converting a model object into a JSON object according to an embodiment of the present invention is implemented.

[0028] To achieve the above objective, according to another aspect of an embodiment of the present invention, an electronic device is provided.

[0029] An electronic device according to an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a method for converting a JSON object into a model object according to an embodiment of the present invention.

[0030] To achieve the above objective, according to another aspect of the embodiments of the present invention, a computer readable medium is provided.

[0031] A computer-readable medium according to an embodiment of the present invention stores a computer program, and when the program is executed by a processor, a method for converting a JSON object into a model object according to an embodiment of the present invention is implemented.

[0032] One embodiment of the above invention has the following advantages or beneficial effects: after obtaining the attribute value of the model object, the type of the attribute value is determined. If the attribute value is an instance of a predefined tool class, a recursive algorithm is used to generate a JSON object, thereby realizing the conversion of a model object nested with other model objects to a JSON object; when the attribute value is an array, and the array element is marked as a JSON-capable model object and is an instance of the model object, the array element is converted to a JSON object, thereby realizing the conversion of an array element from a custom model object to a JSON object; the array elements are pre-marked as JSON-capable model objects and stored as key-value pairs, thereby facilitating the determination of whether the array meets the conditions for conversion; setting the acquisition of attributes The condition of the property name is set, so that the property name is obtained when the condition is met; when the key value type of the JSON object is a dictionary, the property type associated with the key name of the JSON object is found to create an instance of the property type, and then the property name of the instance is set to the key name and the property value is set to the key value, thereby realizing the conversion of the JSON object with the key value type of a dictionary to the model object; when the key value is an array and the array element is marked as a JSONizable model object, an instance of the model object is created to realize the conversion of the JSON object with the key value type of an array to the model object; the condition of obtaining the original key value is set, and the original key value is obtained when the condition is met, so that the original key value is updated when the obtained original key value is not empty.

[0033] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.

[0035] Figure 1 is a schematic diagram of main steps of a method for converting a model object into a JSON object according to an embodiment of the present invention;

[0036] Figure 2 is a schematic diagram of main steps of a method for converting a JSON object into a model object according to an embodiment of the present invention;

[0037] Figure 3 is a main flow chart of a method for converting a model object into a JSON object according to an embodiment of the present invention;

[0038] Figure 4 is a schematic diagram of the result of converting a model object into a JSON object according to an embodiment of the present invention;

[0039] Figure 5 is a schematic diagram of a main flow chart of a method for converting a JSON object into a model object according to an embodiment of the present invention;

[0040] Figure 6 is a schematic diagram of the result of converting a JSON object into a model object according to an embodiment of the present invention;

[0041] Figure 7 is a schematic diagram of main modules of an apparatus for converting a model object into a JSON object according to an embodiment of the present invention;

[0042] Figure 8 is a schematic diagram of main modules of an apparatus for converting a JSON object into a model object according to an embodiment of the present invention;

[0043] Fig. 9 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;

[0044] Fig.10 It is a schematic diagram of the structure of a computer device suitable for implementing the electronic device of the embodiment of the present invention. DETAILED DESCRIPTION

[0045] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0046] Figure 1 Schematic diagram of the main steps of a method for converting a model object into a JSON object according to an embodiment of the present invention. The dictionary and array involved in the embodiment of the present invention are both data types in the Object-C language. Figure 1 As shown, the method for converting a model object into a JSON object in an embodiment of the present invention mainly includes the following steps:

[0047] Step S101: Get the attributes of the predefined model object to obtain the attribute name corresponding to the attribute. The model object is a custom tool class or a subclass of the tool class, including one or more attributes, and the attribute has one or more attribute types, such as int, long, NSString, a custom model object or its subclass, an array whose elements are custom model objects, etc. In this step, call the class_copyPropertyList interface to obtain the attributes of the model object; then call the property_getName interface to obtain the attribute name corresponding to the attribute.

[0048] Step S102: Get the attribute value corresponding to the attribute name from the instantiated model object. Instantiate the model object, then call the -[NSObjectdictionaryWithValuesForKeys:] interface to get the attribute value corresponding to the attribute name. Then, determine whether the attribute value is an instance of a tool class, an array, or other types, and perform different processing according to the determination result.

[0049] Step S103: Determine whether the attribute value is an instance of a predefined tool class. If the attribute value is an instance of the tool class, generate a JSON object through a recursive algorithm. In the embodiment, the tool class is the JDPayJSONModel class. The JDPayJSONModel class inherits from the NSObject class, and has only about 280 lines of code. The class provides a -[JDPayJSONModel toJSONDictionary] interface for converting a model object into a JSON object; provides a JSONizable array property marking interface +[JDPayJSONModelsetJSONAbleElementClass:forArrayProperty:] for marking an element in an array class property as a JSONizable JDPayJSONModel type; implements the conversion from a JSON object to a model object by overloading the -[NSObject setValue:forKey:] interface of its parent class, and implements the conversion from a JSON object to a model object by calling the -[NSObject setValuesForKeysWithDictionary:] interface. When using it, users only need to create a subclass of the JDPayJSONModel class to realize the interchange between model objects and JSON objects. This step is used to determine whether the attribute value is nested with a model object. If so, the nested model object is converted into a JSON object by using a recursive algorithm. Afterwards, the JSON object can be stored in a dictionary in the form of a key-value pair. Through steps S101 to S103, the purpose of converting the nested model objects one by one into JSON objects is achieved.

[0050] Figure 2 FIG. 1 is a schematic diagram of the main steps of a method for converting a JSON object into a model object according to an embodiment of the present invention. Figure 2 As shown, the method for converting a JSON object into a model object in an embodiment of the present invention mainly includes the following steps:

[0051] Step S201: Get the type of the key value of the JSON object, and determine whether the type of the key value is a dictionary. Among them, the JSON object refers to the dictionary data type in the Object-C language, and the JSON object can be converted into standard JSON string data through the API. The data format of the JSON object is: key name / key value pair, the key name / key value pair includes the field name and the corresponding value, the field name and value are both in double quotes, and the two are separated by ":", such as "firstName":"John". The type of the key value can be a string, a number, a dictionary, an array, etc. Call the -[NSObject isKindOfClass:] interface to obtain the type of the key value of the pre-defined JSON object.

[0052] Step S202: If the type of the key value is a dictionary, obtain the predefined model object's properties to find the property type associated with the key name of the JSON object. Call the class_copyPropertyList interface to obtain all the properties of the model object. In the embodiment, the model object is the JDPayJSONModel class. The property name of the model object with the same key name is found by enumeration, and the property type corresponding to the property name is the property type associated with the key name.

[0053] Step S203: Get the original key value of the JSON object. If the original key value is empty, create an instance of the class corresponding to the attribute type, set the attribute name of the instance to the key name, and set the attribute value corresponding to the attribute name to the preset key value. In this step, first determine whether the attribute type is a subclass of the model object. If the attribute type is a subclass of the model object, call the valueForKey interface to obtain the original key value of the JSON object. Then, determine whether the original key value is empty (nil). If so, create an instance of the class corresponding to the attribute type, call the setValue:forKey: interface to set the attribute name to the key name of the JSON object, and set the attribute value corresponding to the attribute name to the preset key value.

[0054] Figure 3 FIG. 1 is a schematic diagram of the main flow of a method for converting a model object into a JSON object according to an embodiment of the present invention. Figure 3 As shown, the method of converting a model object into a JSON object relies on the interface -[JDPayJSONModeltoJSONDictionary]. The implementation of this interface mainly includes the following steps:

[0055] Step S301: define the model object, the attribute types not supported by the model object, and the supported basic classes. In the embodiment, the model object is the JDPayJSONModel class or its subclass, the JDPaJSONModel class is a subclass of NSObject, the model object includes one or more attributes, and the attributes have one or more attribute types. In the embodiment, the model object is defined using the Object-C language: the OJSONModel class. The OJSONModel class defined in this example is a subclass of the JDPayJSONModel class, specifically:

[0056] @interface OJSONModel:JDPayJSONModel

[0057] @property(nonatomic,strong)NSString*string;

[0058] @property(nonatomic,strong)NSMutableString*mutableString;

[0059] @property(nonatomic,strong)NSNumber*number;

[0060] @property(nonatomic,strong)NSDecimalNumber*decimalNumber;

[0061] @property(nonatomic,strong)NSDictionary*dictionary;

[0062] @property(nonatomic,strong)NSDictionary*mutableDictionary;

[0063] @property(nonatomic,strong)NSArray*array;

[0064] @property(nonatomic,strong)NSMutableArray*mutableArray;

[0065] @end

[0066] During initialization, define the attribute type prefixes that the model object does not support, such as pointer: "T^", id type: "T@," , structure: "T{", union type: "T(", etc. Similarly, during initialization, define the Object-C basic classes supported by the model object, such as NSString.class (string class), NSMutableString.class (mutable string class), NSDecimalNumber.class (decimal number class), NSNumber.class (number class), NSMutableDictionary.class (mutable dictionary class), NSDictionary.class (immutable dictionary class), NSArray.class (immutable array class), NSMutableArray.class (mutable array class), etc.

[0067] Step S302: Obtain the properties of the model object. In the embodiment, all properties of the model object are obtained by calling the class_copyPropertyList interface. The properties are the @property statements defined in the model object, that is, the OJSONModel class includes eight properties.

[0068] Step S303: Determine whether there are unenumerated attributes, if yes, execute step S304; if no, end this process. Place all the attributes acquired in step S302 into an attribute array to traverse each attribute in the attribute array.

[0069] Step S304: Enumerate the attributes, and execute step S305. Enumeration means traversing each attribute in the attribute array.

[0070] Step S305: Get the attribute type of the current attribute. In the embodiment, the attribute type is obtained by calling the property_getAttributes interface. The attribute type corresponds to NSString (string), NSMutableString (mutable string), NSNumber (number), NSDecimalNumber (decimal number), NSDictionary (dictionary) and NSArray (array) in the OJSONModel class.

[0071] Step S306: Determine whether the current attribute type is a preset supported attribute type. If yes, execute step S307; if not, discard the corresponding attribute and execute step S304. After obtaining the attribute type of the current attribute, use the attribute type as the current attribute type, and then compare the current attribute type with the defined unsupported attribute type. If the obtained attribute type is an unsupported attribute type, such as a pointer, discard the attribute; if the unsupported attribute type does not include the current attribute type, the attribute type is a supported attribute type.

[0072] Step S307: Determine whether the class corresponding to the current attribute type is an Object-C class. If so, obtain the class name corresponding to the current attribute type and execute step S308; if not, execute step S310. This step determines whether the attribute prefix to which the current attribute type belongs is "T@". If so, the class corresponding to the current attribute type is an Object-C class. After obtaining the attribute type, the NSClassFromString interface can be called to generate the class corresponding to the attribute type.

[0073] Step S308: Determine whether the class corresponding to the current attribute type is a subclass of the JDPayJSONModel class. If yes, execute step S310; if not, execute step S309. In the embodiment, based on the obtained class name corresponding to the current attribute type, the +[Class(BOOL)isSubclassOfClass:(Class)aClass] interface is called to determine whether the class corresponding to the current attribute type is a subclass of the JDPayJSONModel class.

[0074] Step S309: Determine whether the class corresponding to the current attribute type is a supported Objective-C basic class. If yes, execute step S310; if not, execute step S304. Check whether the class name corresponding to the current attribute type exists in the basic class defined in step S301. If yes, the class corresponding to the current attribute type is a supported basic class.

[0075] Step S310: Get the property name of the current property. In the embodiment, the property name is obtained by calling the property_getName interface, and the property name corresponds to string, mutableString, number, decimalNumber, dictionary, mutableDictionary, array, and mutableArray in the OJSONModel class of the embodiment.

[0076] Step S311: Obtain the attribute value corresponding to the current attribute name from the instantiated model object. After obtaining the attribute name of the current attribute, use the attribute name as the current attribute name. In the embodiment, the attribute value is obtained by calling the [NSObjectdictionaryWithValuesForKeys:] interface.

[0077] Step S312: Determine whether the current attribute value is an instance of the JDPayJSONModel class, if yes, execute step S313; if no, execute step S315. After obtaining the value of the current attribute, use the value as the current attribute value.

[0078] Step S313: Generate a JSON object corresponding to the current attribute. The JSON object is an Object-C object with a return value of YES obtained by calling the +[NSJSONSerialization isValidJSONObject:] interface, that is, the object can be converted into standard JSON string data using the iOS system API, but the object must be a dictionary and the Key of the dictionary must be a string, and the elements in the dictionary must be NSString, NSMutableString, NSNumber, NSDecimalNumber, NSDictionary, NSMutableDictionary (variable dictionary), NSArray, and NSMutableArray (variable array). In the embodiment, a recursive algorithm is used to call the toJSONDictionary interface to generate a JSON object corresponding to the current attribute. In the computer field, recursion refers to a method of solving a problem by repeatedly decomposing the problem into similar sub-problems. The similar problem here is to call the same toJSONDictionary interface on different objects to convert the nested model object (JDPayJSONModel) into a JSON object.

[0079] Step S314: Determine whether the generated JSON object is nil, if yes, execute step S304; if no, execute step S324. Among them, nil is a type, it has only one value nil, indicating no value, and its main function is to distinguish any other values.

[0080] Step S315: Determine whether the current attribute value is an array and the array elements of the array are marked as a JSON-able JDPayJSONModel class or its subclass. If yes, execute step S316; if not, execute step S324. In the embodiment, the [value isKindOfClass:NSArray.class] interface is called to determine whether the current attribute value is an array. During initialization, the setJSONAbleElementClass:(Class)elementClassforArrayProperty:(NSString*)propertyName interface is called to mark the elements in the array type attribute as a JSON-able JDPayJSONModel class or its subclass. When marking, the current class name and property name are used as the key, and the passed-in elementClass is used as the value and saved in the global dictionary. When calling setValue:V forKey:K on an object, the class name of the current object is known, and the property name is K, that is, with the class name and property name, it is possible to find out whether the array element is marked by searching the global dictionary.

[0081] Step S316: Create a JSON object array. In the embodiment, the JSON object array can be represented by newValues, and newValues ​​is used to store the JSON object generated by each element in the array of step S315.

[0082] Step S317: Determine whether the array has any unenumerated array elements, if so, execute step S318; otherwise, execute step S323.

[0083] Step S318: Enumerate the array elements of the array and execute step S319.

[0084] Step S319: Determine whether the current array element is an instance of a marked class, if yes, execute step S320; if no, execute step S317. The marked class is the JSON-able JDPayJSONModel class or its subclass that the array element is marked as.

[0085] Step S320: Generate the JSON object corresponding to the current array element. In the embodiment, the -[JDPayJSONModel toJSONDictionary] interface is called to generate the JSON object corresponding to the current array element.

[0086] Step S321: Determine whether the generated JSON object is nil, if so, execute step S317; if not, execute step S322.

[0087] Step S322: Add the generated JSON object to the JSON object array, and execute step S317, that is, add the JSON object corresponding to the current array element to newValues.

[0088] Step S323: Determine whether the JSON object array is empty, if yes, execute step S304; if no, execute step S324, that is, determine whether the JSON object array newValues ​​is empty.

[0089] Step S324: Store the JSON object in a dictionary in the form of a key-value pair. The dictionary is a data structure that includes many built-in functions. The dictionary is a data container for key-value pairs. The key-value pairs (key: value) are separated by colons ":", and the key-value pairs are separated by commas ",". The entire dictionary is included in curly braces "{}", and the keys and values ​​correspond one to one. This step stores all JSON objects in the dictionary in the form of Key (key name)-Value (key value).

[0090] In the embodiment, two model objects are defined, namely OJSONModel class and PSONModel class, both of which are subclasses of JDPayJSONModel class. The definition of OJSONModel class is as described above, and the definition of PSONModel class is:

[0091] @interface PJSONModel:JDPayJSONModel

[0092] @property(nonatomic,assign)char c;

[0093] @property(nonatomic,assign)int intValue;

[0094] @property(nonatomic,assign)short shortValue;

[0095] @property(nonatomic,assign)long longValue;

[0096] @property(nonatomic,assign)long long longLongValue;

[0097] @property(nonatomic,assign)unsigned char uc;

[0098] @property(nonatomic,assign)unsigned int unsignedIntValue;

[0099] @property(nonatomic,assign)unsigned short unsignedShortValue;

[0100] @property(nonatomic,assign)unsigned long unsignedLongValue;

[0101] @property(nonatomic,assign)unsigned long long unsignedLongLongValue;

[0102] @property(nonatomic,assign)float floatValue;

[0103] @property(nonatomic,assign)double doubleValue;

[0104] @property(nonatomic,assign)BOOL boolValue;

[0105] / / Define a member variable named oms, whose type is array, and the array element is a custom OJSONModel model object

[0106] @property(nonatomic,strong)NSArray<OJSONModel*> *oms;

[0107] / / Define a member variable named om, whose type is a custom OJSONModel model object

[0108] @property(nonatomic,strong)OJSONModel*om;

[0109] @end

[0110] Instantiate the JSONModel class and the PSONModel class respectively. The instantiation results are:

[0111] / / Mark the elements in the oms array as OJSONModel classes that can be JSONified

[0112] [PJSONModel setJSONAbleElementClass:OJSONModel.classforArrayProperty:@"oms"];

[0113] / / sore

[0114] OJSONModel*om=[OJSONModel new];

[0115] about.string=@"string";

[0116] om.mutableString=[NSMutableString stringWithString:@"mutableString"];

[0117] om.number=[NSNumber numberWithInteger:11];

[0118] about.decimalNumber=[NSDecimalNumber decimalNumberWithString:@"12"];

[0119] om.dictionary=@{@"key":@"value"};

[0120] om.mutableDictionary=[NSMutableDictionary dictionaryWithDictionary:om.dictionary];

[0121] about.array=@[@1,@2,@3];

[0122] om.mutableArray=[NSMutableArray arrayWithArray:om.array];

[0123] / / pm

[0124] PJSONModel*pm=[PJSONModel new];

[0125] pm.c='c';

[0126] pm.intValue=1;

[0127] pm.shortValue=2;

[0128] pm.longValue = 3;

[0129] pm.longLongValue = 4;

[0130] pm.uc = 'C';

[0131] pm.unsignedIntValue = 5u;

[0132] pm.unsignedShortValue = 6u;

[0133] pm.unsignedLongValue = 7u;

[0134] pm.unsignedLongLongValue=8u;

[0135] pm.floatValue = 9.f;

[0136] pm.doubleValue = 10.0;

[0137] pm.boolValue = YES;

[0138] pm.oms = @[om];

[0139] pm.om=om;

[0140] / / Call the toJSONDictionary interface to convert the model object pm to the JSON object pmd

[0141] NSDictionary*pmd=[pm toJSONDictionary];

[0142] Figure 4 FIG. 1 is a schematic diagram of the result of converting a model object into a JSON object according to an embodiment of the present invention. Figure 4As shown, according to the above-defined and implemented JSONModel class and PSONModel class, and according to the method for converting a model object into a JSON object according to the embodiment of the present invention, the model object pm is finally converted into a JSON object pmd. The conversion process supports all original data types (char, int, short, long, long long, unsigned x, double, float, CGFloat, NSInteger, NSUInteger, etc.), supports Object-C basic data types (NSString, NSMutableString, NSNumber, NSDecimalNumber, NSDictionary, NSMutableDictionary, NSArray, NSMutableArray), supports nested Object-C model objects, and supports Object-C model objects in arrays (NSArray).

[0143] It can be seen from the method for converting a model object into a JSON object in an embodiment of the present invention that after obtaining the attribute value of the model object, the type of the attribute value is determined. If the attribute value is an instance of a predefined tool class, a recursive algorithm is used to generate a JSON object, thereby realizing the conversion of a model object nested with other model objects to a JSON object; when the attribute value is an array, and the array element is marked as a JSON-capable model object and is an instance of the model object, the array element is converted into a JSON object, thereby realizing the conversion of an array element that is a custom model object to a JSON object; the array elements are pre-marked as JSON-capable model objects and stored as key-value pairs to facilitate determination of whether the array meets the conditions for conversion; and the conditions for obtaining the attribute name are set to obtain the attribute name when the conditions are met.

[0144] Figure 5 FIG. 1 is a schematic diagram of the main flow of a method for converting a JSON object into a model object according to an embodiment of the present invention. Figure 5 As shown in the figure, the method of converting a JSON object into a model object relies on the core interface -[NSObjectsetValue:forKey:], which mainly includes the following steps:

[0145] Step S501: Obtain the type of the key value of the JSON object. In the embodiment, the type of the key value V of the preset JSON object is obtained by calling the -[NSObjectisKindOfClass:] interface, and the type of the key value V may be a string, a value, a dictionary, an array, etc.

[0146] Step S502: Determine whether the type of the key value is a dictionary, if it is a dictionary, execute step S503; if it is not a dictionary, execute step S512. In the embodiment, the [V isKindOfClass:NSDictionary.class] interface is called to determine whether the type of the key value is a dictionary.

[0147] Step S504: Get the attributes of the defined model object, and enumerate and search for the attributes associated with the current key name. Get all the attributes of the model object, and find the attribute name of the model object with the same key name by enumeration. The attribute corresponding to the attribute name is the attribute associated with the current key name.

[0148] Step S505: Determine whether the attribute associated with the current key name is found, if so, execute step S506; if not, execute step S519.

[0149] Step S506: Acquire the attribute type of the attribute associated with the current key name.

[0150] Step S507: Determine whether the class corresponding to the attribute type is a subclass of the JDPayJSONModel class, if yes, execute step S508; if not, execute step S519. For example, the attribute type of the defined PJSONModel class includes OJSONModel, and the OJSONModel class is a subclass of the JDPayJSONModel class, the parent class of PJSONModel.

[0151] Step S508: Obtain the original key value of the current key name. In the embodiment, the valueForKey interface is called to obtain the original key value OV of the current key name.

[0152] Step S509: Determine whether the original key value is nil, if yes, execute step S510; if no, execute step S511.

[0153] Step S510: Create an instance of the class corresponding to the attribute type and initialize it, and then execute step S512. Create an instance TI of the class corresponding to the attribute type, and call the -[NSObjectsetValuesForKeysWithDictionary:] interface to initialize TI with the key value V, corresponding to the statement PJSONModel*pmo=[[PJSONModel alloc]initWithDictionary:pmd];.

[0154] Step S511: Update the original key value, and execute step S512. In the embodiment, call -[NSObjectsetValuesForKeysWithDictionary:] interface to update the original key value from OV to V.

[0155] Step S512: Set the attribute value for the attribute associated with the current key name, and end this process. In the embodiment, call the setValue:forKey: interface to set the attribute name to the key name K, set the attribute value of the attribute associated with the current key name to TI or OV, and then mark the current key name as processed.

[0156] Step S513: Determine whether the type of the key value is an array, if it is an array, execute step S514; if it is not an array, execute step S519. In the embodiment, the [V isKindOfClass:NSArray.class] interface is called to determine whether the type of the key value is an array.

[0157] Step S514: Determine whether the array elements in the array are marked as JSON-capable JDPayJSONModel class or its subclass. If so, execute step S515; if not, execute step S519. During initialization, call the setJSONAbleElementClass:(Class)elementClass forArrayProperty:(NSString*)propertyName interface to mark the elements in the array type property as JSON-capable JDPayJSONModel class or its subclass. When marking, use the current class name and property name as the key, the passed-in elementClass as the value, and save them in the global dictionary. When calling setValue:VforKey:K on an object, the class name of the current object is known, and the property name is K, that is, with the class name and property name, you can then search the global dictionary to find out whether the array element is marked.

[0158] Step S515: Create a new array. The new array newValues1 is used to store the instance OI of the Object-C model object converted from the JSON object in the array represented by V.

[0159] Step S516: Enumerate the array elements of the array, create an instance of the model object, initialize the instance and add it to the new array. In the embodiment, enumerate the array element E in the key value V of the array type, create an instance OI of the JDPayJSONModel class, call the -[NSObject setValuesForKeysWithDictionary:] interface to initialize OI with the array element E and add it to the new array newValues1.

[0160] Step S517: Determine whether the new array is empty, if not, execute step S518; if yes, execute step S519, that is, determine whether the new array newValues1 is empty.

[0161] Step S518: Assign the new array to the key value corresponding to the current key name, and execute step S519. Assign the new array newValues1 to the key value V.

[0162] Step S519: Set the attribute name of the attribute to the key name and the attribute value to the key value, and end this process. Call the -[NSObject setValue:forKey:] interface to set the attribute name to the key name of the JSON object and the attribute value corresponding to the attribute name to the key value of the JSON object.

[0163] In the embodiment, two model objects are also defined, namely OJSONModel class and PSONModel class, both of which are subclasses of JDPayJSONModel class. The definitions of OJSONModel class and PSONModel class are as described above. JSONModel class and PSONModel class are instantiated respectively. The instantiation result is as described above, and then the JSON object is converted to a model object through the PJSONModel*pmo=[[PJSONModel alloc]initWithDictionary:pmd] statement.

[0164] Figure 6 FIG. 1 is a schematic diagram of the result of converting a JSON object into a model object according to an embodiment of the present invention. Figure 6As shown, according to the method for converting a JSON object into a model object according to an embodiment of the present invention, the JSON object pmd is finally converted into a model object pmo. The conversion process also supports all original data types (char, int, short, long, long long, unsignedx, double, float, CGFloat, NSInteger, NSUInteger, etc.), supports Object-C basic data types (NSString, NSMutableString, NSNumber, NSDecimalNumber, NSDictionary, NSMutableDictionary, NSArray, NSMutableArray), supports nested Object-C model objects, and supports Object-C model objects in arrays (NSArray).

[0165] It can be seen from the method for converting a JSON object to a model object in an embodiment of the present invention that, when the type of the key value of the JSON object is a dictionary, the attribute type associated with the key name of the JSON object is searched to create an instance of the attribute type, and then the attribute name of the instance is set to the key name, and the attribute value is set to the key value, thereby realizing the conversion of the JSON object whose key value type is a dictionary to the model object; when the key value is an array and the array element is marked as a JSONizable model object, an instance of the model object is created to realize the conversion of the JSON object whose key value type is an array to the model object; and a condition for obtaining the original key value is set, and the original key value is obtained when the condition is met, so as to update the original key value when the obtained original key value is not empty.

[0166] Figure 7 Schematic diagram of main modules of an apparatus for converting a model object into a JSON object according to an embodiment of the present invention. Figure 7 As shown, the device 700 for converting a model object into a JSON object according to an embodiment of the present invention mainly includes:

[0167] The first acquisition module 701 is used to acquire the properties of the predefined model object to obtain the property name corresponding to the property. The model object is a custom tool class or a subclass of the tool class, including one or more properties, and the property has one or more property types, such as int, long, NSString, a custom model object or its subclass, an array whose elements are custom model objects, etc. In this step, the class_copyPropertyList interface is called to acquire the properties of the model object; then, the property_getName interface is called to acquire the property name corresponding to the property.

[0168] The second acquisition module 702 is used to obtain the attribute value corresponding to the attribute name from the instantiated model object. The model object is instantiated, and then the -[NSObjectdictionaryWithValuesForKeys:] interface is called to obtain the attribute value corresponding to the attribute name. It is subsequently necessary to determine whether the attribute value is an instance of a tool class, an array, or other types, so as to perform different processing according to the determination result.

[0169] The generation module 703 is used to determine whether the attribute value is an instance of a predefined tool class. If the attribute value is an instance of the tool class, a JSON object is generated by a recursive algorithm, wherein the tool class is the parent class of the model object. In the embodiment, the tool class is the JDPayJSONModel class. The JDPayJSONModel class inherits from the NSObject class, and has only about 280 lines of code inside. The class provides a -[JDPayJSONModeltoJSONDictionary] interface for converting a model object into a JSON object; provides a JSONizable array property marking interface +[JDPayJSONModelsetJSONAbleElementClass:forArrayProperty:] for marking an element in an array class property as a JSONizable JDPayJSONModel type; realizes the conversion from a JSON object to a model object by overloading the -[NSObject setValue:forKey:] interface of its parent class, and realizes the conversion from a JSON object to a model object by calling the -[NSObject setValuesForKeysWithDictionary:] interface. When using it, the user only needs to create a subclass of the JDPayJSONModel class to realize the interchange between the model object and the JSON object. This step is used to determine whether the attribute value is nested with a model object. If so, the nested model object is converted into a JSON object by using a recursive algorithm. After that, the JSON object can be stored in a dictionary in the form of a key-value pair. The purpose of converting nested model objects one by one into JSON objects is achieved through the above modules.

[0170] In addition, the device 700 for converting a model object into a JSON object according to the embodiment of the present invention may further include: a first processing module and a tag determination module ( Figure 7(not shown in the figure). The first processing module is used to determine whether the attribute value is an array. If the attribute value is an array and the array element of the array is marked as a JSON-convertible model object, it is used to determine whether the array element is an instance of the model object; and when the array element of the array is an instance of the model object, the array element is converted to the JSON object. The marking judgment module is used to mark the array element as a JSON-convertible model object; the class name of the model object and the attribute name are used as keys, and the array element is used as a value to store, so as to determine whether the array element of the array is marked as a JSON-convertible model object.

[0171] From the above description, it can be seen that after obtaining the attribute value of the model object, the type of the attribute value is determined. If the attribute value is an instance of a predefined tool class, a recursive algorithm is used to generate a JSON object, thereby realizing the conversion of a model object nested with other model objects to a JSON object; when the attribute value is an array, and the array element is marked as a JSON-capable model object and is an instance of the model object, the array element is converted to a JSON object, thereby realizing the conversion of an array element that is a custom model object to a JSON object; the array elements are pre-marked as JSON-capable model objects and stored as key-value pairs to facilitate determination of whether the array meets the conditions for conversion; the conditions for obtaining the attribute name are set to obtain the attribute name when the condition is met.

[0172] Figure 8 Schematic diagram of main modules of an apparatus for converting a JSON object into a model object according to an embodiment of the present invention. Figure 8 As shown, the device 800 for converting a JSON object into a model object according to an embodiment of the present invention mainly includes:

[0173] Judgment module 801 is used to obtain the type of the key value of the JSON object and determine whether the type of the key value is a dictionary. Among them, the JSON object refers to the dictionary data type in the Object-C language, and the JSON object can be converted into standard JSON string data through the API. The data format of the JSON object is: key name / key value pair, the key name / key value pair includes the field name and the corresponding value, the field name and value are both in double quotes, and the two are separated by ":", such as "firstName":"John". The type of the key value can be a string, a number, a dictionary, an array, etc. Call the -[NSObject isKindOfClass:] interface to obtain the type of the key value of the pre-defined JSON object.

[0174] The search module 802 is used to obtain the properties of the predefined model object if the type of the key value is a dictionary, so as to find the property type associated with the key name of the JSON object. The class_copyPropertyList interface is called to obtain all the properties of the model object. In the embodiment, the model object is the JDPayJSONModel class. The property name of the model object with the same key name is found by enumeration, and the property type corresponding to the property name is the property type associated with the key name.

[0175] The first setting module 803 is used to obtain the original key value of the JSON object. If the original key value is empty, an instance of the class corresponding to the attribute type is created to set the attribute name of the instance to the key name, and the attribute value corresponding to the attribute name is set to the preset key value. This module first determines whether the attribute type is a subclass of the model object. If the attribute type is a subclass of the model object, the valueForKey interface is called to obtain the original key value of the JSON object. After that, it is determined whether the original key value is empty (nil). If so, an instance of the class corresponding to the attribute type is created, and the setValue:forKey: interface is called to set the attribute name to the key name of the JSON object, and the attribute value corresponding to the attribute name is set to the preset key value.

[0176] In addition, the apparatus 800 for converting a JSON object into a model object according to the embodiment of the present invention may further include: a second processing module and a second setting module ( Figure 8 ). The second processing module is used to determine whether the type of the key value is an array. If the type of the key value is an array and the array element of the array is marked as a JSON-able model object, an instance of the model object is created; and the attribute name of the instance is set to the key name, and the attribute value of the instance is set to the value of the array element. The second setting module is used to determine whether the original key value is empty. If the original key value is not empty, the original key value is updated to a preset key value; and the attribute name is set to the key name, and the attribute value is set to the original key value.

[0177] From the above description, it can be seen that when the key value type of the JSON object is a dictionary, the attribute type associated with the key name of the JSON object is found to create an instance of the attribute type, and then the attribute name of the instance is set to the key name and the attribute value is set to the key value, thereby realizing the conversion of the JSON object whose key value type is a dictionary to the model object; when the key value is an array and the array element is marked as a JSONizable model object, an instance of the model object is created to realize the conversion of the JSON object whose key value type is an array to the model object; a condition for obtaining the original key value is set, and the original key value is obtained when the condition is met, so as to update the original key value when the obtained original key value is not empty.

[0178] Fig. 9 An exemplary system architecture 900 is shown to which the method for converting between a model object and a JSON object or the apparatus for converting between a model object and a JSON object according to an embodiment of the present invention can be applied.

[0179] like Fig. 9 As shown, system architecture 900 may include terminal devices 901, 902, 903, network 904 and server 905. Network 904 is used to provide a medium for communication links between terminal devices 901, 902, 903 and server 905. Network 904 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.

[0180] Users can use terminal devices 901, 902, 903 to interact with server 905 through network 904 to receive or send messages, etc. Various communication client applications can be installed on terminal devices 901, 902, 903, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc.

[0181] The terminal devices 901 , 902 , and 903 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers, etc.

[0182] The server 905 may be a server that provides various services, such as a background management server that analyzes the predefined model objects or JSON objects sent by the administrator using the terminal devices 901, 902, and 903. The background management server may analyze the received model objects or JSON objects, and feed back the processing results (such as the conversion results between the model objects and the JSON objects) to the terminal device.

[0183] It should be noted that the method for converting between model objects and JSON objects provided in the embodiment of the present application is generally executed by the server 905 , and accordingly, the device for converting between model objects and JSON objects is generally set in the server 905 .

[0184] It should be understood that Fig. 9 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.

[0185] According to an embodiment of the present invention, the present invention also provides an electronic device and a computer-readable medium.

[0186] The electronic device of the present invention comprises: one or more processors; a storage device for storing one or more programs, and when the one or more programs are executed by the one or more processors, the one or more processors implement a method for converting between a model object and a JSON object according to an embodiment of the present invention.

[0187] The computer-readable medium of the present invention stores a computer program thereon, and when the program is executed by a processor, a method for converting between a model object and a JSON object according to an embodiment of the present invention is implemented.

[0188] Reference below Fig.10 , which shows a schematic diagram of the structure of a computer system 1000 suitable for implementing an electronic device of an embodiment of the present invention. Fig.10 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.

[0189] like Fig.10 As shown, the computer system 1000 includes a central processing unit (CPU) 1001, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 1002 or a program loaded from a storage part 1008 into a random access memory (RAM) 1003. In the RAM 1003, various programs and data required for the operation of the computer system 1000 are also stored. The CPU 1001, the ROM 1002, and the RAM 1003 are connected to each other through a bus 1004. An input / output (I / O) interface 1005 is also connected to the bus 1004.

[0190] The following components are connected to the I / O interface 1005: an input section 1006 including a keyboard, a mouse, etc.; an output section 1007 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 1008 including a hard disk, etc.; and a communication section 1009 including a network interface card such as a LAN card, a modem, etc. The communication section 1009 performs communication processing via a network such as the Internet. A drive 1010 is also connected to the I / O interface 1005 as needed. A removable medium 1011, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 1010 as needed, so that a computer program read therefrom is installed into the storage section 1008 as needed.

[0191] In particular, according to the embodiments disclosed in the present invention, the process described in the main step diagram above can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the main step diagram. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 1009, and / or installed from the removable medium 1011. When the computer program is executed by the central processing unit (CPU) 1001, the above-mentioned functions defined in the system of the present invention are executed.

[0192] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.

[0193] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, functions and operations of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0194] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor. For example, they may be described as: a processor includes a first acquisition module, a second acquisition module, a generation module, a judgment module, a search module, and a setting module. The names of these modules do not constitute a limitation on the modules themselves in some cases. For example, the first acquisition module may also be described as "a module for acquiring the attributes of a predefined model object to obtain the attribute name corresponding to the attribute".

[0195] As another aspect, the present invention also provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently and not assembled into the device. The above computer-readable medium carries one or more programs. When the above one or more programs are executed by a device, the device includes: obtaining the attributes of a predefined model object to obtain the attribute name corresponding to the attribute; obtaining the attribute value corresponding to the attribute name from the instantiated model object; judging whether the attribute value is an instance of a predefined tool class, if the attribute value is an instance of the tool class, generating a JSON object by a recursive algorithm, wherein the tool class is the parent class of the model object. And including obtaining the type of the key value of the JSON object, judging whether the type of the key value is a dictionary; if the type of the key value is a dictionary, obtaining the attributes of the predefined model object to find the attribute type associated with the key name of the JSON object; creating an instance of the class corresponding to the attribute type, so as to set the attribute name of the instance to the key name, and set the attribute value corresponding to the attribute name to the key value.

[0196] From the above description, it can be seen that after obtaining the attribute value of the model object, the type of the attribute value is determined. If the attribute value is an instance of a predefined tool class, a recursive algorithm is used to generate a JSON object, thereby realizing the conversion of a model object with other model objects nested in it to a JSON object; when the attribute value is an array, and the array element is marked as a JSON-convertible model object and is an instance of the model object, the array element is converted to a JSON object, thereby realizing the conversion of an array element from a custom model object to a JSON object; the array elements are marked as JSON-convertible model objects in advance and stored as key-value pairs to facilitate determining whether the array meets the conditions for conversion; the conditions for obtaining the attribute name are set to The acquisition of the attribute name is performed when the condition is met; when the key value type of the JSON object is a dictionary, the attribute type associated with the key name of the JSON object is found to create an instance of the attribute type, and then the attribute name of the instance is set to the key name and the attribute value is set to the key value, thereby realizing the conversion of the JSON object whose key value type is a dictionary to the model object; when the key value is an array and the array element is marked as a JSONizable model object, an instance of the model object is created to realize the conversion of the JSON object whose key value type is an array to the model object; a condition for acquiring the original key value is set, and the acquisition of the original key value is performed when the condition is met, so as to update the original key value when the acquired original key value is not empty.

[0197] The above product can execute the method provided by the embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method. For technical details not described in detail in this embodiment, please refer to the method provided by the embodiment of the present invention.

[0198] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.

Claims

1. A method for converting a model object into a JSON object, characterized in that: include: Obtaining a predefined attribute of a model object to obtain an attribute name corresponding to the attribute; Obtain the attribute value corresponding to the attribute name from the instantiated model object; The JSONizable array property marking interface is used to determine whether the property value is an instance of a predefined tool class. If the property value is an instance of the tool class, a JSON object is generated through a recursive algorithm, wherein the tool class is the parent class of the model object; the JSONizable array property marking interface is used to mark the elements in the array class property as JSONizable types.

2. The method according to claim 1, characterized in that After the step of obtaining the attribute value corresponding to the attribute name from the instantiated model object, the method further includes: determining whether the attribute value is an array; if the attribute value is an array and the array element of the array is marked as a JSON-convertible model object, determining whether the array element is an instance of the model object; and when the array element of the array is an instance of the model object, converting the array element into the JSON object.

3. The method according to claim 2, characterized in that The method also includes: marking the array element as a JSON-capable model object; using the class name of the model object and the attribute name as a key and storing the array element as a value to determine whether the array element of the array is marked as a JSON-capable model object.

4. The method according to any one of claims 1 to 3, characterized in that: Obtaining the attribute name corresponding to the attribute, including: obtaining the attribute type corresponding to the attribute, and determining whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, determining whether the class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, obtaining the class name corresponding to the attribute type, and determining whether the class name corresponding to the attribute type is a subclass of the tool class; if the class name corresponding to the attribute type is not a subclass of the tool class, determining whether the class name corresponding to the attribute type is an Object-C base class; when the class of the attribute type is not an Object-C class, or the class name corresponding to the attribute type is a subclass of the tool class, or the class name corresponding to the attribute type is the Object-C base class, obtaining the attribute name corresponding to the attribute.

5. A method for converting a JSON object into a model object, characterized in that: include: Get the type of the key value of the JSON object, and determine whether the type of the key value is a dictionary; If the type of the key value is a dictionary, obtain the attributes of the predefined model object to find the attribute type associated with the key name of the JSON object; obtain the original key value of the JSON object, and if the original key value is empty, create an instance of the class corresponding to the attribute type to set the attribute name of the instance to the key name, and set the attribute value corresponding to the attribute name to the preset key value.

6. The method according to claim 5, characterized in that The method also includes: determining whether the type of the key value is an array; if the type of the key value is an array and the array element of the array is marked as a JSON-able model object, creating an instance of the model object; setting the attribute name of the instance to the key name, and setting the attribute value of the instance to the value of the array element.

7. The method according to claim 5 or 6, characterized in that: The obtaining of the original key value of the JSON object includes: determining whether the attribute type is a subclass of the model object, and if the attribute type is a subclass of the model object, obtaining the original key value of the JSON object; the method also includes: determining whether the original key value is empty, and if the original key value is not empty, updating the original key value to a preset key value; setting the attribute name to the key name, and setting the attribute value to the original key value.

8. A device for converting a model object into a JSON object, characterized in that: include: A first acquisition module is used to acquire a predefined attribute of a model object to obtain an attribute name corresponding to the attribute; A second acquisition module is used to acquire the attribute value corresponding to the attribute name from the instantiated model object; A generation module is used to determine whether the attribute value is an instance of a predefined tool class through a JSONizable array attribute marking interface. If the attribute value is an instance of the tool class, a JSON object is generated through a recursive algorithm, wherein the tool class is the parent class of the model object; the JSONizable array attribute marking interface is used to mark the elements in the array class attribute as JSONizable types.

9. The device according to claim 8, characterized in that The device also includes: a first processing module, used to determine whether the attribute value is an array, if the attribute value is an array and the array element of the array is marked as a JSON-convertible model object, then determine whether the array element is an instance of the model object; and when the array element of the array is an instance of the model object, convert the array element into the JSON object.

10. The device according to claim 9, characterized in that The device also includes: a marking judgment module, which is used to mark the array elements as JSON-capable model objects; and uses the class name of the model object and the attribute name as keys and the array elements as values ​​to store, so as to determine whether the array elements of the array are marked as JSON-capable model objects.

11. The device according to any one of claims 8 to 10, characterized in that The first acquisition module is further used to: obtain the attribute type corresponding to the attribute, and determine whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, determine whether the class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, obtain the class name corresponding to the attribute type, and determine whether the class name corresponding to the attribute type is a subclass of the tool class; if the class name corresponding to the attribute type is not a subclass of the tool class, determine whether the class name corresponding to the attribute type is an Object-C base class; and when the class of the attribute type is not an Object-C class, or the class name corresponding to the attribute type is a subclass of the tool class, or the class name corresponding to the attribute type is the Object-C base class, obtain the attribute name corresponding to the attribute.

12. A device for converting a JSON object into a model object, characterized in that: include: A judgment module is used to obtain the type of the key value of the JSON object and judge whether the type of the key value is a dictionary; A search module, configured to obtain a predefined attribute of a model object if the type of the key value is a dictionary, so as to find an attribute type associated with the key name of the JSON object; The first setting module is used to obtain the original key value of the JSON object. If the original key value is empty, an instance of the class corresponding to the attribute type is created to set the attribute name of the instance to the key name and the attribute value corresponding to the attribute name to a preset key value.

13. The device according to claim 12, characterized in that The device also includes: a second processing module, which is used to determine whether the type of the key value is an array. If the type of the key value is an array and the array element of the array is marked as a JSON-able model object, an instance of the model object is created; and the attribute name of the instance is set to the key name, and the attribute value of the instance is set to the value of the array element.

14. The device according to claim 12 or 13, characterized in that The first setting module is also used to: determine whether the attribute type is a subclass of the model object, and if the attribute type is a subclass of the model object, obtain the original key value of the JSON object; the device also includes: a second setting module, used to determine whether the original key value is empty, and if the original key value is not empty, update the original key value to a preset key value; and set the attribute name to the key name and set the attribute value to the original key value.

15. An electronic device, characterized in that: include: one or more processors; A storage device, used for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1-7.

16. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 7 is implemented.

Citation Information

Patent Citations

  • Method and system of adapting data model to user interface component

    CN102906697A

  • Automatic semanteme extraction method for Web data exchange

    CN103886046A

  • Abstract control model programming device and method

    CN104407863A

  • Method and system for converting JSON data into model data

    CN105159928A

  • Webpage saving method and device

    CN105468606A