【问题标题】:get null value for plist when get data from json and store in plist从json获取数据并存储在plist中时获取plist的空值
【发布时间】:2013-04-23 18:54:29
【问题描述】:

我有一个应用程序可以自行从 url 获取 json 并将值存储在 .plist 文件中。

这是我的代码,但无法正常工作,并且我的 plist 文件为空(请指导我)

这是我的 json:

[
    {
        "id": "1",
        "title": "Apple Releases iPad 3",
        "article": "This week Apple Inc. released the third gen iPad. It's available now for $599. It comes in 2 models: 8GB and 16GB.",
        "timestamp": "1343782587",
        "date_string": "Tue, Jul 31st, 2012 @ 7:56"
    },
    {
        "id": "2",
        "title": "Microsoft Releases Windows 8",
        "article": "Last week Microsoft released Windows 8 for consumer purchase. It's available now starting at $99 for the Home version. It comes with Microsoft Office '12 already installed.",
        "timestamp": "1343782649",
        "date_string": "Tue, Jul 31st, 2012 @ 7:57"
    },
    {
        "id": "3",
        "title": "Blizzard Announces Diablo IV",
        "article": "This week, long-time Diablo fans are euphoric with excitement at the announcement of Diablo IV, which will be released this fall, for $20! This original $20 purchase will also include all future expansion packs.",
        "timestamp": "1343782742",
        "date_string": "Tue, Jul 31st, 2012 @ 7:59"
    }
]

-(NSString*)docsDir {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory,
     NSUserDomainMask, YES)objectAtIndex:0]; 
}

- (void)viewDidLoad {
     [super viewDidLoad];
     [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
     NSURL *url = [NSURL URLWithString:@"http://192.168.1.109/mamal/json.php"];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
     [con start];  
}

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

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

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
     [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
      name = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
      for (int i =0; i<[name count]; i++) {
      NSIndexPath *indexPath = [self.table indexPathForSelectedRow];
      n = [[name objectAtIndex:(indexPath.row)+i]objectForKey:@"title"];
      if(!add){
            add = [NSMutableArray array];
       }
       [add addObject:n];
         }
       NSLog(@"add : %@",add);
       listPath = [[self docsDir]stringByAppendingFormat:@"janatan.plist"];    
       array = [NSMutableArray arrayWithContentsOfFile:listPath];
       [array addObjectsFromArray:add];
       [array writeToFile:listPath atomically:YES];
       NSLog(@"array : %@",array);         //array is null :(
       [table reloadData]; 
}

【问题讨论】:

  • listPath = [[self docsDir]stringByAppendingFormat:@"janatan.plist"]; --> 这是什么是空的
  • 不,我的朋友,我在 "add" 中有 3 个值,但 "array" 为 null 。 [数组 addObjectFromArray:add];看这段代码!!!

标签: ios objective-c json plist


【解决方案1】:

首先,您需要更详细地描述哪些不起作用。

其次,分解问题,使用调试器或 NSLog 检查所有中间产品。

在 didReceiveData 中记录你得到的数据

你正在进入 connectionDidFinishLoading

当你进入connectionDidFinishLoading时你有什么数据

name.count 是什么?

您从磁盘加载的阵列是否有效?您必须先加载一个空的 plist,然后才能开始附加它。 (这对我来说是唯一看起来像错误的事情)。

一般要点:您可以改进您的命名,添加什么??? 我也会添加更多代码文档,这看起来大部分是有效的代码,但逻辑相当混乱。

希望这会有所帮助 戈登

【讨论】:

    【解决方案2】:

    看不到你的 JSON,我猜你有一些不符合指定类型的对象或数据结构类型。

    • 顶级对象是 NSArray 或 NSDictionary。

    • 所有对象都是 NSString、NSNumber、NSArray、 NSDictionary 或 NSNull。

    • 所有字典键都是 NSString 的实例。

    • 数字不是 NaN 或无穷大。

    如果您的 JSON 不符合这些准则,当您尝试将其转换为 pList 时,它将失败。

    首先进行错误检查尝试

     isValidJSONObject:
    

    在你的“数据”对象上,看看你得到了什么。

    接下来你做了太多工作,NSJSONSerialization 应该返回一个你可以直接写入 plist 的基础对象。

    例如,如果你返回的是一个 NSDictionary 对象,你可以使用

    writeToFile:atomically: 
    

    我认为你尝试过,但你在做之前对数据进行了相当多的更改。

    【讨论】:

    • 看来您正在返回一系列字典。
    • 我的朋友我可以在 plist 文件中编写 nsarray 并帮助此代码:if (![[NSFileManager defaultManager]fileExistsAtPath:listPath]) { [[NSFileManager defaultManager]copyItemAtPath:[[NSBundle mainBundle]pathForResource: @"list" ofType:@"plist"] toPath:listPath error:nil]; NSLog(@"创建这个"); } 但这个地方我不知道为什么???
    • 我没有看到您在问题评论中列出的代码
    • 另外,您的代码中声明的“add”在哪里......它不在您的示例中。如果您从未声明它,这将导致问题。
    【解决方案3】:

    我认为错误是由于创建 listPath 的方式造成的,您应该使用 stringByAppendingPathComponent: 而不是 stringByAppendingFormat: 。

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {
    
       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        name = [NSJSONSerialization JSONObjectWithData:data 
                                               options:NSJSONReadingAllowFragments 
                                                 error:nil];
        if(!add){
             NSMutableArray *add = [NSMutableArray array];
        }
    
        for (int i =0; i<[name count]; i++) {
             NSIndexPath *indexPath = [self.table indexPathForSelectedRow];
             n = name[indexPath.row+i][@"title"];
             [add addObject:n];
        }
    
        NSLog(@"add : %@",add);
    
        listPath = [[self docsDir]stringByAppendingPathComponent:@"janatan.plist"];
    
        NSMutableArray *savedArray = [NSMutableArray arrayWithContentsOfFile:listPath];
        if (!savedArray) {
            savedArray = [NSMutableArray array];
        }
        [savedArray addObjectsFromArray:add];
    
        [savedArray writeToFile:listPath atomically:YES];
    
        NSLog(@"array : %@",savedArray); 
        [self.table reloadData]; 
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-06
      • 1970-01-01
      • 2011-11-06
      • 1970-01-01
      • 2013-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多