-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonHelper.m
41 lines (32 loc) · 1.21 KB
/
JsonHelper.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// JsonHelper.m
// xoc
//
// Copyright 2012 artmobile@gmail.com. All rights reserved.
//
#import "JsonHelper.h"
#import "JSONKit.h"
@implementation JsonHelper
/*!
@method get:
@abstract Retrieves JSON from provided URL.
@discussion This function will return a Dictionary populated from JSON response.
@param URL to fetch JSON from and the timeInterval to wait for response.
@result A newly-created and autoreleased NSURLRequest instance.
*/
+ (NSDictionary*) get: (NSString*) url timeoutInterval:(NSTimeInterval) timeoutInterval {
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:timeoutInterval];
NSHTTPURLResponse* response;
NSError* err;
NSData* jsonData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
if([response statusCode] == 200){
JSONDecoder* decoder = [[JSONDecoder alloc] init];
NSDictionary *dict = [decoder objectWithData:jsonData];
return dict;
}
else
return [NSDictionary alloc];
}
@end