开发者

how to parse JSON data and show content in iphone app

I want that the object which are coming in array how to store them in object like when i use like

myArray.id=
myArray.name=

so it shuls show values

SBJsonParser *parser = [[SBJsonParser alloc]开发者_如何学Python init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.krsconnect.no/community/api.html?method=categories&appid=620&mainonly=true"]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];
NSArray *results = [parser objectWithString:json_string error:nil];


There is a great tutorial on how to parse JSON by Matt Gallagher here. Hope that gets you started in the right direction.

The following should display results for your code/JSON :

// Get JSON as a NSString from NSData response
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSDictionary *object = [parser objectWithString:json_string error:nil];

// the JSON response is an Array
NSArray *results = [parser objectWithString:json_string error:nil];
NSDictionary *dictOne = [results objectAtIndex:0];
NSArray *activitiesArray = [dictOne objectForKey:@"activities"];
NSDictionary *dictTwo = [activitiesArray objectAtIndex:0];
NSDictionary *eventDict = [dictTwo objectForKey:@"event"];

NSLog(@"%@ - %@", [eventDict objectForKey:@"category"]);


There are some things you have to look at when calling data from json, but its quite easy. First of all you should add these two lines at the top of you m-file:

#define kAsyncModus dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kYourJsonURL [NSURL URLWithString: @"_THE_JSON_URL_"]

then, insert following part into your viewDidLoad method. "dispatch_async" has to be used with the previous defined macro (at the top) so you create a thread. Otherwise your app could crash if maybe there is no response from the server but the method keeps waiting for it.

// get elements of json
dispatch_async(kAsyncModus, ^{
    NSData *data = [NSData dataWithContentsOfURL: kYourJsonURL];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data
                        waitUntilDone:YES];
});       

and create a new method that will get single elements out of the received json

- (void)fetchedData:(NSData *)responseData{
    NSError *error;
    NSDictionary *json =
    [NSJSONSerialization JSONObjectWithData:responseData
                                    options:kNilOptions
                                      error:&error];

    NSArray *results = [json objectForKey:@"_firstElementInJsonFile_"];


    NSLog(@"Results: %@", results);

}

for more detailes information you use the results

NSDictionary *specificData = [results objectAtIndex:0];

and it should be done.

I just explained it in the shortest way, if you want to have all facts look at following tutorial. But I'm not sure, if there is support for older devices that use < iOS5. I couldn't test it till know. If you need something like this, maybe you can look at JSONKit on Git. Maybe there is somebody who knows these detailes, would be interesting to know for me too.


JSONKit makes this pretty easy, might be worth considering. And its very fast.


The json-framework is the way to go.

However, this JSON is not valid. Tell whoever is providing the API to ensure their JSON returns are valid.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜