-1

我有一个应用程序可以自行从 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]; 
}
4

3 回答 3

0

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

  • 顶级对象是 NSArray 或 NSDictionary。

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

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

  • 数字不是 NaN 或无穷大。

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

首先进行错误检查尝试

 isValidJSONObject:

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

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

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

writeToFile:atomically: 

我认为您可以尝试,但在您这样做之前您会稍微更改数据。

于 2013-04-23T19:00:55.217 回答
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]; 

}
于 2013-04-23T20:12:36.470 回答
0

首先,您需要对不起作用的内容进行更多描述。

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

记录你在 didReceiveData 中得到的数据

你进入connectionDidFinishLoading

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

什么是name.count?

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

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

希望这可以帮助戈登

于 2013-04-23T19:07:34.353 回答