페이지

2012년 5월 21일 월요일

JSON parse


// NSMutableArray로 보여주기
        self.jsonParseArray = [result JSONValue];
        NSLog(@"%@",[self.jsonParseArray objectAtIndex:4]);    // 1단계
        for (int i=0; i<[self.jsonParseArray count]; i++) {
            NSLog(@"%@",[[self.jsonParseArray objectAtIndex:i] objectForKey:@"id"]);
        }
        for (NSDictionary *dic in self.jsonParseArray) {
            NSLog(@"%@",dic);    // 2단계
        }




///////////////////////////////////////////////////////////////////////////////
    // Create new SBJSON parser object
    SBJSON *parser = [[SBJSON alloc] init];
    // Prepare URL request to download statuses from Server
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    // Perform request and get JSON back as a NSData object
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    // Get JSON as a NSString from NSData response
    NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
    NSString *searchTag = @"[";
    //NSLog(@"%@", json_string);
    NSLog(@"%d", [json_string rangeOfString:searchTag options:NSCaseInsensitiveSearch].location);
    // parse the JSON response into an object
    // Here we're using NSArray since we're parsing an array of JSON status objects
    NSArray *statuses = [parser objectWithString:json_string error:nil];
    // Each element in statuses is a single status
    // represented as a NSDictionary
    //NSLog(@"2 %@" ,json_string);
    for (NSDictionary *status in statuses)
    {
        // You can retrieve individual values using objectForKey on the status NSDictionary
        // This will print the tweet and username to the console
        NSLog(@"1");
    }
    ///////////////////////////////////////////////////////////////////////////////

댓글 없음:

댓글 쓰기