2014-09-03 41 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); }];