2014-09-03 64 views
-2

我尝试从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模拟器无法显示的数据。

我没有任何想法,哪里出问题了!?!

有没有人有想法或解决方案?

+0

你检查过你的JSON响应吗?将错误传递给JSON序列化可能会被破坏。添加NSLog(“Response dictinary%@”,json);到你的更新方法 – Injectios 2014-09-03 17:25:11

+0

转到json.org并学习JSON语法。它只需要5-10分钟。 – 2014-09-03 17:26:11

+1

而且**使用NSJSONSerialization的'error:'parm **! – 2014-09-03 17:27:04

回答

2

首先,例外是​​编程错误。你不会发现异常。如果你得到异常,你的程序崩溃,你找到原因,你修复代码。 Objective-C就是这样做的。人们可能会认为你是一名Java程序员。

其次,你收到的字典的NSLog将是有用的。您打印的内容与您的代码不符。

第三,当您获取JSON数据时,您只需检查一切。你检查你是否收到过字典。你检查是否有一个名为“docs”的属性,它是一个数组。你检查它是否至少有一个元素。你得到第一个元素并检查它是否是字典。您检查字典是否有属性“title”和属性“edition”,并且它们是字符串等。第四,为了帮助保持理智,请定义一个对象来表示JSON数据中的内容,将字典作为初始化工具,并将所有内容提取到该对象中。所以从那时起你就处于安全的NSObject领域。

第五,当用户阅读“无法解析JSON订阅源”时,您认为会发生什么?他们可怜的大脑会爆炸。错误消息是针对用户的,而不是针对程序员的。

+1

不错的一个:)特别是你的最后一点:D – HAS 2014-09-03 17:56:21

+0

我知道你听不到它,但我现在赞扬你的答案 – 2014-09-04 08:58:15