2012-08-16 14 views
0

我有一個NET Web服務,在這種格式返回一個JSON數據:無法從Json的保存NSDictionary的反序列化從.NET Web服務返回的plist

 NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataPlist options:NSJSONReadingMutableContainers error:&errorJson]; 

[ 
{ 
    "MEMBERID": "xxx", 
    "PREFIXINFORMAL": "Mr.", 
    "FIRSTNAME": "xxx", 
    "MIDDLENAME": "xxxx", 
    "LASTNAME": "xxxx", 
    "SUFFIX": xxxx 
} 

]

我的代碼保存此的NSDictionary plist:

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString *documentsDirectory = [paths objectAtIndex:0]; 
     NSString *path = [documentsDirectory stringByAppendingPathComponent:@"boardMemberInfo.plist"]; 
     NSDictionary *test = [NSDictionary dictionaryWithObject:@"test" forKey:@"test"]; 
     for(NSDictionary *item in json) { 
      BOOL success = [item writeToFile:path atomically:YES]; 
      if(success) { 
       NSLog(@"Success Saving New plist"); 
      } else { 
       NSLog(@"Failure Saving New plist"); 
      } 
     } 

既沒有[item writeToFile ...]也沒有[json writeToFile ...]工作。但是,簡單的[test writeToFile ...]工作。我錯過了什麼?

感謝,

回答

0
,你可以做

最好的辦法是分解的保存過程,並使用NSError明白是什麼問題:

for(NSDictionary *item in json) { 
     NSError *error; 
     NSData *serializedData = [NSPropertyListSerialization dataWithPropertyList:item format:… options:… error:&error]; 
     if (!serializedData) { NSLog(@"Can't serialize data: %@", error); continue; } 
     if (![serializedData writeToFile:path options:… error:&error]) { 
      NSLog(@"Can't write to %@: %@", path, error); 
     } else { 
      NSLog(@"Success Saving New plist"); 
     } 
    } 
+0

有從查詢返回所以將writeToFile失敗的空值。我使用decode()sql函數不返回任何null。它現在有效。 – imObjCSwifting 2012-08-16 20:58:36

相關問題