2017-06-18 119 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