亲爱的程序员。 我必须连接到api并使用我回来的json将它存储在一个表中。 使用AFnetworking,Xcode和ios。 到目前为止,它已经让我整整一天,让我疯了。我已经尝试了几个教程和项目,但我无法让它工作。
第一个问题是我似乎无法找到我在json中找到的钥匙......
当然我必须解析json,这对我来说不起作用
你能帮我把我的json带到桌子视图吗?
我使用的api是:https://api.9292.nl/0.1/locations?lang=nl-NL&q=amsterd
我在应用程序中收到的json作为输出 {
"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代码中的密钥以及如何将这些值放入Xcode,ios中的表中 。只需要在表格中显示ID和NAME
请不要链接到教程,因为我已经看过它们了 提前致谢
答案 0 :(得分: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);
}];
}
答案 1 :(得分: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); }];