2017-06-18 7 views
0

JSON文字列を解析しようとしていますが、SIGABRTエラーが発生します。私が使用していますJSONを解析しようとするとSIGABRT

コード:

NSString *test = @'{"notifications":[{"id":"fae9a890-2791-46e2-ad9c-5a72f602a2e8","created":"2017-06-17T21:57:28+00:00","thread_id":3964,"reply_id":null,"thread":{"id":3964,"subject":"[CakePHP] Pagination"},"users_from":{"username":"Royal"},"content":"has posted a reply in"},{"id":"00732627-f23e-423e-b885-add968575972","created":"2017-06-17T20:08:05+00:00","thread_id":3964,"reply_id":79478,"thread":{"id":3964,"subject":"[CakePHP] Pagination"},"users_from":{"username":"Royal"},"content":"has quoted you in"}]}'; 

NSError *error; 
     NSMutableDictionary *allCourses = [NSJSONSerialization JSONObjectWithData:test 
                      options:kNilOptions 
                      error:&error]; 
     if(error) 
     { 
      NSLog(@"%@", [error localizedDescription]); 
     } 
     else { 
      NSArray *monday = allCourses[@"notifications"]; 
      for (NSDictionary *theCourse in monday) 
      { 
       NSLog(@"----"); 
       NSLog(@"Title: %@", theCourse[@"subject"]); 
       NSLog(@"Id: %@", theCourse[@"id"]); 
       NSLog(@"----"); 
      } 
     } 

感謝。

答えて

0

間違いはあなたのNSStringの宣言である:

NSString *test = @'{"notifications"}'; 

これは間違っています。

NSStringは常に@ "bla bla bla"という形式を使用する必要があります。

あなたはこの

NSString *test = @"{\"notifications\"}"; 
+0

エラーまだ何とか... – Anoniem

+0

作品、感謝のようなあなたのテスト文字列に二重引用符のためのエスケープを組み込む必要があります。これを自動的に行う方法はありますか? – Anoniem

+0

私の場合は、単にプロジェクトにテキストファイルを追加してそこにjsonと入力し、コードでtexfileを読み込んで辞書にロードします。あなたはエスケープに対処する必要はありませんその方法 – GeneCode

関連する問題