2

我在获取 JSON 文件中想要的数据时遇到了一些麻烦。这是我的控制台输出的缩短版本:

{
    AUD =     {
        15m = "125.15547";
        24h = "124.74";
        buy = "121.0177";
        last = "125.15547";
        sell = "123.44883";
        symbol = "$";
    };
    BRL =     {
        15m = "120.34";
        24h = "120.34";
        buy = "120.34";
        last = "120.34";
        sell = "120.34";
        symbol = "R$";
    };
    CAD =     {
        15m = "129.08612";
        24h = "131.07";
        buy = "128.66227";
        last = "129.08612";
        sell = "129.08612";
        symbol = "$";
    };
}

我正在尝试使用内置的 JSON 解析库来解析文件。这是我 viewDidLoad方法中的解析器:

    _tickerArray = [NSMutableArray array];

    NSURL *tickerDataURL = [NSURL URLWithString:@"https://blockchain.info/ticker"];
    NSData *jsonData = [NSData dataWithContentsOfURL:tickerDataURL];
    NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"%@", dataDictionary);
    NSArray *ar = [NSArray arrayWithObject:dataDictionary];
    for (NSString *key in [dataDictionary allKeys]) {
        for (NSDictionary *dict in ar) {
            TickerData *t;
            t.currency = [dict objectForKey:key];
            t.symbol = [dict objectForKey:@"symbol"];
            t.last = [dict objectForKey:@"last"];
            [_tickerArray addObject:t];
        }

    }

我想将货币代码(如AUD or BRLt.currency与货币字典中包含的其他一些数据一起存储,但现在我的应用程序崩溃了。错误代码:

NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil

似乎没有任何对象被添加到_tickerArray

帮助?

编辑:获取键以显示填充其他字段的正确数据:

 NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
    NSLog(@"%@", dataDictionary);


    for (NSString *key in [dataDictionary allKeys]) {
        NSDictionary *dic=[dataDictionary objectForKey:key];
            TickerData *t=[[TickerData alloc] init];
            t.currency = key;//EDITED
            t.symbol = [dic objectForKey:@"symbol"];
            t.last = [dic objectForKey:@"last"];
            [_tickerArray addObject:t];

    }
4

3 回答 3

4

t为零,你必须分配/初始化它:

TickerData *t = [[TickerData alloc] init];
于 2013-04-04T04:56:45.057 回答
3
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSLog(@"%@", dataDictionary);
//NSArray *ar = [NSArray arrayWithObject:dataDictionary];//REMOVED
for (NSString *key in [dataDictionary allKeys]) {
    NSDictionary *dic=[dataDictionary objectForKey:key];//ADDED
    for (NSString *dickey in [dic allKeys]) { //MODIFIED
        NSDictionary *dict=[dic objectForKey:dicKey];//ADDED
        TickerData *t=[[TickerData alloc] init];//ALLOC INIT ?
        t.currency = key;//EDITED
        t.symbol = [dict objectForKey:@"symbol"];
        t.last = [dict objectForKey:@"last"];
        [_tickerArray addObject:t];
    }

}

您的数据不包含任何数组,它的所有字典,尝试上面的代码也看到评论..

希望它有效..

编辑:

是的,您也已经初始化了对象,正如上面其他答案中所建议的那样。

于 2013-04-04T04:56:43.480 回答
0

尝试一下....

NSURL *url = [NSURL URLWithString:@"https://blockchain.info/ticker"];

NSLog(@"API : %@",url);

NSMutableData *jsonData = [NSMutableData dataWithContentsOfURL:url];

NSString *data = [[NSString alloc] initWithBytes: [jsonData mutableBytes] length:[jsonData length] encoding:NSUTF8StringEncoding];

dictionary = [data JSONValue];
NSDictionary *dict = [dictionary objectForKey:@"AUD"];
NSLog(@"%@",dict);
NSString *last = [dict valueForKey:@"last"];
NSLog(@"%@",last);
于 2013-04-04T04:56:26.737 回答