0

Possible Duplicate:
How do I parse JSON with Objective-C?

Let me first say that I am still new to Objective-C but I am looking for way to store JSON data from a restful API to a variable. I have already established a connection to the API I am just unfamiliar with the commands to search to find specific information. Any help or references would be greatly appreciated. Thanks!

Community
  • 1
  • 1
bardockyo
  • 1,415
  • 6
  • 21
  • 32
  • Is not but this might help. I'll check it out. Thanks. – bardockyo Jan 21 '13 at 02:40
  • Your question is kind of ambiguous - do you need help downloading and parsing JSON? Do you need instructions on interacting with your API (is that what you mean by '...commands to search...'?)? – Carl Veazey Jan 21 '13 at 02:42
  • "I am just unfamiliar with the commands to search to find specific information" - there are no standard "JSON commands" to do anything. You can get data as a response to HTTP requests, that's it. What sort of HTTP request means what is up to the creator of the API to decide, you need to work with their documentation. – millimoose Jan 21 '13 at 03:08
  • @Carl I am looking for way to interact with the API. What I am attempting to do is have some info from the API like say a username and store or show that name on a text field within an app if that makes sense. – bardockyo Jan 21 '13 at 03:11
  • You are looking for parsing it, as, while parsing it you can find the information of interest and pull it out, which is why I suggested it is actually a duplicate. – James Black Jan 21 '13 at 03:34
  • Well, either you store the JSON string or you parse the JSON and store the parsed results. – Hot Licks Jan 21 '13 at 03:42

3 Answers3

1

One of the easiest way to retrieve JSON data from a restful API is using the AFNetworking library.

Here's a sample code

NSURL *url = [NSURL URLWithString:@"http://yourAPIUrl/whatever"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    // do whatever you like with your JSON object
    NSLog(JSON);
} failure:nil];

[operation start];
Gabriele Petronella
  • 106,943
  • 21
  • 217
  • 235
1

Add JSON Parser for Objective-c. Its opensource.

SBJSON *parser = [[SBJSON alloc] init];
NSString *tempStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

// parse the JSON string into an object - assuming json_string is a NSString of JSON data
id object = [parser objectWithString:tempStr error:nil];

[resultObj release];
resultObj = [object retain];
Madhu
  • 1,542
  • 1
  • 14
  • 30
-1
NSDictionary* myDict =  [NSJSONSerialization JSONObjectWithData:myJsonData 
                                                        options:0 
                                                          error:&error];
foundry
  • 31,615
  • 9
  • 90
  • 125