2013-01-04 115 views
0

返回空值「 」我有一個JSON響應我從restkit得到一個奇怪的錯誤。 RestKit從JSON響應

我做一個POST請求到服務器,並將其與"someVal" = "<null>",而不是"someVal" = null響應。這使得Xcode中對付它在一個非常奇怪的方式,我似乎無法保存,因爲它... NSUserDefaults的,我不能刪除它就像我一個正常的空值...那會被檢測爲?我想也許是一個字符串?但是,不能......因爲某種原因,它不能保存爲一個字符串。

+1

您確定該值存儲爲字符串「」「」嗎?因爲印刷字典的描述與'[NSNull空]'值也產生輸出'鍵=「」'。 –

回答

1

我不認爲這個問題是有限的,只是RestKit。更多來自爲您提供JSON的服務器。我在使用AFNetworking時遇到了同樣的問題。

我發現這個字典類別,你可以用它來刪除空值並用空字符串替換。你可以使用它,或者只是將邏輯翻錄出來,以排序你的空值。

@implementation NSDictionary (JRAdditions) 

- (NSDictionary *) dictionaryByReplacingNullsWithStrings { 

    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:self]; 
    const id nul = [NSNull null]; 
    const NSString *blank = @""; 

    for(NSString *key in self) { 
     const id object = [self objectForKey:key]; 
     if(object == nul) { 
      //pointer comparison is way faster than -isKindOfClass: 
      //since [NSNull null] is a singleton, they'll all point to the same 
      //location in memory. 
      [replaced setObject:blank forKey:key]; 
     } 
    } 
    return [NSDictionary dictionaryWithDictionary:replaced]; 
} 

+ (NSDictionary *)dictionaryByReplacingNullsWithStrings:(NSDictionary *)dict { 
    NSMutableDictionary *replaced = [NSMutableDictionary dictionaryWithDictionary:dict]; 
    const id nul = [NSNull null]; 
    const NSString *blank = @""; 

    for(NSString *key in dict) { 
     const id object = [dict objectForKey:key]; 
     if(object == nul) { 
      //pointer comparison is way faster than -isKindOfClass: 
      //since [NSNull null] is a singleton, they'll all point to the same 
      //location in memory. 
      [replaced setObject:blank forKey:key]; 
     } 
    } 
    return [NSDictionary dictionaryWithDictionary:replaced]; 
} 

@end 

// you can use this category on your dictionaries. 
NSDictionary *jsonDict = JSON; 
jsondict = [jsonDict dictionaryByReplacingNullsWithStrings]; 
+0

對於顯示爲「」,而不是反正空我值這個行不通的。我使用這種方法來刪除標準的空值,但是當我NSLog數據饋送時,它並不刪除顯示爲「」的值。 – jimbob

+0

它可能是一個具有該值的字符串。也許修改該方法以替換空字符串。 –

+0

它不認爲「」是一個字符串類型,雖然這很奇怪。 – jimbob