About the common method for object mapping on iOS-standby

Source: Internet
Author: User

For specific use, please see the instructions below, or see the unit test code in the project. Or, refer to the original document:

Https://github.com/mystcolor/JTObjectMapping

How to use

========

In most cases, a method is needed:

[nsobject objectfromjsonobject: stateobject mapping:nil];

In which, replace the nsobject with the name of the class we want to map into.

Slightly more complicated, assuming that the properties of the class and the key names in the JSON are not one by one corresponding, then write a mapping relationship table as the mapping parameter:

nsdictionary *mapping = [nsdictionary dictionarywithobjectsandkeys:

@ "MyState", @ "state",

@ "Movielist", @ "movie",

Nil];

[nsobject objectfromjsonobject: stateobject mapping:mapping];

If it is more complicated, we need to map an array, each element of the array is mapped to an object, then the 2nd method is used:

[nsobject mappingwithkey:@ "responsestate" mapping:nil]

Similarly, replace the nsobject with the corresponding class for the array element, where key is the property that holds the entire array.

Attention

====

The strings in JSON are automatically mapped to nsnumber or int, without the need for special code. If you want to map a string date to a nsdate type, please refer to the original document.

Let me give you a concrete example

===========

Let's say we have a JSON like this:

{

"State": [

{

"Respcode": "0000",

"Respmsg": "1"

}

],

"Movie": [

{

"Body": "At first, this is just the story of the Leaf asked him born in Foshan,"

"Pics": "/images/jz/4779-1-20121225133147.jpg,/images/jz/4779-2-20121225133147.jpg",

"Name": "The Guru of the Generation",

"Movid": "4779",

"Length": "120",

"Hasplan": "1",

"Trailor": "/4779.M4V",

"Director": "Tony Leung Ziyi Zhang Zhang Zhen",

"Type": "Action/Biography/Plot/imax",

"url": "http://img.fun-guide.mobi/show?src=http://zhaohang.fun-guide.mobi:8080/tm/images/hb/4779- 20121225133147.jpg&w=140 ",

"Popularity": "6"

},

{

...

}

]

}

Now, you want to map it to a custom object. There are several situations:

In the first case, simply map the first element inside the state array into a Fgstatemodel class

So, take out this node:

nsdictionary *stateobject = [self. JSON Objectforkey:@ "state"] Objectatindex:0];

Then, perform the mapping:

self. state = [fgstatemodel objectfromjsonobject: stateobject mapping:nil ];

In the second case, the array of movie dictionaries is mapped into an array, and each element of the array is Fgmoviemodel

Nsarray *movieobject = [self. JSON Objectforkey:@ "movie"];

self. movielist = [fgmoviemodel objectfromjsonobject: movieobject mapping:Nil ];

In the third case, if the object's properties are different from the JSON name, you can use the mapping table, for example, we want to map the entire JSON object to a Fgresponsemodel object, where the state array is mapped to the ResponseState property, and the movie array maps to The Movielist property. So, we need to tell the mapper what object the elements in this array are mapped to.

So, by mapping this argument, tell it: "Please map each element in the state array in JSON into a Fgstatemodel object, and put the final array into the responsestate attribute." In addition, the array of the movie is mapped into an Fgmoviemodel array and saved to the movielist attribute. "

nsdictionary *mapping = [nsdictionary dictionarywithobjectsandkeys:

[fgstatemodel Mappingwithkey:@ "responsestate" Mapping:nil], @ "state",

[fgmoviemodel Mappingwithkey:@ "movielist" Mapping:nil], @ "movie",

Nil];

self. Response = [fgresponsemodel -objectfromjsonobject: Self. JSON mapping: mapping];

About the common method for object mapping on iOS-standby

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.