1

请帮我调试这段代码

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
    NSError *error = nil;
    NSURL *urls = [NSURL URLWithString:[NSString stringWithFormat:@"http://cnapi.iconnectgroup.com/api/UserProfile?id=1"]];
    NSString *json = [NSString stringWithContentsOfURL:urls encoding:NSASCIIStringEncoding error:&error];
        NSLog(@"JSon data = %@ and Error = %@", json, error);
    if(!error)
    {
        NSData *jsonData = [json dataUsingEncoding:NSASCIIStringEncoding];
        NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
        NSLog(@"JSON data is :: %@", myJsonArray);

        for(NSDictionary *jsonDictionary in myJsonArray)
        {
            //NSString *uids = jsonDictionary[@"UID"];
            NSString *address1 = jsonDictionary[@"Address1"];
            NSString *address2 = jsonDictionary[@"Address2"];
            NSString *city = jsonDictionary[@"City"];
            NSString *emailId = jsonDictionary[@"EmailID"];
            NSString *fname = jsonDictionary[@"FName"];
            NSString *fax = jsonDictionary[@"Fax"];
            NSString *lname = jsonDictionary[@"LName"];
            NSString *password = jsonDictionary[@"Password"];
            NSString *phone = jsonDictionary[@"Phone"];
            NSString *state = jsonDictionary[@"State"];
            NSString *uids = [jsonDictionary objectForKey:@"UID"];
            NSString *zip = jsonDictionary[@"Zip"];
            NSString *company = jsonDictionary[@"company"];
            NSString *department = jsonDictionary[@"department"];

            NSLog(@"Uid is = %@", uids);
            NSLog(@"First Name = %@", fname );
            NSLog(@"Last Name = %@", lname);
            NSLog(@"Company = %@", company);
            NSLog(@"Email Id = %@", emailId);
            NSLog(@"Password = %@", password);
            NSLog(@"Department = %@", department);
            NSLog(@"Address 1 = %@", address1);
            NSLog(@"Address 2 = %@", address2);
            NSLog(@"City = %@", city);
            NSLog(@"State = %@", state);
            NSLog(@"Zip = %@", zip);
            NSLog(@"Phone = %@", phone);
            NSLog(@"Fax = %@", fax);

        }
    }

  });
    [activity stopAnimating];
    self.activity.hidden = YES;
}

图片会告诉你错误在哪里。单击 stepover 进行调试后出现此错误。我也试过

NSString *address1 = [jsonDictionary objectForKey:@"Address1"];

在此处输入图像描述

4

3 回答 3

2

从 url 的输出中可以看出,它不是数组而是字典。您正在尝试在此处转换为数组。

NSArray *myJsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

而是使用这个

 NSDictionary *myJsonDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];

然后删除不需要它的 for 循环。并将您的变量 jsonDictionary 替换为 myJsonDictionary 以便检索值。

// for(NSDictionary *jsonDictionary in myJsonArray)

现在运行它会好的。为我工作得很好

如果输出是字典数组,它会看起来像这样,周围有方括号。

For Ex: [{"id": "1", "name":"Aaa"}, {"id": "2", "name":"Bbb"}]

如果您不确定来自 url 的响应的性质,您可以检查类型。例如:

id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData   options:0 error:nil];

if ([jsonObject isKindOfClass:[NSArray class]]) {
    NSLog(@"its an array!");

    NSArray *myJsonArray = (NSArray *)jsonObject;
   // Handle Array of Dictionary

for(NSDictionary *jsonDictionary in myJsonArray)
    {
        NSString *address1 = jsonDictionary[@"Address1"];
       //and so on
    }
}
else {
NSLog(@"It's Dictionary");
NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
NSLog(@"jsonDictionary - %@",jsonDictionary);
//Handle NSDictionary
}
于 2013-10-10T18:34:27.757 回答
1

jsonDictonary 是一个 NSString,不像你所期望的 NSDictonary。

仔细检查您的 JSON,也许在调用该函数之前检查它是否是 NSDictionary。

于 2013-10-09T12:24:11.403 回答
0

您应该使用半标准框架之一将 JSON 解析为核心数据。有一些关于这些的问题。

在这种情况下,您的 JSON 只有一个不是数组的对象。一般来说,如果服务器发送了意外的东西,你的应用程序不应该中止,所以最好使用一个会大声抱怨 JSON 格式错误的解析器,而不是从头开始使用它。

于 2013-10-10T21:31:45.657 回答