2013-10-25 54 views
1

我正在尋找Restkit 2的一個很好的教程。我看到的每個地方都在討論Object Mapping。是不是可以使用Restkit並獲取JSON作爲字符串,然後直接使用JSON。Restkit 2 JSON示例

+0

你看了RestKit GitHub的頁面上的文檔? – Wain

+0

我完全沒有經過它,我完全不熟悉ios開發和restkit,我一直在尋找它 – Noor

+1

不要使用RestKit來獲取JSON。改用AFNetworking。 – Wain

回答

0

AFNetworking做工作,

AFNetworking可以如圖here使用cocoapads安裝,

使用AFNetworking的示例請求:

NSURL *url = [[NSURL alloc] initWithString:@"https://www.ez-point.com/search"]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
[request setValue:@"xxxxxxxxxxx" forHTTPHeaderField:@"Authorization" ]; 
[request setHTTPMethod:@"GET"]; 

NSMutableDictionary *jsonDic = [[NSMutableDictionary alloc]init]; 
[jsonDic setValue:@"UJO526" forKey:@"search_text" ]; 
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrinted error:nil]; 
[request setHTTPBody:jsonData]; 


[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
AFJSONRequestOperation *operation = 
[AFJSONRequestOperation JSONRequestOperationWithRequest:request 
     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
      NSArray *searchResults = JSON; 
      if ([searchResults count] == 1){ 
       id result = [searchResults objectAtIndex:0]; 
       double latitude = [[result valueForKey:@"latitude"] doubleValue]; 
       double longitude = [[result valueForKey:@"longitude"] doubleValue]; 
       NSString *ezPoint = [result valueForKey:@"value"]; 
       NSString *tags = [result valueForKey:@"tags"]; 

       [self setAnnotation:latitude ForLongitude:longitude withEZPoint:ezPoint WithTags:tags]; 
      } 
     } 
     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 

     } 
]; 
[operation start];