1

我正在尝试获取一些 json 数据并将其显示为 UILabel 中的文本,但我不断收到应用程序崩溃并出现以下错误 -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x1f8cfff0?

这是我的代码和 json 响应。我在我的日志中看到我从调用中获取了名称,但应用程序轰炸了那个错误。我有 2 个 UILabel 块,其中一个显示 json 响应的文本格式,另一个显示文本中的实际 json 响应。

我正在尝试提取此人的姓名,当 json 返回时,我可以在日志中看到 Bilbo Baggins。

这是我的 json 输出:

{"ProfileID":34,"ProfilePictureID":20,"Name":"Bilbo Baggins","Clients":[{"ClientID":91,"Name":"Fnurky"},{"ClientID":92,"Name":"A different client"},{"ClientID":95,"Name":"Second Community"},{"ClientID":96,"Name":"Britehouse"}]}

和我的代码尝试将其显示为 uilabel 作为文本。

#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1
#define kLatestKivaLoansURL [NSURL URLWithString: @"http://www.ddproam.co.za/Central/Profile/JSONGetProfileForUser"] //2

#import "JsonViewController.h"

@interface NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress;
-(NSData*)toJSON;
@end

@implementation NSDictionary(JSONCategories)
+(NSDictionary*)dictionaryWithContentsOfJSONURLString:(NSString*)urlAddress
{
NSData* data = [NSData dataWithContentsOfURL: [NSURL URLWithString: urlAddress] ];
__autoreleasing NSError* error = nil;
id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error != nil) return nil;
return result;
}

-(NSData*)toJSON
{
NSError* error = nil;
id result = [NSJSONSerialization dataWithJSONObject:self options:kNilOptions error:&error];
if (error != nil) return nil;
return result;    
}
@end

@implementation JsonViewController

- (void)viewDidLoad
{
[super viewDidLoad];

dispatch_async(kBgQueue, ^{
    NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}

 - (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                     options:kNilOptions 
                                                       error:&error];
NSArray* defineJsonData = [json objectForKey:@"Name"]; //2

NSLog(@"Name: %@", defineJsonData); //3

// 1) Get the latest loan
NSDictionary* loan = [defineJsonData objectAtIndex:0];


// 3) Set the label appropriately
humanReadble.text = [NSString stringWithFormat:@"Hello: %@",
                     [(NSDictionary*)[loan objectForKey:@"Name"] objectForKey:@"Name"]];

//build an info object and convert to json
NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:
                      [loan objectForKey:@"Name"],
                      nil];

//convert object to data
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:info 
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

//print out the data contents
jsonSummary.text = [[NSString alloc] initWithData:jsonData
                                         encoding:NSUTF8StringEncoding];

}

@end
4

1 回答 1

2

一个组合——对不起——糟糕的变量名和一个复杂的结构。

第一:在这里你得到完整的 JSON 作为字典:

NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
                                                     options:kNilOptions 
                                                       error:&error];

根据您的 Q,它具有以下结构:

{
   "ProfileID":34,
   "ProfilePictureID":20,
   "Name":"Bilbo Baggins",
   "Clients":
   [
      {
         "ClientID":91,
         "Name":"Fnurky"
      },
      {  
         "ClientID":92,
         "Name":"A different client"
      },
      {
         "ClientID":95,
         "Name":"Second Community"
      },
      {
         "ClientID":96,
         "Name":"Britehouse"
      }
   ]
}

第二:使用下一个语句,您只需得到明显类似于人的名称:

NSArray* defineJsonData = [json objectForKey:@"Name"]; //2

有根:

你得到的——看看你的 JSON——是:

   "Name":"Bilbo Baggins",
  • 你得到了 key 的对象Name。保存对结果的引用的 var 应该被称为表达 this。让我们改变一下:

    NSArray* name = [json objectForKey:@"Name"]; //2

  • 接下来 - 查看您的 JSON - 该键后面的对象是 的实例NSString,而不是NSArray. 让我们修复它:

    NSString* name = [json objectForKey:@"Name"]; //2

第三:

这样做会使编译器抛出错误。这是因为这个声明:

NSDictionary* loan = [defineJsonData objectAtIndex:0];

更改为新的 var 名称:

NSDictionary* loan = [name objectAtIndex:0];

编译器是对的:你没有数组,所以你不能发送objectAtIndex:.

于 2013-05-16T08:57:36.023 回答