THIS CONTENT DOWNLOAD SHORTLY

Objective

This is a quick guide on JSON Parse in iOS. A simple example described how to parse JSON and store JSON data in array or dictionary for further use.

 

Introduction:

JSON stands for JavaScript Object Notation. It is data interchange format that can be easily read and write by human and can be easily parse by machine. Given steps are very simple and it explain in simple manner with suitable code, to understand parse JSON in iOS we need to follows given steps.

 

Step 1 Create XCode Project

Create new XCode project name it as JSONParsingDemo. It contains one UIViewController in Main.storyboard file. The UIViewController class provides the fundamental view-management model for all iOS apps.

 

Step 2 Parse JSON File

The given code describe how to convert json file into NSData. Then pass that data object in NSJSONSerialization method JSONObjectWithData:options:error. That will return array or dictionary as per JSON format.

The NSJSONSerialization class to convert JSON to Foundation objects and convert Foundation objects to JSON.

-(void)parseJSONFile
{
NSData *data = [NSDatadataWithContentsOfFile:[[NSBundlemainBundle] pathForResource:@"Displaydata" ofType:@"json"]];
 
NSDictionary *dictTemp = [NSJSONSerializationJSONObjectWithData:data options:0 error:nil];
NSArray *arrColors = [dictTempvalueForKey:@"colorsArray"];
 
for (inti=0; i<arrColors.count; i++) {
        Colors *colors = [[Colors alloc] init];
        [colorsparseResponse:[arrColorsobjectAtIndex:i]];
        [marrAllColorsaddObject:colors];
    }
 
    [selfdisplayAllColros];
}
 
 

Step 3 Create Colors Model File

Create a model class that is subclass of NSObject class. That contains all the property with the keys available in JSON dictionary and write a parse JSON dictionary method and store it in class property.

In our example we have created Colors class as below.

Colors.h:

#import<Foundation/Foundation.h>
 
@interface Colors : NSObject
 
@property (nonatomic,strong) NSString *colorName;
@property (nonatomic,strong) NSString *hexValue;
 
- (int)parseResponse:(NSDictionary *)receivedObjects;
 
@end

Colors.m:

#import "Colors.h"
 
@implementation Colors
 
@synthesizecolorName;
@synthesizehexValue;
 
- (int)parseResponse:(NSDictionary *)receivedObjects
{
colorName = [receivedObjectsobjectForKey:@"colorName"];
hexValue = [receivedObjectsobjectForKey:@"hexValue"];
return 0;
}
 
@end
 
 

Step 4 Display Colors Function

In our this example it contains array of colors with key colorsArray. Create color object and call parseResponse method and pass dictionary which contains colorname and its hexvalue. Add color object in a mutable array.

Now you can use color object and its colorname and hexvalue property in whole class. To describe how to access color object’s property I have print colorname and hexvalue in displayAllColors method.

-(void)displayAllColors
{
for (inti=0; i<marrAllColors.count; i++) {
        Colors *color = [marrAllColorsobjectAtIndex:i];
NSLog(@"Color : Name = %@ hexValue = %@",color.colorName,color.hexValue);
    }
}
 
 

Step 5 call function from viewDidLoad

Call parseJSONFile method from viewDidLoad() which looks like.

(void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    marrAllColors = [[NSMutableArray alloc] init];
    
    [self parseJSONFile];
}

I hope you found this blog helpful while JSON Parse in iOS. Let me know if you have any questions or concerns regarding iOS, please put a comment here and we will get back to you ASAP. Got an Idea of iPhone Application Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am iOS developer with an aspiration of learning new technology and creating a bright future in Information Technology.

face mask Belial The Demon Headgear Pocket Staff Magic