Get the App
SLTechnology News&Howtos  ›  Development  › 

What is JSONModel?

Shulou Source: shulou.com Published: 2022-06-02 11:36:17 10月06日 Update

This article mainly shows you "what is JSONModel", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn this article "what is JSONModel?"

JSONModel is an open source library. Using this library, you can easily solve problems such as changing dictionaries to models and solve key-value pairs that do not match exactly (that is, when KVC assignment cannot be used).

[size=1em] first define a class that inherits JSONModel

[size=1em] / / this class JuBaModel is the base class of the model, and later models will inherit JuBaModel (the name of this class is prefixed with the name of my project, and readers can define it themselves)

[size=1em] # import

[size=1em] # import "JSONModel.h"

[size=1em] @ interface JuBaModel: JSONModel

[size=1em] @ end

[size=1em] # import "JuBaModel.h"

[size=1em] @ implementation JuBaModel

[size=1em] / * this is the method of JSONModel. The purpose is to convert the _ mode named to the Camel named method.

[size=1em] [size=1em] because the server-side naming words are separated by _, and OC is named in Camel nomenclature.

[size=1em] [size=1em] for example: hello_word in JSON and helloWord should be defined in the model (so that it can be matched automatically)

[size=1em] [size=1em] * /

[size=1em] + (JSONKeyMapper *) keyMapper {

[size=1em] [size=1em] return [size=1em] [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase]

[size=1em]}

[size=1em] / * this ensures that the attributes in JSON are selectable

[size=1em] [size=1em] that is, if there are more attributes in Model, it doesn't matter if you have fewer attributes.

[size=1em] [size=1em] when assigning values to a model with a dictionary, JSONModel can always find an one-to-one correspondence

[size=1em] [size=1em] for example, there is a key hell0_word username password in JSON and only username password in the model

[size=1em] [size=1em] then when you assign a value automatically, the key corresponding to username password will be assigned automatically, and KVC cannot be used in this case.

[size=1em] [size=1em] * /

[size=1em] + ([size=1em] Bool [size = 1em]) propertyIsOptional: (NSString *) propertyName {

[size=1em] [size=1em] return [size=1em] YES

[size=1em]}

@ end

The above is all the content of this article "what is JSONModel?" thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Models contents methods articles dictionaries attributes situations methods learning help one-to-one correspondence it doesn't matter that is prefixes words only always can easy to understand more server Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Redmi Shulou Information Apple Microsoft