从json获取数据并在plist中存储时获取plist的null值

时间:2013-04-23 18:54:29

标签: ios objective-c json plist

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

这是我的代码,但没有工作,我的plist文件为null(请指导我)

这是我的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]; 
}

3 个答案:

答案 0 :(得分:0)

如果没有能够看到你的JSON,我的猜测是你有一些不符合指定类型的对象或数据结构。

  • 顶级对象是NSArray或NSDictionary。

  • 所有对象都是NSString,NSNumber,NSArray的实例, NSDictionary,或NSNull。

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

  • 数字不是NaN或无穷大。

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

首先进行错误检查,然后尝试

 isValidJSONObject:

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

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

例如,如果您获得的是NSDictionary对象,则可以使用

writeToFile:atomically: 

我认为你尝试过,但是在你做之前你会改变数据。

答案 1 :(得分:0)

首先,你需要在不起作用的情况下更具描述性。

其次,打破问题,检查调试器或NSLog,所有中间产品。

记录您在didReceiveData

中获得的数据

你是否进入了connectionDidFinishLoading

当您进入connectionDidFinishLoading

时,您对数据有什么了解?

name.count是什么?

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

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

希望这会有所帮助 戈登

答案 2 :(得分:0)

我认为错误是由于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]; 

}
相关问题