1

我在将 JSON 数据存储到单个对象的数组中时遇到问题。似乎问题出在处理 JSON 请求的 dispatch_asynch 的执行中。当我在方法之前创建一个断点而不是单步执行应用程序时,它似乎只是通过发送到 dispatch_async 的块。

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
    NSString *json = [NSString stringWithContentsOfURL:url
                                              encoding:NSASCIIStringEncoding
                                                 error:&error];
    NSLog(@"\nJSON: %@ \n Error: %@", json, error);

    if(!error) {
        NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                 options:kNilOptions
                                                                   error:&error];
        NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];

        for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
            NSString *tempString = [[NSString alloc] initWithString:name];
            Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
            [self addCompany:company];

我非常感谢大家在这个问题上的帮助和支持。

4

2 回答 2

1
 if(!error) {
...
      }
else
{
    NSLog(@"%@",[error localizedDescription]);
}

记录错误并找出发生了什么

我尝试过的这段代码运行良好

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    NSError *error = nil;
    NSURL *url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
    NSString *json = [NSString stringWithContentsOfURL:url
                                              encoding:NSASCIIStringEncoding
                                                 error:&error];
    NSLog(@"\nJSON: %@ \n Error: %@", json, error);

    if(!error) {
        NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
                                                                 options:kNilOptions
                                                                   error:&error];
        NSArray *tempArray = [NSArray arrayWithObjects:@"Call Support Desk", @"Call Genius Bar", nil];

        for (NSString *name in [jsonDict valueForKeyPath:@"name"]) {
            NSString *tempString = [[NSString alloc] initWithString:name];
            NSLog(@"%@",tempString);
        }
    }
});

回复

2013-03-05 22:02:44.312 newTrial[4711:12303] 
JSON: [{"created_at":"2013-03-04T00:09:06Z","id":1,"name":"Apple","updated_at":"2013-03-04T00:09:06Z","actions":[{"created_at":"2013-03-04T00:09:07Z","id":1,"name":"Call Support Desk","updated_at":"2013-03-04T00:09:07Z"},{"created_at":"2013-03-04T00:09:07Z","id":2,"name":"Call Genius Bar","updated_at":"2013-03-04T00:09:07Z"}]},{"created_at":"2013-03-04T02:01:49Z","id":2,"name":"Comcast","updated_at":"2013-03-04T02:01:49Z","actions":[{"created_at":"2013-03-04T02:01:49Z","id":3,"name":"Account Services","updated_at":"2013-03-04T02:01:49Z"}]}] 
 Error: (null)
2013-03-05 22:02:51.766 newTrial[4711:12303] Apple

所以我对问题的假设是您将值存储到数组的位置。检查它是否正确初始化问题可能在这里

Company *company = [[Company alloc] initWithName:tempString available_actions:tempArray];
[self addCompany:company];
于 2013-03-05T16:23:51.180 回答
0

它对我来说就像一个魅力:

dispatch_queue_t _queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(_queue, ^{
    NSError *_error = nil;
    NSURL *_url = [NSURL URLWithString:@"http://sleepy-dusk-3603.herokuapp.com/companies.json"];
    NSData *_json = [NSData dataWithContentsOfURL:_url];
    if (_json) {
         id _response = [NSJSONSerialization JSONObjectWithData:_json options:kNilOptions error:&_error];
         if (!_error) {
            // do whatever you want to do here...
         } else {
            NSLog(@"%@", _response);
         }
    }
});

回应是:

(
        {
        actions =         (
                        {
                "created_at" = "2013-03-04T00:09:07Z";
                id = 1;
                name = "Call Support Desk";
                "updated_at" = "2013-03-04T00:09:07Z";
            },
                        {
                "created_at" = "2013-03-04T00:09:07Z";
                id = 2;
                name = "Call Genius Bar";
                "updated_at" = "2013-03-04T00:09:07Z";
            }
        );
        "created_at" = "2013-03-04T00:09:06Z";
        id = 1;
        name = Apple;
        "updated_at" = "2013-03-04T00:09:06Z";
    },
        {
        actions =         (
                        {
                "created_at" = "2013-03-04T02:01:49Z";
                id = 3;
                name = "Account Services";
                "updated_at" = "2013-03-04T02:01:49Z";
            }
        );
        "created_at" = "2013-03-04T02:01:49Z";
        id = 2;
        name = Comcast;
        "updated_at" = "2013-03-04T02:01:49Z";
    }
)
于 2013-03-05T16:44:27.973 回答