2017-06-18 84 views
0

我想解析一个JSON字符串,但它与SIGABRT错误下降。我使用SIGABRT当试图解析JSON

代码:

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