Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing a JSON array with dictionaries
    primarykey
    data
    text
    <p>I'm having some trouble getting to the data I want to in the JSON file. Here is a shortened version of the output from my console: </p> <pre><code>{ 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 = "$"; }; } </code></pre> <p>I'm trying to parse the file using the built in JSON parsing library. Here is the parser in my <code>viewDidLoad</code> method:</p> <pre><code> _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:&amp;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]; } } </code></pre> <p>I want to store the currency code (like <code>AUD or BRL</code>) into <code>t.currency</code> along with some of the other data contained in the currency dictionary but now my app is crashing. Error code:</p> <pre><code>NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil </code></pre> <p>None of the objects seem to get added to the <code>_tickerArray</code></p> <p>Help?</p> <p>EDIT: Getting the keys to display with the proper data populating other fields:</p> <pre><code> NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&amp;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]; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload