我從一個Web服務這樣JSON:廣東話訪問序列化JSON數據(NSJSONSerialization)
{
"Respons": [{
"status": "101",
"uid": "0"
}]
}
我曾嘗試用下面的訪問數據:
NSError* error;
//Response is a NSArray declared in header file.
self.response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSString *test = [[self.response objectAtIndex:0] objectForKey:@"status"]; //[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance
NSString *test = [[self.response objectForKey:@"status"] objectAtIndex:0]; //(null)
但他們沒有工作,如果我NSLog的NSArray舉行串行化數據,這我我得到:
{
Respons = (
{
status = 105;
uid = 0;
}
);
}
我如何訪問數據?
閱讀錯誤。 JOSN是一本字典,而不是一個數組。 – rmaddy
self.response應該是NSDictionary – random
請參閱json.org。學習如何閱讀JSON(學習大約需要10分鐘)。然後瞭解解碼JSON如何使用NSLog轉儲(幾乎和原始JSON一樣,只使用'()'而不是'[]'而不是所有數據都被引用)。 –