【问题标题】:Show JSON in tableview IOS Xcode [closed]在tableview IOS Xcode中显示JSON [关闭]
【发布时间】:2014-09-03 11:31:45
【问题描述】:

亲爱的程序员。 我必须连接到一个 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

请不要链接到教程,因为我都看过了 提前致谢

【问题讨论】:

标签: ios objective-c json parsing


【解决方案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);
    }];

}

【讨论】:

    【解决方案2】:

    使用AFNetworking时,响应对象为NSDictionary Object,无需转换为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); }];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2011-01-23
      • 1970-01-01
      相关资源
      最近更新 更多