0

I am trying to learn how to use API's with Objective-C. I am using the data from here: https://btc-e.com/api/2/ltc_usd/ticker and I only want the 'last' value. I have tried extracting the value like this:

NSURL * url=[NSURL URLWithString:@"https://btc-e.com/api/2/ltc_usd/ticker"];
NSData * data=[NSData dataWithContentsOfURL:url];
NSError * error;

NSMutableDictionary  * json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSArray *keys = [json allKeys];
NSString *jsonStr = [json objectForKey:keys[0]];

NSArray *c1 = [jsonStr componentsSeparatedByString:@"last = \""];
NSArray *c2 = [[c1 objectAtIndex:1] componentsSeparatedByString:@"\";"];
NSString *result = [c2 objectAtIndex:0];

NSLog(@"%@", result);

However, this gives me the following errors:

2014-03-02 15:03:24.915 Litecoin Ticker[5727:303] -[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x608000240690
2014-03-02 15:03:24.915 Litecoin Ticker[5727:303] -[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x608000240690

I'm not entirely sure this is only way to extract values from API's, but i can't seem to find out how to do it. Can anyone help?

3
  • Go to json.org and learn the JSON syntax. It takes 5-10 minutes and knowing it makes everything easier to understand. Commented Mar 2, 2014 at 15:19
  • And there is no need to use componentsSeparatedByString. Just reference the NSDictionarys using objectForKey (or the new form using []). NSNumber* last = json[@"ticker"][@"last"]; Commented Mar 2, 2014 at 15:22
  • (And, especially when taking data off the web, always check the value back from NSJSONSerialization and, if nil, at least NSLog the error value.) Commented Mar 2, 2014 at 15:23

1 Answer 1

1
NSString *jsonStr = [json objectForKey:keys[0]];
^^^^^^^^^^^^^^^^^
// nope, it's a NSDictionary...

...and it has already been parsed!

And if you NSLog it you will see it's content. Here's how you access the last field after you have the JSON parsed by NSJSONSerialization:

NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &error];
NSNumber *last = json[@"ticker"][@"last"];

And that's it.


As a side note

NSData * data = [NSData dataWithContentsOfURL:url];

is awful, since it's synchronous! Consider using an asynchronous approach (the nuclear - and probably best - option is to use AFNetworking). Here's a complete AFNetworking example:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"https://btc-e.com/api/2/ltc_usd/ticker" parameters:nil success:^(AFHTTPRequestOperation *operation, id JSON) {
    NSNumber *last = JSON[@"ticker"][@"last"];
    NSLog(@"last value: %@", last);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, I can't believe I was overcomplicating it that much! What does the AFNetworking part do?
It's a networking framework. It basically wraps the Objective-C networking API into a nicer block-based one.
@user2397282 w.r.t. your specific case it makes the call asynchronous, so that it doesn't block the thread you're performing it on (if it's the main thread you're going to block the UI, until the request is performed!)
Note that the return value for last will be an NSNumber, not an NSString.
@GabrielePetronella I have tried using your asynchronous approach, but it gives me an error when I run, basically saying that the data in the link is in the wrong format: text/html, not json?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.