CN110764768A - Method and device for mutual conversion between model object and JSON object - Google Patents

Method and device for mutual conversion between model object and JSON object Download PDF

Info

Publication number
CN110764768A
CN110764768A CN201810833719.1A CN201810833719A CN110764768A CN 110764768 A CN110764768 A CN 110764768A CN 201810833719 A CN201810833719 A CN 201810833719A CN 110764768 A CN110764768 A CN 110764768A
Authority
CN
China
Prior art keywords
attribute
class
name
json
type
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN201810833719.1A
Other languages
Chinese (zh)
Inventor
董魁
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Jingdong Financial Technology Holding Co Ltd
Original Assignee
Beijing Jingdong Financial Technology Holding Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Jingdong Financial Technology Holding Co Ltd filed Critical Beijing Jingdong Financial Technology Holding Co Ltd
Priority to CN201810833719.1A priority Critical patent/CN110764768A/en
Publication of CN110764768A publication Critical patent/CN110764768A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

The invention discloses a method and a device for mutual conversion between a model object and a JSON object, and relates to the technical field of computers. One embodiment of the method comprises: acquiring attributes of a predefined model object to obtain attribute names corresponding to the attributes; acquiring an attribute value corresponding to the attribute name from the instantiated model object; and judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the 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. According to the method, after the attribute value of the model object is obtained, the type of the attribute value is judged, if the attribute value is the instance of the predefined tool class, the JSON object is generated by using a recursive algorithm, and the conversion from the model object nested with other model objects to the JSON object is realized.

Description

Method and device for mutual conversion between model object and JSON object
Technical Field
The invention relates to the field of computers, in particular to a method and a device for mutual conversion between a model object and a JSON object.
Background
In the existing iOS system, all attributes of a model object are dynamically acquired through an API (application programming Interface) during conversion between the model object and a JSON object, and are implemented by combining some algorithms. The following two implementation modes are provided: the first method is as follows: after all attributes are acquired, an attribute Value is acquired by calling a Key (Key) interface with the name of the attribute or calling a Get method, the attribute Value is stored in a dictionary (NSDictionary) in a Key-Value pair mode, and then an interface of + [ NSJSONSerializationdataWithJSONObject: options: error: ] is called to be converted into JSON data. The second method comprises the following steps: and the conversion between the model object and the JSON object is realized through a JSONModel model framework. Wherein the iOS system is a mobile operating system developed by apple Inc.; JSON is a lightweight data exchange format.
In the process of implementing the invention, the inventor finds that at least the following problems exist in the prior art:
the first mode only supports the transformation of NSString attribute types, does not support model object nesting, and does not support model objects in arrays (NSArray). The second mode can support the conversion, but the realization algorithm and the interface are complex, the code amount is large, and the use is difficult.
Disclosure of Invention
In view of this, the embodiments of the present invention provide a method and an apparatus for inter-converting a model object and a JSON object, including a method and an apparatus for converting a model object into a JSON object, and a method and an apparatus for converting a JSON object into a model object. In this embodiment, after the attribute value of the model object is obtained, the type of the attribute value is determined, and if the attribute value is an instance of a predefined tool class, a JSON object is generated using a recursive algorithm, thereby realizing conversion from the model object nested with other model objects to the JSON object.
To achieve the above object, according to an aspect of the embodiments of the present invention, there is provided a method of converting a model object into a JSON object.
The method for converting the model object into the JSON object comprises the following steps: acquiring attributes of a predefined model object to obtain attribute names corresponding to the attributes; acquiring an attribute value corresponding to the attribute name from the instantiated model object; and judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the 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.
Optionally, after the step of obtaining the attribute value corresponding to the attribute name from the instantiated model object, the method further includes: judging whether the attribute value is an array or not, and if the attribute value is the array and the array element of the array is marked as a JSON model object, judging whether the array element is an example of the model object or not; and when the array element of the array is the instance of the model object, converting the array element into the JSON object.
Optionally, the method further comprises: marking the array elements as JSON-enabled model objects; and taking the class name and the attribute name of the model object as keys, and taking the array element as a value to store so as to judge whether the array element of the array is marked as a JSON model object.
Optionally, obtaining an attribute name corresponding to the attribute includes: acquiring an attribute type corresponding to the attribute, and judging whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, judging whether a class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, acquiring a class name corresponding to the attribute type, and judging 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 the subclass of the tool class, judging whether the class name corresponding to the attribute type is an Object-C basic class or not; and when the class of the attribute type is not the Object-C class, or the class name corresponding to the attribute type is the subclass of the tool class, or the class name corresponding to the attribute type is the Object-C basic class, acquiring the attribute name corresponding to the attribute.
To achieve the above object, according to another aspect of the embodiments of the present invention, there is provided a method for converting a JSON object into a model object.
The method for converting the JSON object into the model object in the embodiment of the invention comprises the following steps: acquiring the type of a key value of a JSON object, and judging whether the type of the key value is a dictionary or not; if the type of the key value is a dictionary, acquiring the attribute of a predefined model object to search an attribute type associated with the key name of the JSON object; and acquiring an original key value of the JSON object, if the original key value is null, creating an instance of the class corresponding to the attribute type, setting an attribute name of the instance as the key name, and setting an attribute value corresponding to the attribute name as a preset key value.
Optionally, the method further comprises: judging whether the type of the key value is an array or not, and if the type of the key value is the array and the array element of the array is marked as a JSON model object, creating an example of the model object; and setting the attribute name of the instance as the key name, and setting the attribute value of the instance as the value of the array element.
Optionally, the obtaining an original key value of the JSON object includes: judging whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, acquiring an original key value of the JSON object; the method further comprises the following steps: judging whether the original key value is empty, and if not, updating the original key value to a preset key value; and setting the attribute name as the key name, and setting the attribute value as the original key value.
To achieve the above object, according to another aspect of the embodiments of the present invention, there is provided an apparatus for converting a model object into a JSON object.
The device for converting the model object into the JSON object in the embodiment of the invention comprises the following steps: the first acquisition module is used for acquiring the attribute of a predefined model object to obtain an attribute name corresponding to the attribute; the second obtaining module is used for obtaining the attribute value corresponding to the attribute name from the instantiated model object; and the generating module is used for judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the instance of the tool class, generating the JSON object by a recursive algorithm, wherein the tool class is a parent class of the model object.
Optionally, the apparatus further comprises: the first processing module is used for judging whether the attribute value is an array or not, and judging whether the array element is an example of the model object or not if the attribute value is the array and the array element of the array is marked as the JSON model object; and when the array element of the array is the instance of the model object, converting the array element into the JSON object.
Optionally, the apparatus further comprises: the mark judging module is used for marking the array elements as JSON model objects; and taking the class name and the attribute name of the model object as keys, and storing the array element as a value to judge whether the array element of the array is marked as a JSON model object.
Optionally, the first obtaining module is further configured to: acquiring an attribute type corresponding to the attribute, and judging whether the attribute type is a preset supported attribute type; if the attribute type is the supported attribute type, judging whether a class corresponding to the attribute type is an Object-C class; if the class corresponding to the attribute type is an Object-C class, acquiring a class name corresponding to the attribute type, and judging 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 the subclass of the tool class, judging whether the class name corresponding to the attribute type is an Object-C basic class or not; and when the class of the attribute type is not the Object-C class, or the class name corresponding to the attribute type is the subclass of the tool class, or the class name corresponding to the attribute type is the Object-C basic class, acquiring the attribute name corresponding to the attribute.
To achieve the above object, according to another aspect of the embodiments of the present invention, there is provided an apparatus for converting a JSON object into a model object.
The device for converting the JSON object into the model object in the embodiment of the invention comprises the following steps: the judging module is used for acquiring the type of the key value of the JSON object and judging whether the type of the key value is a dictionary or not; the searching module is used for acquiring the attribute of a predefined model object if the type of the key value is a dictionary so as to search the attribute type associated with the key name of the JSON object; and the first setting module is used for acquiring an original key value of the JSON object, if the original key value is null, creating an instance of the class corresponding to the attribute type, setting the attribute name of the instance as the key name, and setting the attribute value corresponding to the attribute name as a preset key value.
Optionally, the apparatus further comprises: the second processing module is used for judging whether the type of the key value is an array or not, and if the type of the key value is an array and the array element of the array is marked as a JSON-enabled model object, creating an example of the model object; and setting the attribute name of the instance as the key name, and setting the attribute value of the instance as the value of the array element.
Optionally, the first setting module is further configured to: judging whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, acquiring an original key value of the JSON object; the device further comprises: the second setting module is used for judging whether the original key value is null or not, and if the original key value is not null, updating the original key value to a preset key value; and setting the attribute name as the key name and setting the attribute value as the original key value.
To achieve the above object, according to still another aspect of embodiments of the present invention, there is provided an electronic apparatus.
An electronic device of an embodiment of the present invention includes: one or more processors; a storage device, configured to store one or more programs that, when executed by the one or more processors, cause the one or more processors to implement a method for converting a model object into a JSON object according to an embodiment of the present invention.
To achieve the above object, according to still another aspect of embodiments of the present invention, there is provided a computer-readable medium.
A computer readable medium of an embodiment of the present invention has stored thereon a computer program that, when executed by a processor, implements a method of converting a model object into a JSON object of an embodiment of the present invention.
To achieve the above object, according to still another aspect of embodiments of the present invention, there is provided an electronic apparatus.
An electronic device of an embodiment of the present invention includes: one or more processors; the storage device is used 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 realize the method for converting the JSON object into the model object.
To achieve the above object, according to still another aspect of embodiments of the present invention, there is provided a computer-readable medium.
A computer readable medium of an embodiment of the present invention has a computer program stored thereon, and when the program is executed by a processor, the program implements a method of converting JSON objects into model objects of an embodiment of the present invention.
One embodiment of the above invention has the following advantages or benefits: after the attribute value of the model object is obtained, the type of the attribute value is judged, if the attribute value is the instance of the predefined tool class, the JSON object is generated by using a recursive algorithm, and the conversion from the model object nested with other model objects to the JSON object is realized; when the attribute value is an array, and the array element is marked as a JSON model object and is an instance of the model object, converting the array element into the JSON object, thereby realizing the conversion from the model object with the array element being self-defined to the JSON object; the array elements are marked as JSON model objects in advance, and are stored by key value pairs, so that whether the array meets the conversion condition or not is conveniently determined; setting a condition for acquiring the attribute name to execute acquisition of the attribute name if the condition is satisfied; when the type of the key value of the JSON object is a dictionary, searching an attribute type associated with the key name of the JSON object to create an instance of the attribute type, setting the attribute name of the instance as the key name and setting the attribute value as the key value, and realizing the conversion from the JSON object with the key value type as the dictionary to a model object; when the key value is an array and the array element is marked as a JSON model object, creating an instance of the model object to realize the conversion from the JSON object with the key value type as the array to the model object; and setting a condition for acquiring the original key value, and executing the acquisition of the original key value under the condition of meeting the condition so as to update the original key value when the acquired original key value is not empty.
Further effects of the above-mentioned non-conventional alternatives will be described below in connection with the embodiments.
Drawings
The drawings are included to provide a better understanding of the invention and are not to be construed as unduly limiting the invention. Wherein:
FIG. 1 is a 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;
FIG. 2 is a schematic diagram of the main steps of a method for converting JSON objects into model objects according to an embodiment of the present invention;
FIG. 3 is a schematic main flow diagram of a method for converting a model object into a JSON object according to an embodiment of the present invention;
FIG. 4 is a diagram illustrating the result of converting a model object into a JSON object according to an embodiment of the present invention;
FIG. 5 is a schematic main flow chart of a method for converting a JSON object into a model object according to an embodiment of the invention;
FIG. 6 is a diagram illustrating the result of converting a JSON object into a model object according to an embodiment of the present invention;
FIG. 7 is a schematic diagram of the main modules of an apparatus for converting a model object into a JSON object according to an embodiment of the present invention;
FIG. 8 is a schematic diagram of the main modules of an apparatus for converting JSON objects into model objects according to an embodiment of the present invention;
FIG. 9 is an exemplary system architecture diagram in which embodiments of the present invention may be employed;
FIG. 10 is a schematic diagram of a computer apparatus suitable for use in an electronic device to implement an embodiment of the invention.
Detailed Description
Exemplary embodiments of the present invention are described below with reference to the accompanying drawings, in which various details of embodiments of the invention are included to assist understanding, and which are to be considered as merely exemplary. Accordingly, those of ordinary skill in the art will recognize that various changes and modifications of the embodiments described herein can be made without departing from the scope and spirit of the invention. Also, descriptions of well-known functions and constructions are omitted in the following description for clarity and conciseness.
Fig. 1 is a 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 the groups involved in the embodiment of the invention are data types in the Object-C language. As shown in fig. 1, the method for converting a model object into a JSON object according to the embodiment of the present invention mainly includes the following steps:
step S101: and acquiring the attribute of the predefined model object to obtain an attribute name corresponding to the attribute. The model object is a self-defined tool class or a subclass of the tool class, and comprises one or more attributes, wherein the attributes comprise one or more attribute types, such as int, long, NSString, a self-defined model object or a subclass thereof, an array, and an array element is a self-defined model object. In the step, a class _ copyPropertyList interface is called to obtain the attribute of the model object; and then, calling a property _ getName interface to acquire a property name corresponding to the property.
Step S102: and acquiring the attribute value corresponding to the attribute name from the instantiated model object. The model object is instantiated, then an interface of [ NSObjectdiactionWithValuesForKeys ] is called, the attribute value corresponding to the attribute name can be obtained, and whether the attribute value is an example of a tool class, an array or other types needs to be judged subsequently, so that different processing is carried out according to the judgment result.
Step S103: and judging whether the attribute value is a predefined tool class instance, and if the attribute value is the tool class instance, generating the JSON object through a recursive algorithm. In an embodiment, the tool class is a JDPayJSONModel class. The JDPayJSONModel class inherits from the NSObject class, and the inside of the JDPayJSONModel class only contains codes of about 280 lines, and the JDPayJSONModel to JSONDirectional interface provides an interface for converting a model object into a JSON object; providing a JSON-enabled array attribute marking interface + [ JDPayJSONModelsetJSONAbleElementClass: forArrayProperty ] for marking that an element in a certain array class attribute is a JSON-enabled JDPayJSONModel type; the conversion of the JSON object to the model object is realized by reloading the interface of the [ NSObject setValue: ] of the parent class, and the conversion of the JSON object to the model object is realized by calling the interface of the [ NSObject setValuesForKeyWithDictionary: ]. When the user uses the method, the user only needs to create subclasses of JDPayJSONModel classes, and the model object and the JSON object can be exchanged. The step is used for judging whether the attribute value is nested with the model object, and if so, the nested model object is converted into the JSON object by using a recursive algorithm. The JSON object may then be stored in a dictionary in the form of key-value pairs. The purpose of converting the nested model objects into JSON objects one by one is realized through the steps S101 to S103.
Fig. 2 is a schematic diagram of the main steps of the method for converting JSON objects into model objects according to the embodiment of the present invention. As shown in fig. 2, the method for converting a JSON object into a model object according to the embodiment of the present invention mainly includes the following steps:
step S201: and acquiring the type of the key value of the JSON object, and judging whether the type of the key value is a dictionary or not. The JSON Object refers to a dictionary data type in an Object-C language, and can be converted into standard JSON character string data through an API (application programming interface). The data format of the JSON object is as follows: a key name/key-value pair comprising a field name and a corresponding value, both in a double quotation mark, with ": "separate, such as" firstName ": John". The type of key value may be a string, a value, a dictionary, an array, etc. And calling an interface of [ NSObject isKindOfClass: ] to acquire the type of the key value of the pre-defined JSON object.
Step S202: and if the type of the key value is a dictionary, acquiring the attribute of a predefined model object so as to search the attribute type associated with the key name of the JSON object. And calling a class _ copyPropertyList interface to acquire all attributes of the model object, wherein the model object is the JDPayJSONModel class in the embodiment. And finding out the attribute name of the model object which is the same as the key name through enumeration, wherein the attribute type corresponding to the attribute name is the attribute type associated with the key name.
Step S203: and acquiring an original key value of the JSON object, if the original key value is null, creating an instance of the class corresponding to the attribute type, setting an attribute name of the instance as the key name, and setting an attribute value corresponding to the attribute name as a preset key value. Firstly, judging whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, calling a valueForKey interface to acquire an original key value of the JSON object. And then, judging whether the original key value is null (nil), if so, creating an instance of the class corresponding to the attribute type, calling setValue: forKey, setting an attribute name as the key name of the JSON object by using an interface, and setting an attribute value corresponding to the attribute name as a preset key value.
Fig. 3 is a schematic main flow diagram of a method for converting a model object into a JSON object according to an embodiment of the present invention. As shown in fig. 3, in the method for converting a model object into a JSON object, an interface on which a core depends is- [ JDPayJSONModeltoJSONDictionary ], and the implementation of the interface mainly includes the following steps:
step S301: a model object, attribute types not supported by the model object, and supported base classes are defined. In an embodiment, the model object is a JDPayJSONModel class or a subclass thereof, the JDPaJSONModel class is a subclass of NSObject, and the model object includes one or more attributes, and the attributes have one or more attribute types. In an embodiment, the Object-C language is used to define the model objects: the OJSONModel, defined in this example, is a subclass of the JDPayJSONModel, and specifically includes:
@interface OJSONModel:JDPayJSONModel
@property(nonatomic,strong)NSString*string;
@property(nonatomic,strong)NSMutableString*mutableString;
@property(nonatomic,strong)NSNumber*number;
@property(nonatomic,strong)NSDecimalNumber*decimalNumber;
@property(nonatomic,strong)NSDictionary*dictionary;
@property(nonatomic,strong)NSDictionary*mutableDictionary;
@property(nonatomic,strong)NSArray*array;
@property(nonatomic,strong)NSMutableArray*mutableArray;
@end
at initialization, attribute type prefixes not supported by the model object are defined, such as, for example, pointer: "T ^", id type: "T @,", structure: "T {", anion type: similarly, at initialization, Object-C base classes supported by the model Object are defined, such as nsstring.class, nsmultimedia string.class, nsdecelmalnumber.class, nsnumber.class, nsmutaldictariany.class, nsdiactionary.class, nsdirctionary.class, nsarray.class, nsmutavariable array.class, and so on.
Step S302: and acquiring the attribute of the model object. In an embodiment, all attributes of the model object are obtained by calling a class _ copyPropertyList interface. The attribute is the @ property statement in the defined model object, i.e. the OJSONModel class includes eight attributes.
Step S303: judging whether the un-enumerated attribute exists, if so, executing the step S304; if not, the flow ends. All the attributes acquired in step S302 are placed in an attribute array to traverse each attribute in the attribute array.
Step S304: enumerate the attributes, and execute step S305. Enumerating, i.e., traversing, each attribute in the attribute array.
Step S305: and acquiring the attribute type of the current attribute. In an embodiment, the property type is obtained by calling the property _ getAttributes interface. The attribute types correspond to NSString (string), NSMutableString (variable string), NSNumber (digits), NSDecimalNumber (decimal digits), NSDictionary (dictionary), and NSArray (array) in the OJSONModel class.
Step S306: judging whether the current attribute type is a preset support attribute type, if so, executing a step S307; if not, the corresponding attribute is discarded and step S304 is performed. After acquiring the attribute type of the current attribute, taking the attribute type as the current attribute type, then comparing the current attribute type with the defined attribute type which is not supported, and if the acquired attribute type is the attribute type which is not supported, such as a pointer and the like, discarding the attribute; and if the attribute type which is not supported does not have the current attribute type, the attribute type is a supported attribute type.
Step S307: judging whether the class corresponding to the current attribute type is an Object-C class, if so, acquiring a class name corresponding to the current attribute type, and executing step S308; if not, step S310 is performed. The step is to judge 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 the attribute type is obtained, an NSClassFromString interface can be called to generate a class corresponding to the attribute type.
Step S308: judging whether the class corresponding to the current attribute type is a subclass of the JDPayJSONModel class, if so, executing the step S310; if not, step S309 is performed. In the embodiment, based on the obtained Class name corresponding to the current attribute type, whether the Class corresponding to the current attribute type is a subclass of the JDPayJSONModel Class is determined by calling a + [ Class (boll) issubclassfofclass (Class) aClass ] interface.
Step S309: judging whether the class corresponding to the current attribute type is a supported Object-C basic class, if so, executing step S310; if not, step S304 is performed. And searching whether the class name corresponding to the current attribute type exists in the basic class defined in the step S301, and if so, taking the class corresponding to the current attribute type as a supported basic class.
Step S310: and acquiring the attribute name of the current attribute. In an embodiment, the property names corresponding to string, multitableString, number, decimalNumber, dictionary, multitableDictionary, array, and multitableArray in the OJSONModel class of the embodiment are obtained by calling the property _ getName interface.
Step S311: and acquiring the attribute value corresponding to the current attribute name from the instantiated model object. And after the attribute name of the current attribute is acquired, taking the attribute name as the current attribute name. In an embodiment, the attribute values are obtained by calling the [ NSObjectdiactionWithValuesForKeys: ] interface.
Step S312: judging whether the current attribute value is an instance of the JDPayJSONModel class, if so, executing the step S313; if not, step S315 is performed. And after the value of the current attribute is acquired, taking the value as the value of the current attribute.
Step S313: and generating the JSON object corresponding to the current attribute. The JSON Object is an Object-C Object with YES return value obtained by calling + [ NSJSONSerialization isValidJSONObject ], i.e. the Object can be converted into standard JSON string data using the API of the iOS system, 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, NSMutabiltrying, NSNumber, NSDecimal number, NSDictionary, NSMutableDictionary, NSArray, and NSMutableArray. In the embodiment, a recursive algorithm is used for calling a toJSONDirectional 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 homogenous sub-problems. The same kind of problem here is to call the same toJSONDirectional interface on different objects to convert the nested model object (JDPayJSONModel) into a JSON object.
Step S314: judging whether the generated JSON object is nil, if so, executing the step S304; if not, step S324 is performed. Where nil is a type that has only one value nil, indicating no value, and its main function is to distinguish between any other values.
Step S315: judging whether the current attribute value is an array and the array element of the array is marked as a JSON JDPayJSONModel class or a subclass thereof, if so, executing a step S316; if not, step S324 is performed. In the embodiment, whether the current attribute value is an array is judged by calling a value iskindoofclass NSArray interface. At initialization, the setJSONAbleElementClass (Class) elementClassForarrayProperty (NSString) propertName interface is called to mark elements within the property of the tuple type as JSON enabled JDPayJSONModel Class or subclasses thereof. And during marking, the current class name and the attribute name are used as keys, and the transmitted elementClass is used as a value and is stored in the global dictionary. When a certain object calls setValue V forKey K, the class name of the current object is known, the attribute name is K, namely the class name and the attribute name exist, and then the global dictionary lookup can know whether the array element is marked.
Step S316: an array of JSON objects is created. In an embodiment, the JSON object array may be represented by newValues, which is used to store the JSON object generated by each element in the array of step S315.
Step S317: judging whether the array has unassociated array elements, if so, executing step S318; otherwise, step S323 is performed.
Step S318: enumerating array elements of the array, and executing step S319.
Step S319: judging whether the current array element is an example of a marked class, if so, executing the step S320; if not, step S317 is performed. The tagged class is a JSON enabled JDPayJSONModel class or a subclass thereof for which array elements are tagged.
Step S320: and generating the JSON object corresponding to the current array element. In the embodiment, a- [ JDPayJSONModel to JSONDirectional ] interface is called to generate a JSON object corresponding to the current array element.
Step S321: judging whether the generated JSON object is nil, if so, executing the step S317; if not, step S322 is performed.
Step S322: and adding the generated JSON object into a JSON object array, and executing the step S317. I.e. the JSON object corresponding to the current array element is added to the newValues.
Step S323: judging whether the JSON object array is empty, if so, executing a step S304; if not, step S324 is performed. Namely, whether the JSON object array newValues is empty is judged.
Step S324: and storing the JSON object into a dictionary in a key value pair form. The dictionary is a data structure and comprises a plurality of built-in functions, the dictionary is a data container of key value pairs, and the key value pairs (key: value) are marked by colon': "split, comma between key-value pairs", "split, whole dictionary included in curly brackets" { } ", and both keys and values are in one-to-one correspondence. This step stores all JSON objects in a dictionary in the form of Key (Key name) -Value (Key Value).
In an embodiment, two model objects are defined, an OJSONModel class and a PSONModel class, which are subclasses of the JDPayJSONModel class. The OJSONModel class is defined as described above, and the PSONModel class is defined as:
@interface PJSONModel:JDPayJSONModel
@property(nonatomic,assign)char c;
@property(nonatomic,assign)int intValue;
@property(nonatomic,assign)short shortValue;
@property(nonatomic,assign)long longValue;
@property(nonatomic,assign)long long longLongValue;
@property(nonatomic,assign)unsigned char uc;
@property(nonatomic,assign)unsigned int unsignedIntValue;
@property(nonatomic,assign)unsigned short unsignedShortValue;
@property(nonatomic,assign)unsigned long unsignedLongValue;
@property(nonatomic,assign)unsigned long long unsignedLongLongValue;
@property(nonatomic,assign)float floatValue;
@property(nonatomic,assign)double doubleValue;
@property(nonatomic,assign)BOOL boolValue;
// define the member variable named oms, which is an array of type and whose elements are custom OJSONModel model objects
@property(nonatomic,strong)NSArray<OJSONModel*>*oms;
// define member variables with name om, which are of the custom OJSONModel model object type
@property(nonatomic,strong)OJSONModel*om;
@end
The JSONModel class and the PSONModel class are instantiated separately. The instantiation results are as follows:
// elements in the tag oms array are JSON enabled OJSONModel classes
[PJSONModel setJSONAbleElementClass:OJSONModel.classforArrayProperty:@"oms"];
//om
OJSONModel*om=[OJSONModel new];
om.string=@"string";
om.mutableString=[NSMutableString stringWithString:@"mutableString"];
om.number=[NSNumber numberWithInteger:11];
om.decimalNumber=[NSDecimalNumber decimalNumberWithString:@"12"];
om.dictionary=@{@"key":@"value"};
om.mutableDictionary=[NSMutableDictionary dictionaryWithDictionary:om.dictionary];
om.array=@[@1,@2,@3];
om.mutableArray=[NSMutableArray arrayWithArray:om.array];
//pm
PJSONModel*pm=[PJSONModel new];
pm.c='c';
pm.intValue=1;
pm.shortValue=2;
pm.longValue=3;
pm.longLongValue=4;
pm.uc='C';
pm.unsignedIntValue=5u;
pm.unsignedShortValue=6u;
pm.unsignedLongValue=7u;
pm.unsignedLongLongValue=8u;
pm.floatValue=9.f;
pm.doubleValue=10.0;
pm.boolValue=YES;
pm.oms=@[om];
pm.om=om;
// Call toJSONDirectional interface to convert model object pm into JSON object pmd
NSDictionary*pmd=[pm toJSONDictionary];
FIG. 4 is a diagram illustrating the result of converting a model object into a JSON object according to an embodiment of the present invention. As shown in fig. 4, according to the JSONModel class and the PSONModel class defined and implemented as above, the model object pm is finally converted into the JSON object pmd according to the method for converting the model object into the JSON object of the embodiment of the present invention. The conversion process supports all raw data types (char, int, short, long, unknown x, double, float, CGFloat, NSInteger, NSUInteger, etc.), Object-C base data types (NSString, NSMutableString, NSNumber, NSDecimalNumber, NSDictionary, NSMutableDictionary, NSArray, NSMutableArray), Object-C model Object nesting, and Object-C model objects within an array (NSArray).
According to the method for converting the model object into the JSON object, the type of the attribute value is judged after the attribute value of the model object is obtained, if the attribute value is the instance of the predefined tool class, the JSON object is generated by using the recursive algorithm, and the conversion from the model object nested with other model objects to the JSON object is realized; when the attribute value is an array, and the array element is marked as a JSON model object and is an instance of the model object, converting the array element into the JSON object, thereby realizing the conversion from the model object with the array element being self-defined to the JSON object; the array elements are marked as JSON model objects in advance, and are stored by key value pairs, so that whether the array meets the conversion condition or not is conveniently determined; a condition for acquiring the attribute name is set to execute acquisition of the attribute name in a case where the condition is satisfied.
Fig. 5 is a schematic main flow diagram of a method for converting JSON objects into model objects according to an embodiment of the present invention. As shown in FIG. 5, in the method for converting JSON objects into model objects, the interface on which the kernel depends is [ NSObjectsetValue: forKey: ], which mainly comprises the following steps:
step S501: and acquiring 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 a- [ NSObjectISKindOfClass: ] interface, and the type of the key value V can be a character string, a numerical value, a dictionary, an array and the like.
Step S502: judging whether the type of the key value is a dictionary, if so, executing a step S503; if not, step S512 is performed. In the embodiment, whether the type of the key value is a dictionary is judged by calling a [ V isKindOfClass: NSDictionary.
Step S504: and acquiring the attribute of the defined model object, and enumerating and searching the attribute associated with the current key name. And acquiring all attributes of the model object, and finding out the attribute name of the model object which is the same as the current key name through enumeration, wherein the attribute corresponding to the attribute name is the attribute associated with the current key name.
Step S505: judging whether the attribute associated with the current key name is found, if so, executing the step S506; if not, step S519 is performed.
Step S506: an attribute type of an attribute associated with the current key name is obtained.
Step S507: judging whether the class corresponding to the attribute type is a subclass of the JDPayJSONModel class, if so, executing a step S508; if not, step S519 is performed. For example, the defined attribute type of the PJSONModel class includes OJSONModel, which is a subclass of the JDPayJSONModel class, the parent class of PJSONModel.
Step S508: and acquiring the original key value of the current key name. In the embodiment, a valueForKey interface is called to obtain the original key value OV of the current key name.
Step S509: judging whether the original key value is nil, if so, executing the step S510; if not, step S511 is performed.
Step S510: and creating an instance of the class corresponding to the attribute type, initializing, and executing the step S512. Creating an instance TI of the class corresponding to the attribute type, and calling a key value V for an interface to initialize the TI, wherein the key value V corresponds to PJSONModel pmo [ [ PJSONModel alloc ] initWithDictionary: pmd ]; a statement.
Step S511: and updating the original key value, and executing the step S512. In an embodiment, the interface of [ NSObjectsetValuesKeysWithDictionary: ] is called to update the original key value from OV to V.
Step S512: and setting an attribute value for the attribute associated with the current key name, and ending the process. In the embodiment, a setValue for key interface is called to set the attribute name as the key name K, correspondingly set the attribute value of the attribute associated with the current key name as TI or OV, and then mark the current key name as processed.
Step S513: judging whether the type of the key value is an array, if so, executing the step S514; if not, step S519 is performed. In the embodiment, whether the type of the key value is an array is judged by calling a [ V isKindOfClass: NSArray.
Step S514: judging whether the array elements in the array are marked as JSON JDPayJSONModel or subclass thereof, if so, executing step S515; if not, step S519 is performed. When initializing, a setJSONAbleElementClass (Class) elementClass for ArrayProperty (NSString) propertName interface is called to mark elements in the property of the array type as JSON-enabled JDPayJSONModel or subclasses thereof. And during marking, the current class name and the attribute name are used as keys, and the transmitted elementClass is used as a value and is stored in the global dictionary. When a setValue: VforKey: K is called on an object, the class name of the current object is known, the attribute name is K, namely the class name and the attribute name exist, and then the global dictionary lookup can know whether the array element is marked.
Step S515: a new array is created. The new array newValues1 is used to store the instance OI of the Object-C model Object of the JSON Object transformation in the array of the subsequent V representation.
Step S516: enumerating array elements of the array, creating an instance of the model object, initializing the instance and adding to the new array. In an embodiment, enumerate the array element E in the key-value V of the array type, create instance OI of the JDPayJSONModel class, call the- [ NSObject setValuesForKeysWithDirectional: ] interface initialize OI with array element E and add to the new array newValues 1.
Step S517: judging whether the new array is empty, if not, executing step S518; if so, step S519 is performed. I.e., determine if the new array newValues1 is empty.
Step S518: and assigning the new number group to the key value corresponding to the current key name, and executing the step S519. The new array newValues1 is assigned to the key value V.
Step S519: and setting the attribute name of the attribute as the key name and the attribute value as the key value, and ending the process. And calling a [ NSObjectsetValue: forKey: ] interface, setting the attribute name as the key name of the JSON object, and setting the attribute value corresponding to the attribute name as the key value of the JSON object.
In an embodiment, two model objects are also defined, an OJSONModel class and a PSONModel class, and both model objects are subclasses of the JDPayJSONModel class. The OJSONModel class and the PSONModel class are defined as described above. The JSONModel class and the PSONModel class are instantiated separately. The instantiation result is as described above, and the JSON object is then transformed into a model object by a PJSONModel pmo [ [ PJSON model alloc ] initWithdictionary: pmd ] statement.
FIG. 6 is a diagram illustrating the result of converting a JSON object into a model object according to an embodiment of the present invention. As shown in fig. 6, according to the method for converting a JSON object into a model object of the embodiment of the present invention, the JSON object pmd is finally converted into a model object pmo. The conversion process also supports all raw data types (char, int, short, long, unsigned, double, float, CGFloat, NSInteger, NSUInteger, etc.), Object-C base data types (NSString, NSMutableString, NSNumber, NSDecimalNumber, NSDictionary, NSMutableDictionary, NSArray, NSMutableArray), Object-C model Object nesting, and Object-C model objects within an array (NSArray).
According to the method for converting the JSON object into the model object, disclosed by the embodiment of the invention, 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 the instance of the attribute type, the attribute name of the instance is set as the key name, and the attribute value is set as the key value, so that the conversion from the JSON object with the key value type as the dictionary to the model object is realized; when the key value is an array and the array element is marked as a JSON model object, creating an instance of the model object to realize the conversion from the JSON object with the key value type as the array to the model object; and setting a condition for acquiring the original key value, and executing the acquisition of the original key value under the condition of meeting the condition so as to update the original key value when the acquired original key value is not empty.
FIG. 7 is a schematic diagram of the main modules of an apparatus for converting a model object into a JSON object according to an embodiment of the present invention. As shown in fig. 7, an apparatus 700 for converting a model object into a JSON object according to an embodiment of the present invention mainly includes:
the first obtaining module 701 is configured to obtain an attribute of a predefined model object to obtain an attribute name corresponding to the attribute. The model object is a self-defined tool class or a subclass of the tool class, and comprises one or more attributes, wherein the attributes comprise one or more attribute types, such as int, long, NSString, a self-defined model object or a subclass thereof, an array, and an array element is a self-defined model object. In the step, a class _ copyPropertyList interface is called to obtain the attribute of the model object; and then, calling a property _ getName interface to acquire a property name corresponding to the property.
A second obtaining module 702, configured to obtain an attribute value corresponding to the attribute name from the instantiated model object. The model object is instantiated, then an interface of [ NSObjectdiactionWithValuesForKeys ] is called, the attribute value corresponding to the attribute name can be obtained, and whether the attribute value is an example of a tool class, an array or other types needs to be judged subsequently, so that different processing is carried out according to the judgment result.
A generating module 703, configured to determine whether the attribute value is an instance of a predefined tool class, and if the attribute value is the instance of the tool class, generate a JSON object through a recursive algorithm, where the tool class is a parent class of the model object. In an embodiment, the tool class is a JDPayJSONModel class. The JDPayJSONModel class inherits from the NSObject class, and the inside of the JDPayJSONModel class only contains codes of about 280 lines, and the JDPayJSONModeltoJSONDirectional class provides a- [ JDPayJSONModeltoJSONDirectional ] interface which is used for converting a model object into a JSON object; providing a JSON-enabled array attribute marking interface + [ JDPayJSONModelsetJSONAbleElementClass: forArrayProperty ] for marking that an element in a certain array class attribute is a JSON-enabled JDPayJSONModel type; the conversion of the JSON object to the model object is realized by reloading the interface of the [ NSObject setValue: ] of the parent class, and the conversion of the JSON object to the model object is realized by calling the interface of the [ NSObject setValuesForKeyWithDictionary: ]. When the user uses the method, the user only needs to create subclasses of JDPayJSONModel classes, and the model object and the JSON object can be exchanged. The step is used for judging whether the attribute value is nested with the model object, and if so, the nested model object is converted into the JSON object by using a recursive algorithm. The JSON object may then be stored in a dictionary in the form of key-value pairs. The purpose of converting the nested model objects into JSON objects one by one is achieved through the modules.
In addition, the apparatus 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 mark judging module (not shown in fig. 7). The first processing module is configured to determine whether the attribute value is an array, and if the attribute value is an array and an array element of the array is marked as a JSON-enabled model object, determine whether the array element is an instance of the model object; and when the array element of the array is the instance of the model object, converting the array element into the JSON object. The mark judging module is used for marking the array elements as JSON model objects; and taking the class name and the attribute name of the model object as keys, and taking the array element as a value to store so as to judge whether the array element of the array is marked as a JSON model object.
From the above description, it can be seen that, by obtaining the attribute value of the model object, determining the type of the attribute value, and if the attribute value is the predefined instance of the tool class, generating the JSON object by using a recursive algorithm, the conversion from the model object nested with other model objects to the JSON object is realized; when the attribute value is an array, and the array element is marked as a JSON model object and is an instance of the model object, converting the array element into the JSON object, thereby realizing the conversion from the model object with the array element being self-defined to the JSON object; the array elements are marked as JSON model objects in advance, and are stored by key value pairs, so that whether the array meets the conversion condition or not is conveniently determined; a condition for acquiring the attribute name is set to execute acquisition of the attribute name in a case where the condition is satisfied.
FIG. 8 is a schematic diagram of the main modules of an apparatus for converting JSON objects into model objects according to an embodiment of the present invention. As shown in fig. 8, the apparatus 800 for converting a JSON object into a model object according to an embodiment of the present invention mainly includes:
the determining module 801 is configured to obtain a type of a key value of the JSON object, and determine whether the type of the key value is a dictionary. The JSON Object refers to a dictionary data type in an Object-C language, and can be converted into standard JSON character string data through an API (application programming interface). The data format of the JSON object is as follows: a key name/key-value pair comprising a field name and a corresponding value, both in a double quotation mark, with ": "separate, such as" firstName ": John". The type of key value may be a string, a value, a dictionary, an array, etc. And calling an interface of [ NSObject isKindOfClass: ] to acquire the type of the key value of the pre-defined JSON object.
A searching module 802, configured to obtain an attribute of a predefined model object if the type of the key value is a dictionary, so as to search for an attribute type associated with the key name of the JSON object. And calling a class _ copyPropertyList interface to acquire all attributes of the model object, wherein the model object is the JDPayJSONModel class in the embodiment. And finding out the attribute name of the model object which is the same as the key name through enumeration, wherein the attribute type corresponding to the attribute name is the attribute type associated with the key name.
A first setting module 803, configured to obtain an original key value of the JSON object, and if the original key value is null, create an instance of the class corresponding to the attribute type, so as to set an attribute name of the instance as the key name, and set an attribute value corresponding to the attribute name as a preset key value. The module firstly judges whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, a valueForKey interface is called to obtain an original key value of the JSON object. And then, judging whether the original key value is null (nil), if so, creating an instance of the class corresponding to the attribute type, calling setValue: forKey, setting an attribute name as the key name of the JSON object by using an interface, and setting an attribute value corresponding to the attribute name as a preset key value.
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 setup module (not shown in fig. 8). The second processing module is configured to determine whether the type of the key value is an array, and create an instance of the model object if the type of the key value is an array and an array element of the array is marked as a JSON-enabled model object; and setting the attribute name of the instance as the key name, and setting the attribute value of the instance as the value of the array element. The second setting module is configured to determine whether the original key value is null, and update the original key value to a preset key value if the original key value is not null; and setting the attribute name as the key name and setting the attribute value as the original key value.
From the above description, it can be seen 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 further, the attribute name of the instance is set as the key name, and the attribute value is set as the key value, so that the conversion from the JSON object with the key value type as the dictionary to the model object is realized; when the key value is an array and the array element is marked as a JSON model object, creating an instance of the model object to realize the conversion from the JSON object with the key value type as the array to the model object; and setting a condition for acquiring the original key value, and executing the acquisition of the original key value under the condition of meeting the condition so as to update the original key value when the acquired original key value is not empty.
FIG. 9 illustrates an exemplary system architecture 900 of a method or apparatus for model object to JSON object interoperability to which embodiments of the present invention may be applied.
As shown in fig. 9, the system architecture 900 may include end devices 901, 902, 903, a network 904, and a server 905. Network 904 is the medium used to provide 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 fiber optic cables, to name a few.
A user may use the terminal devices 901, 902, 903 to interact with a server 905 over a network 904 to receive or send messages and the like. The terminal devices 901, 902, 903 may have various communication client applications installed thereon, such as a shopping application, a web browser application, a search application, an instant messaging tool, a mailbox client, social platform software, and the like.
The terminal devices 901, 902, 903 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The server 905 may be a server providing various services, such as a background management server that analyzes predefined model objects or JSON objects transmitted by an administrator using the terminal devices 901, 902, 903. The background management server may analyze and perform other processing on the received model object or JSON object, and feed back a processing result (e.g., a conversion result between the model object and the JSON object) to the terminal device.
It should be noted that the method for model object and JSON object interaction provided in the embodiment of the present application is generally executed by the server 905, and accordingly, the device for model object and JSON object interaction is generally disposed in the server 905.
It should be understood that the number of terminal devices, networks, and servers in fig. 9 is merely illustrative. There may be any number of terminal devices, networks, and servers, as desired for implementation.
The invention also provides an electronic device and a computer readable medium according to the embodiment of the invention.
The electronic device of the present invention includes: one or more processors; the storage device is used 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 realize the method for converting the model object and the JSON object.
The computer readable medium of the present invention stores thereon a computer program, which when executed by a processor implements a method for model object and JSON object transformation according to an embodiment of the present invention.
Referring now to FIG. 10, shown is a block diagram of a computer system 1000 suitable for use in implementing an electronic device of an embodiment of the present invention. The electronic device shown in fig. 10 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present invention.
As shown in fig. 10, the computer system 1000 includes a Central Processing Unit (CPU)1001 that 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 section 1008 into a Random Access Memory (RAM) 1003. In the RAM 1003, various programs and data necessary for the operation of the computer system 1000 are also stored. The CPU 1001, ROM 1002, and RAM 1003 are connected to each other via a bus 1004. An input/output (I/O) interface 1005 is also connected to bus 1004.
The following components are connected to the I/O interface 1005: an input section 1006 including a keyboard, a mouse, and the like; an output section 1007 including a display such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 1008 including a hard disk and the like; and a communication section 1009 including a network interface card such as a LAN card, a modem, or the like. The communication section 1009 performs communication processing via a network such as the internet. The driver 1010 is also connected to the I/O interface 1005 as necessary. A removable medium 1011 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 1010 as necessary, so that a computer program read out therefrom is mounted into the storage section 1008 as necessary.
In particular, the processes described above with respect to the main step diagrams may be implemented as computer software programs, according to embodiments of the present disclosure. For example, embodiments of the present disclosure include a computer program product comprising a computer program embodied on a computer readable medium, the computer program containing program code for performing the method illustrated in the main step diagram. In such an embodiment, the computer program may be downloaded and installed from a network through the communication part 1009 and/or installed from the removable medium 1011. The computer program executes the above-described functions defined in the system of the present invention when executed by the Central Processing Unit (CPU) 1001.
It should be noted that the computer readable medium shown in the present invention can be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, 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 disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing. In the present invention, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In the present invention, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, fiber optic cable, RF, etc., or any suitable combination of the foregoing.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams or flowchart illustration, and combinations of blocks in the block diagrams or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The modules described in the embodiments of the present invention may be implemented by software or hardware. The described modules may also be provided in a processor, which may be described as: a processor comprises a first obtaining module, a second obtaining module, a generating module, a judging module, a searching module and a setting module. The names of the modules do not form a limitation on the module itself in some cases, for example, the first obtaining module may also be described as a "module that obtains attributes of a predefined model object to obtain attribute names corresponding to the attributes".
As another aspect, the present invention also provides a computer-readable medium that may be contained in the apparatus described in the above embodiments; or may be separate and not incorporated into the device. The computer readable medium carries one or more programs which, when executed by a device, cause the device to comprise: acquiring attributes of a predefined model object to obtain attribute names corresponding to the attributes; acquiring an attribute value corresponding to the attribute name from the instantiated model object; and judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the 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. Obtaining the type of a key value of the JSON object, and judging whether the type of the key value is a dictionary or not; if the type of the key value is a dictionary, acquiring the attribute of a predefined model object to search an attribute type associated with the key name of the JSON object; and creating an instance of the class corresponding to the attribute type, setting the attribute name of the instance as the key name, and setting the attribute value corresponding to the attribute name as the key value.
From the above description, it can be seen that, by obtaining the attribute value of the model object, determining the type of the attribute value, and if the attribute value is the predefined instance of the tool class, generating the JSON object by using a recursive algorithm, the conversion from the model object nested with other model objects to the JSON object is realized; when the attribute value is an array, and the array element is marked as a JSON model object and is an instance of the model object, converting the array element into the JSON object, thereby realizing the conversion from the model object with the array element being self-defined to the JSON object; the array elements are marked as JSON model objects in advance, and are stored by key value pairs, so that whether the array meets the conversion condition or not is conveniently determined; setting a condition for acquiring the attribute name to execute acquisition of the attribute name if the condition is satisfied; when the type of the key value of the JSON object is a dictionary, searching an attribute type associated with the key name of the JSON object to create an instance of the attribute type, setting the attribute name of the instance as the key name and setting the attribute value as the key value, and realizing the conversion from the JSON object with the key value type as the dictionary to a model object; when the key value is an array and the array element is marked as a JSON model object, creating an instance of the model object to realize the conversion from the JSON object with the key value type as the array to the model object; and setting a condition for acquiring the original key value, and executing the acquisition of the original key value under the condition of meeting the condition so as to update the original key value when the acquired original key value is not empty.
The product can execute the method provided by the embodiment of the invention, and has corresponding functional modules and beneficial effects of the execution method. For technical details that are not described in detail in this embodiment, reference may be made to the method provided by the embodiment of the present invention.
The above-described embodiments should not be construed as limiting the scope of the invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions can occur, depending on design requirements and other factors. Any modification, equivalent replacement, and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims (16)

1. A method for converting a model object to a JSON object, comprising:
acquiring attributes of a predefined model object to obtain attribute names corresponding to the attributes;
acquiring an attribute value corresponding to the attribute name from the instantiated model object;
and judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the 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.
2. The method according to claim 1, wherein after the step of obtaining the attribute value corresponding to the attribute name from the instantiated model object, the method further comprises:
judging whether the attribute value is an array or not, and if the attribute value is the array and the array element of the array is marked as a JSON model object, judging whether the array element is an example of the model object or not;
and when the array element of the array is the instance of the model object, converting the array element into the JSON object.
3. The method of claim 2, further comprising:
marking the array elements as JSON-enabled model objects;
and taking the class name and the attribute name of the model object as keys, and taking the array element as a value to store so as to judge whether the array element of the array is marked as a JSON model object.
4. The method according to any one of claims 1 to 3, wherein obtaining the attribute name corresponding to the attribute comprises:
acquiring an attribute type corresponding to the attribute, and judging whether the attribute type is a preset supported attribute type;
if the attribute type is the supported attribute type, judging whether a class corresponding to the attribute type is an Object-C class;
if the class corresponding to the attribute type is an Object-C class, acquiring a class name corresponding to the attribute type, and judging 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 the subclass of the tool class, judging whether the class name corresponding to the attribute type is an Object-C basic class or not;
and when the class of the attribute type is not the Object-C class, or the class name corresponding to the attribute type is the subclass of the tool class, or the class name corresponding to the attribute type is the Object-C basic class, acquiring the attribute name corresponding to the attribute.
5. A method for converting a JSON object to a model object, comprising:
acquiring the type of a key value of a JSON object, and judging whether the type of the key value is a dictionary or not;
if the type of the key value is a dictionary, acquiring the attribute of a predefined model object to search an attribute type associated with the key name of the JSON object;
and acquiring an original key value of the JSON object, if the original key value is null, creating an instance of the class corresponding to the attribute type, setting an attribute name of the instance as the key name, and setting an attribute value corresponding to the attribute name as a preset key value.
6. The method of claim 5, further comprising:
judging whether the type of the key value is an array or not, and if the type of the key value is the array and the array element of the array is marked as a JSON model object, creating an example of the model object;
and setting the attribute name of the instance as the key name, and setting the attribute value of the instance as the value of the array element.
7. The method according to claim 5 or 6, wherein the obtaining of the original key value of the JSON object comprises:
judging whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, acquiring an original key value of the JSON object;
the method further comprises the following steps: judging whether the original key value is empty, and if not, updating the original key value to a preset key value;
and setting the attribute name as the key name, and setting the attribute value as the original key value.
8. An apparatus for converting a model object to a JSON object, comprising:
the first acquisition module is used for acquiring the attribute of a predefined model object to obtain an attribute name corresponding to the attribute;
the second obtaining module is used for obtaining the attribute value corresponding to the attribute name from the instantiated model object;
and the generating module is used for judging whether the attribute value is an instance of a predefined tool class, and if the attribute value is the instance of the tool class, generating the JSON object by a recursive algorithm, wherein the tool class is a parent class of the model object.
9. The apparatus of claim 8, further comprising: a first processing module for
Judging whether the attribute value is an array or not, and if the attribute value is the array and the array element of the array is marked as a JSON model object, judging whether the array element is an example of the model object or not; and
and when the array element of the array is the instance of the model object, converting the array element into the JSON object.
10. The apparatus of claim 9, further comprising: a mark judgment module for
Marking the array elements as JSON-enabled model objects; and
and taking the class name and the attribute name of the model object as keys, and taking the array element as a value to store so as to judge whether the array element of the array is marked as a JSON model object.
11. The apparatus according to any one of claims 8 to 10, wherein the first obtaining module is further configured to:
acquiring an attribute type corresponding to the attribute, and judging whether the attribute type is a preset supported attribute type;
if the attribute type is the supported attribute type, judging whether a class corresponding to the attribute type is an Object-C class;
if the class corresponding to the attribute type is an Object-C class, acquiring a class name corresponding to the attribute type, and judging 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 the subclass of the tool class, judging whether the class name corresponding to the attribute type is an Object-C basic class or not; and
and when the class of the attribute type is not the Object-C class, or the class name corresponding to the attribute type is the subclass of the tool class, or the class name corresponding to the attribute type is the Object-C basic class, acquiring the attribute name corresponding to the attribute.
12. An apparatus for converting a JSON object to a model object, comprising:
the judging module is used for acquiring the type of the key value of the JSON object and judging whether the type of the key value is a dictionary or not;
the searching module is used for acquiring the attribute of a predefined model object if the type of the key value is a dictionary so as to search the attribute type associated with the key name of the JSON object;
and the first setting module is used for acquiring an original key value of the JSON object, if the original key value is null, creating an instance of the class corresponding to the attribute type, setting the attribute name of the instance as the key name, and setting the attribute value corresponding to the attribute name as a preset key value.
13. The apparatus of claim 12, further comprising: a second processing module for
Judging whether the type of the key value is an array or not, and if the type of the key value is the array and the array element of the array is marked as a JSON model object, creating an example of the model object; and
and setting the attribute name of the instance as the key name, and setting the attribute value of the instance as the value of the array element.
14. The apparatus of claim 12 or 13, wherein the first setting module is further configured to:
judging whether the attribute type is the subclass of the model object, and if the attribute type is the subclass of the model object, acquiring an original key value of the JSON object;
the device further comprises: the second setting module is used for judging whether the original key value is null or not, and if the original key value is not null, updating the original key value to a preset key value; and
and setting the attribute name as the key name, and setting the attribute value as the original key value.
15. An electronic device, comprising:
one or more processors;
a storage device for storing one or more programs,
when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1-7.
16. A computer-readable medium, on which a computer program is stored, which, when being executed by a processor, carries out the method according to any one of claims 1-7.
CN201810833719.1A 2018-07-26 2018-07-26 Method and device for mutual conversion between model object and JSON object Pending CN110764768A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810833719.1A CN110764768A (en) 2018-07-26 2018-07-26 Method and device for mutual conversion between model object and JSON object

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810833719.1A CN110764768A (en) 2018-07-26 2018-07-26 Method and device for mutual conversion between model object and JSON object

Publications (1)

Publication Number Publication Date
CN110764768A true CN110764768A (en) 2020-02-07

Family

ID=69327397

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810833719.1A Pending CN110764768A (en) 2018-07-26 2018-07-26 Method and device for mutual conversion between model object and JSON object

Country Status (1)

Country Link
CN (1) CN110764768A (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112130860A (en) * 2020-09-23 2020-12-25 北京奇艺世纪科技有限公司 JSON object analysis method and device, electronic device and storage medium
CN113282274A (en) * 2020-02-20 2021-08-20 北京京东振世信息技术有限公司 Data processing method and device

Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6035303A (en) * 1998-02-02 2000-03-07 International Business Machines Corporation Object management system for digital libraries
US20100185869A1 (en) * 2009-01-20 2010-07-22 International Business Machines Corporation Method and system for signing javascript object notation (json) messages
US20100185862A1 (en) * 2009-01-20 2010-07-22 International Business Machines Corporation Method and System for Encrypting JavaScript Object Notation (JSON) Messages
CN102906697A (en) * 2010-06-03 2013-01-30 国际商业机器公司 Method and system of adapting data model to user interface component
US20130332898A1 (en) * 2012-06-07 2013-12-12 Microsoft Corporation Object extensions using attributes to decouple base classes from derived classes
CN103886046A (en) * 2014-03-11 2014-06-25 中国信息安全测评中心 Automatic semanteme extraction method for Web data exchange
CN104407863A (en) * 2014-11-21 2015-03-11 用友软件股份有限公司 Abstract control model programming device and method
CN105159928A (en) * 2015-08-05 2015-12-16 深圳联友科技有限公司 Method and system for converting JSON data into model data
CN105468606A (en) * 2014-08-25 2016-04-06 小米科技有限责任公司 Webpage saving method and device
EP3211853A1 (en) * 2016-02-26 2017-08-30 Mitsubishi Electric R & D Centre Europe B.V. Real-time validation of json data applying tree graph properties
CN108008936A (en) * 2017-12-04 2018-05-08 北京奇艺世纪科技有限公司 A kind of data processing method, device and electronic equipment
US20180129959A1 (en) * 2016-11-10 2018-05-10 General Electric Company Methods and systems for programmatically selecting predictive model parameters
CN108196844A (en) * 2018-01-04 2018-06-22 武汉斗鱼网络科技有限公司 Information processing method, device, electronic equipment and storage medium

Patent Citations (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6035303A (en) * 1998-02-02 2000-03-07 International Business Machines Corporation Object management system for digital libraries
US20100185869A1 (en) * 2009-01-20 2010-07-22 International Business Machines Corporation Method and system for signing javascript object notation (json) messages
US20100185862A1 (en) * 2009-01-20 2010-07-22 International Business Machines Corporation Method and System for Encrypting JavaScript Object Notation (JSON) Messages
CN102906697A (en) * 2010-06-03 2013-01-30 国际商业机器公司 Method and system of adapting data model to user interface component
US20130332898A1 (en) * 2012-06-07 2013-12-12 Microsoft Corporation Object extensions using attributes to decouple base classes from derived classes
CN103886046A (en) * 2014-03-11 2014-06-25 中国信息安全测评中心 Automatic semanteme extraction method for Web data exchange
CN105468606A (en) * 2014-08-25 2016-04-06 小米科技有限责任公司 Webpage saving method and device
CN104407863A (en) * 2014-11-21 2015-03-11 用友软件股份有限公司 Abstract control model programming device and method
CN105159928A (en) * 2015-08-05 2015-12-16 深圳联友科技有限公司 Method and system for converting JSON data into model data
EP3211853A1 (en) * 2016-02-26 2017-08-30 Mitsubishi Electric R & D Centre Europe B.V. Real-time validation of json data applying tree graph properties
US20180129959A1 (en) * 2016-11-10 2018-05-10 General Electric Company Methods and systems for programmatically selecting predictive model parameters
CN108008936A (en) * 2017-12-04 2018-05-08 北京奇艺世纪科技有限公司 A kind of data processing method, device and electronic equipment
CN108196844A (en) * 2018-01-04 2018-06-22 武汉斗鱼网络科技有限公司 Information processing method, device, electronic equipment and storage medium

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN113282274A (en) * 2020-02-20 2021-08-20 北京京东振世信息技术有限公司 Data processing method and device
CN113282274B (en) * 2020-02-20 2023-09-22 北京京东振世信息技术有限公司 Data processing method and device
CN112130860A (en) * 2020-09-23 2020-12-25 北京奇艺世纪科技有限公司 JSON object analysis method and device, electronic device and storage medium
CN112130860B (en) * 2020-09-23 2023-09-05 北京奇艺世纪科技有限公司 JSON object analysis method and device, electronic equipment and storage medium

Similar Documents

Publication Publication Date Title
CN107402746B (en) Method and device for automatically generating code file
CN110689268B (en) Method and device for extracting indexes
CN110706093A (en) Accounting processing method and device
CN109766127B (en) Method for updating application version information
CN113050940A (en) Method for previewing small program, related device and computer program product
CN113760276A (en) Method and device for generating page code
CN110764768A (en) Method and device for mutual conversion between model object and JSON object
CN114443076A (en) Mirror image construction method, device, equipment and storage medium
CN112818026A (en) Data integration method and device
CN115495175A (en) Picture display method and device, terminal equipment and computer medium
CN113536748A (en) Method and device for generating chart data
CN112905225A (en) Method and device for creating continuous integration tool construction task
CN112256254A (en) Method and device for generating layout code
CN113760240B (en) Method and device for generating data model
CN115526587A (en) Method and device for generating customs message
CN114661402A (en) Interface rendering method and device, electronic equipment and computer readable medium
CN114036397A (en) Data recommendation method and device, electronic equipment and medium
CN113448960A (en) Method and device for importing form file
CN112596729A (en) Target application interface generation method and device, readable medium and electronic equipment
CN111078230A (en) Code generation method and device
CN111143408A (en) Event processing method and device based on business rules
CN112948472A (en) Data processing method, device, equipment and computer readable medium
CN113760344A (en) Dynamic configuration method, device, electronic equipment and storage medium
CN111737372A (en) Map data generation method and device
CN114969059B (en) Method and device for generating order information, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
CB02 Change of applicant information

Address after: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant after: Jingdong Technology Holding Co.,Ltd.

Address before: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant before: Jingdong Digital Technology Holding Co.,Ltd.

Address after: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant after: Jingdong Digital Technology Holding Co.,Ltd.

Address before: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant before: JINGDONG DIGITAL TECHNOLOGY HOLDINGS Co.,Ltd.

Address after: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant after: JINGDONG DIGITAL TECHNOLOGY HOLDINGS Co.,Ltd.

Address before: 101111 Room 221, 2nd Floor, Block C, 18 Kechuang 11th Street, Beijing Economic and Technological Development Zone

Applicant before: BEIJING JINGDONG FINANCIAL TECHNOLOGY HOLDING Co.,Ltd.

CB02 Change of applicant information