Mjextension JSON Quick Parse

Source: Internet
Author: User

Mjextension

To make a long story short, let's see how we use a column

1. Pull the frame into your project first

2, first of all I use the JSON outermost is a dictionary, according to the model of the data we can classify this as an array in the dictionary, there are other in the array for this situation I need to build two data model one is to get the array of the other array in the city element

Explain the idea behind the code

#import <Foundation/Foundation.h>

@interface Nscity:nsobject
@property (Nonatomic,strong) NSString *city;
@property (Nonatomic,strong) NSString *h5_url;
@property (Nonatomic,strong) nsnumber* ID;
@property (Nonatomic,strong) NSString *deal_url;
@end

Nscity.m
mjextension Example
//
Created by Tareba on 15/12/18.
COPYRIGHT©2015 year Tanada. All rights reserved.
//

#import "NSCity.h"

@implementation nscity
-(void) SetValue: (ID) value Forundefinedkey: (NSString *) Key {
if ([Key isequaltostring:@ "id"]) {
Self.id=value;
}
}
@end

#import <Foundation/Foundation.h>

@interface Nsdeals:nsobject
@property (Nonatomic,strong) Nsmutablearray *deals;

@end

#import "NSDeals.h"
#import "MJExtension.h"
@implementation Nsdeals

The following methods are key

、、
+ (Nsdictionary *) Mj_objectclassinarray
{
Return @{
@ "Deals": @ "Nscity",

};
}
@end

#import "ViewController.h"
#import "MJExtension.h"

#import "NSCity.h"
@interface Viewcontroller ()

@end
@implementation Viewcontroller

-(void) Viewdidload {
[Super Viewdidload];
Nsurlrequest *request =[nsurlrequestrequestwithurl:[nsurlurlwithstring:@ "Http://api.dianping.com/v1/deal/find_ Deals?appkey=4123794720&category=%e7%be%8e%e9%a3%9f&city=%e9%83%91%e5%b7%9e&region=%e9%87%91%e6%b0 %b4%e5%8c%ba&sort=2&sign=fb7a61eb742a4b697b666985348614c7a8f763a3 "];
Nsurlsession *seesion = [Nsurlsessionsharedsession];
Nsurlsessiondatatask *task =[seesiondatataskwithrequest:request completionhandler:^ (NSData * _Nullable data, Nsurlresponse * _nullable response, Nserror * _nullable error) {

Nsdictionary *dic=[nsjsonserialization JSONObjectWithData:dataoptions:NSJSONReadingMutableContainers Error:nil];


Nsdeals *result =[nsdealsmj_objectwithkeyvalues:dic];


For (nscity *city inresult.deals) {
NSLog (@ "%lu", (unsigned long) result.deals.count);
NSLog (@ "%@", City.deal_url);
}

}];
[Task resume];
}

@end

This method is to change the hierarchical relationship of JSON data into an attribute relationship, the next layer becomes the property of the previous layer, and then the property can be accessed directly by the elder sister. As for the keyword to become a property, due to the urgency of the future and the validation and the usual use of setvaluefor Undefinekey to resolve whether the same will be verified as soon as possible after the modification

Mjextension JSON Quick Parse

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.