我嘗試從json下載數據從web服務器到我的ios應用程序。IOS下載數據與JSON的iPhone應用程序
That's的JSON輸出
{
"responseHeader":{
"status":0,
"QTime":37,
"params":{
"wt":"json",
"q":"title:ios"
}
},
"response":{
"numFound":348,
"start":0,
"docs":[
{
"edition":"2. ed.",
"illustrated":"Not Illustrated",
"id":"BSZ117259543",
"author":"Raw, Charles",
"title":"IOS /",
"spelling":"Raw, Charles (DE-576)180904566 Do you sincerely want to be rich? span. IOS/Charles Raw; Bruce Page; Godfrey Hodgson 2. ed. São Paulo : Ed. Expressão e Cultura, 1972 XV, 496 S. Page, Bruce (DE-576)162468539 aut Hodgson, Godfrey (DE-576)161444822 aut",
"last_indexed":"2012-09-02T01:11:38Z",
"recordtype":"marc",
"title_auth":"IOS /",
"title_sort":"ios",
"title_short":"IOS /",
"fullrecord":"00985nam a22003372"
}
]
}
}
我的項目的代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
// 1 Schritt
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
//code executed in the background
// 2
//NSData* kivaData = [NSData dataWithContentsOfURL:[NSURLURLWithString:@"http://api.kivaws.org/v1/loans/search.json?status=fundraising"]];
NSData* kivaData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://bib.flux-services.net/solr/biblio/select?q=title:ios&wt=json"]];
//3
NSDictionary* json = nil;
if (kivaData) {
json = [NSJSONSerialization JSONObjectWithData:kivaData options:kNilOptions error:nil];
}
//4
dispatch_async(dispatch_get_main_queue(), ^{
[self updateUIWithDictionary:json];
});
});
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)updateUIWithDictionary:(NSDictionary*)json {
@try {
label.text = [NSString stringWithFormat:@"%@ from %@",
json[@"docs"][0][@"title"],
json[@"docs"][0][@"edition"],
//json[@"loans"][0][@"loan_amount"],
//json[@"loans"][0][@"use"],
nil
];
}
@catch (NSException *exception) {
[[[UIAlertView alloc]initWithTitle:@"Error"
message:@"Could not parse the json feed."
delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil]show];
NSLog(@"Exception: %@", exception);
}
當我運行PROGRAMM那麼IPhone模擬器無法顯示的數據。
我沒有任何想法,哪裏出問題了!?!
有沒有人有想法或解決方案?
你檢查過你的JSON響應嗎?將錯誤傳遞給JSON序列化可能會被破壞。添加NSLog(「Response dictinary%@」,json);到你的更新方法 – Injectios 2014-09-03 17:25:11
轉到json.org並學習JSON語法。它只需要5-10分鐘。 – 2014-09-03 17:26:11
而且**使用NSJSONSerialization的'error:'parm **! – 2014-09-03 17:27:04