當輸出整個解析的JSON對象,它工作正常,但是試圖輸出一個鍵的值,當它與(null)
不能得到JSON字符串鍵,出現空
上來分析代碼:
-(NSString *)getNews{
__block NSString *strReturn;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://URL/URL.json"]];
__block NSDictionary *json;
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
json = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
NSLog(@"Async JSON news: %@", json[@"ID"]);
//strReturn = json[@"ID"];
}];
return strReturn = @"Hello";
}
這裏是來自服務器上的JSON輸出文件;
[{"ID":"1","Item Description":"Test News Item Active","News Text":"This is a test news story.\r\n\r\nit should have a few line breaks.\r\n\r\nbut that's about it.","Valid From":"2014-05-23 00:00:00","Valid To":"2014-09-30 23:59:59"}]
誰能告訴我,爲什麼它沒有找到的鑰匙ID
價值?
轉到json.org並瞭解JSON語法。這隻需要5-10分鐘,然後你會看到你上面有一個包含單個JSON對象的ARRAY。 –
另外,'錯誤'parm是有原因的,如果你忽略它,我會惡意地低估你。 –
謝謝大家,我已經注意到了你所說的一切,但是我有另一個讀取JSON(它不是在一個數組內,因此必須是問題)相同的塊。我將嘗試尋找檢索數組中對象的值的方法 – Jack4