我從Yummly API獲取數據,我想將它用作序列化的JSON數據。但是,它目前是一個字符串,我不知道如何正確地將其轉換爲數據。該代碼如下:將NSString轉換爲NSData以便在XCODE中使用
NSString *searchParameters = @"basil"; //should be from text box
//NSError *error1 = nil;
NSString *searchURLName = [@"http://api.yummly.com/v1/api/recipes?_app_id=myAPIId&_app_key=myAPIkey&" stringByAppendingString:searchParameters];
NSURL *searchURL = [NSURL URLWithString:searchURLName];
NSString *searchResults = [NSString stringWithContentsOfURL:searchURL encoding:NSUTF8StringEncoding error:nil];
// Here, the search results are formatted just like a normal JSON file,
// For example:
/* [
"totalMatchCount":777306,
"facetCounts":{}
]
*/
// however it is a string, so I tried to convert it to data
NSData *URLData = [searchResults dataUsingEncoding:NSUTF8StringEncoding];
URLData = [URLData subdataWithRange:NSMakeRange(0, [URLData length] - 1)];
_searchArray = [NSJSONSerialization JSONObjectWithData:URLData options:NSJSONReadingMutableContainers error:nil];
某處過去四年線,它沒有做它應該並且在數據對象的數據。任何意見或正確的方向快速提示非常感謝!謝謝正在從NSJSONSerialization
對象返回you1
你爲什麼要做subdataWithRange?爲什麼你不提供NSError指針給JSONObjectWithData'錯誤:'參數(然後檢查產生的錯誤信息)? –