-1

URLからjsonを取得し、.plistファイルに値を保存するアプリケーションが1つあります。

これは私のコードですが、機能していません.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]; 
}
4

3 に答える 3

0

まず、何が機能していないかをもっと詳しく説明する必要があります。

次に、問題を分解し、デバッガーまたは NSLog を使用して、すべての中間生成物を確認します。

取得したデータを didReceiveData に記録します

connectionDidFinishLoadingに入っていますか

connectionDidFinishLoadingに入ったとき、データには何がありますか

名前.カウントとは何ですか?

ディスクからロードしているアレイは有効ですか? 追加を開始する前に、ロードする空の plist が必要です。(これは私にとってエラーのように見える唯一のものです)。

一般的なポイント: ネーミングを改善できます。追加とは何ですか? コードのドキュメントも追加したいと思います。これはほとんど有効なコードに見えますが、ロジックはかなり混乱しています。

これがゴードンに役立つことを願っています

于 2013-04-23T19:07:34.353 に答える
0

JSON を見ることができなければ、指定された型に準拠していない Type のオブジェクトまたはデータ構造があると思います。

  • 最上位オブジェクトは NSArray または NSDictionary です。

  • すべてのオブジェクトは、NSString、NSNumber、NSArray、NSDictionary、または NSNull のインスタンスです。

  • すべての辞書キーは NSString のインスタンスです。

  • 数値は NaN または無限大ではありません。

JSON がこれらのガイドラインに適合しない場合、pList に変換しようとすると失敗します。

最初にエラーチェックを試してください

 isValidJSONObject:

「データ」オブジェクトで、何が得られるかを確認してください。

次に、NSJSONSerialization は、plist に直接書き込むことができる基本オブジェクトを返す必要があります。

たとえば、返されるものが NSDictionary オブジェクトである場合は、使用できます

writeToFile:atomically: 

試してみると思いますが、そうする前にデータをかなり変更します。

于 2013-04-23T19:00:55.217 に答える