2014-09-03 43 views
-5

親愛的程序員。 我必須連接到一個API,並使用我回來的JSON將它存儲在一個表中。 使用AFnetworking,Xcode和ios。 到目前爲止,它一直在給我一整天的時間,使我瘋狂。我已經嘗試了幾個教程和項目,但我無法讓它工作。在tableview中顯示JSON IOS Xcode

第一個問題是我似乎無法找到我的,我回來了JSON鍵..

當然,我不得不解析JSON,它並沒有真正對我也

工作能你請幫我把我的json放到桌面視圖中?

我使用的API是:https://api.9292.nl/0.1/locations?lang=nl-NL&q=amsterd

我在應用程序收到爲輸出 {

"locations": [ 
    { 
     "id": "station-amsterdam-centraal", 
     "type": "station", 
     "stationId": "asd", 
     "stationType": "Station", 
     "name": "Amsterdam Centraal", 
     "place": { 
     "name": "Amsterdam", 
     "regionCode": "NH", 
     "regionName": "Noord-Holland", 
     "showRegion": false, 
     "countryCode": "NL", 
     "countryName": "Nederland", 
     "showCountry": false 
     }, 
     "latLong": { 
     "lat": 52.378706, 
     "long": 4.900489 
     }, 
     "urls": { 
     "nl-NL": "/station-amsterdam-centraal", 
     "en-GB": "/en/station-amsterdam-centraal" 
     } 

The code I have right now is: 
// 
// ViewController.m 
// 9292apiconnection 
// 
// Created by TheDutchBeast on 02-09-14. 
// Copyright (c) 2014 Lambregts. All rights reserved. 
// 

#import "ViewController.h" 
#import <AFNetworking/AFNetworking.h> 

@interface ViewController() 

@property (strong, nonatomic) NSDictionary *posts; 
@property (strong, nonatomic) NSMutableArray *post; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager GET:@"http://api.9292.nl/0.1/locations?lang=nl-NL&q=amsterd" parameters:nil success:^(
     AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"JSON: %@", responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

請幫我確定從關鍵的JSON json代碼以及如何將這些值存入Xcode的表中,ios 。只有ID和名稱需要在表格中

請以教程沒有鏈接中顯示,因爲我已經看到他們事先都 感謝

+2

你試過了什麼? http://meta.stackexchange.com/questions/122986/is-it-ok-to-leave-what-have-you-tried-comments – 2014-09-03 11:34:13

回答

1

試試這個響應JSON轉換成NSDictionary

NSDictionary *receivedDataDic = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:kNilOptions error:&error]; 

現在通過使用鍵名訪問您想要的值,如

NSString * id = [receivedDataDic valueForKey:@"id"]; 
NSString * name = [receivedDataDic valueForKey:@"name"]; 

使用這些變量,您想要

在你的代碼進行這些更改

@interface ViewController() 
{ 
    NSString * id ,* name; 
} 

@property (strong, nonatomic) NSDictionary *posts; 
@property (strong, nonatomic) NSMutableArray *post; 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    [manager GET:@"http://api.9292.nl/0.1/locations?lang=nl-NL&q=amsterd" parameters:nil success:^(
     AFHTTPRequestOperation *operation, id responseObject) { 

     NSDictionary *receivedDataDic = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:kNilOptions error:&error]; 

     id = [receivedDataDic valueForKey:@"id"]; 
     name = [receivedDataDic valueForKey:@"name"]; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }]; 

} 
0

當使用AFNetworking,響應對象是一個NSDictionary對象,沒有必要轉換爲一個NSDictionary

,你應該能夠解析位置這樣的數組。

AFHTTPRequestOperationManager *manager = 
    [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer = [AFJSONResponseSerializer serializer]; 
[manager GET:@"http://api.9292.nl/0.1/locations?lang=nl-NL&q=amsterd" 
    parameters:nil 
    success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSArray *locations = [responseObject objectForKey:@"locations"]; 
     for (id obj in locations) { 
     NSLog(@"id: %@, name: %@", [obj objectForKey:@"id"], 
       [obj objectForKey:@"name"]); 
     } 
    } 
    failure:^(AFHTTPRequestOperation *operation, 
      NSError *error) { NSLog(@"Error: %@", error); }];