【问题标题】:Unable to read data after NSJSONSerializationNSJSONSerialization 后无法读取数据
【发布时间】:2013-12-13 10:41:08
【问题描述】:

我正在从服务器获取一个 json 数组。 jsonOutput 对象正确显示 2 个对象。但我无法显示或提取数据。有人能帮帮我吗。我试过 以下方式:

 for (id key in jsonOutput) {
          NSLog(@"key: %@, value: %@", key, [jsonOutput objectForKey:key]);
        }

声明: NSDictionary *jsonOutput;

实际方法:

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
        data=[[NSMutableData alloc] init];
    }

    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
    {
        [data appendData:theData];


       // if ([connection isEquals:connect1]){
            // this is request urlConnectionRecsender
       // }
    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {

    jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];


    for (id key in jsonOutput) {
          NSLog(@"key: %@, value: %@", key, [jsonOutput objectForKey:key]);
        }

    }

【问题讨论】:

  • 您可以传入一个错误变量并检查该对象。如果没有错误写入变量,请检查是否有数组或字典。

标签: ios objective-c nsdictionary nsjsonserialization


【解决方案1】:

试试这个

NSError *error;

jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

if(error) 
    NSLog(@"%@",error.description);

如果你不知道什么是响应类型,那么最好先检查响应类型

id responseObj = [NSJSONSerialization
                      JSONObjectWithData:data
                      options:kNilOptions
                      error:&error];
    if ([responseObj isKindOfClass:[NSArray class]]) {
       //Your response is a array
    }
    else if([responseObj isKindOfClass:[NSDictionary class]]) {
        //Your response is a dictionary
    }

您的数组包含 NSDictionary

{ cropName = corn; cropOrderId = 1; cropPrice = 100; "farmer_id" = 1; orderStatus = pending; quantity = 5; } 

使用此代码获取价值

for(NSDictionary*dict in jsonObject) {

    NSArray *allKeysarr = [dic allKeys];

    for(NSString *key in allKeysarr) {

         NSLog(@"%@",[dic valueForKey:key]);
     }

}

【讨论】:

  • 不,它不能工作相同的错误 -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7518aa0
  • 那么你的响应是一个数组。使 jsonOutput 到 NSArray
  • 我使用了上面的代码和它的一个数组。但即使将类型更改为数组后,我仍然会收到错误消息。使用 for(id key in jsonOutput){ NSLog(key); }
  • 你如何访问数组中的值?
  • 现在使用 for(id key in jsonOutput){ NSLog(@"%@",key); } .... as {cropName = 玉米;作物订单 ID = 1;作物价格 = 100; “农民ID”= 1; orderStatus = 待处理;数量 = 5;我如何获取它们中的每一个?
【解决方案2】:

首先使用错误处理:

NSError *error;
jsonOutput= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if (error) NSLog(@"error: %@",error.description);

然后通过以下方式访问值:

NSString *value1 = [jsonOutput objectForKey:@"YOUR_KEY1"];

【讨论】:

    【解决方案3】:

    NSData *xmlData = [NSData dataWithContentsOfFile:xmlPath];

    NSError *jsonError = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:xmlData options:kNilOptions error:&jsonError];
    
    if ([jsonObject isKindOfClass:[NSArray class]]) {
        NSLog(@"its an array!");
        NSArray *jsonArray = (NSArray *)jsonObject;
        NSLog(@"jsonArray - %@",jsonArray);
    }
    else {
        NSLog(@"its probably a dictionary");
        NSDictionary *jsonDictionary = (NSDictionary *)jsonObject;
        NSLog(@"jsonDictionary - %@",jsonDictionary);
    }
    

    试试这个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-22
      相关资源
      最近更新 更多