2013-04-23 54 views
-1

我有一个应用程序在自我中从url中获取json并将值存储在.plist文件中。当从json获取数据并在plist中存储时,为plist获得null值

这是我的代码,但没有工作,在空我的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]; 
} 
+0

listPath = [[self docsDir] stringByAppendingFormat:@“janatan.plist”]; - >是这是什么NULL – 2013-04-23 19:23:05

+0

没有我的朋友我有3个值在“添加”但“阵列”为空。 [数组addObjectFromArray:add];看这个代码! – janatan 2013-04-23 19:29:48

回答

0

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

  • 顶级对象是NSArray或NSDictionary。

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

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

  • 数字不是NaN或无穷大。

如果你的JSON不符合这些准则,当你试图将其转换为一个plist中它将会失败。

首先进行错误校验尝试

isValidJSONObject: 
你的“数据”对象

,看看你会得到什么。

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

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

writeToFile:atomically: 

哪我想尝试,但你改变了数据颇有几分你做之前。

+0

我张贴我的json请引导我! – janatan 2013-04-23 19:04:17

+0

看来你正在找回一组字典。 – 2013-04-23 19:19:09

+0

我的朋友,我可以在plist文件中使用下面的代码写下nsarray:if(![[NSFileManager defaultManager] fileExistsAtPath:listPath]){[NSFileManager defaultManager] copyItemAtPath:[[NSBundle mainBundle] pathForResource:@“list”ofType: @“plist”] toPath:listPath error:nil]; NSLog(@“create this”); }但这个地方我不能为什么? – janatan 2013-04-23 19:25:52

0

首先,你需要更多描述性的东西在不起作用。第二,打破这个问题,或者用调试器或NSLog检查所有的中间产品。

登录您在didReceiveData

得到的数据是你进入connectionDidFinishLoading

你有数据什么当你进入connectionDidFinishLoading

什么是name.count?

从磁盘加载的数组是否有效?您必须先加载一个空plist才能开始附加到它。 (这是唯一看起来像我的错误)。

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

希望这有助于 戈登

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]; 

}