开发者

how to parse a string in iphone, objective c?

I have a string in this format :- { panel : {start : [{"element_id" : 0, "element_name" :0, "element_image" : 0, "element_desc" : 0, "element_dob" : 0, "awards" :0},]} how can I parse this string. please help me to overcome this problem. I tried SBJSon - code: -

NSLog(@"response string before = %@", responseStr);
NSData *fileData = [NSData dataWithContentsOfFile:responseStr];
NSString *responseString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
NSLog(@"response string after = %@", responseString);
NSError *jsonError = nil;
NSDictionary *feed = nil;
    SBJsonParser *json = [[SBJsonParser new] autorelease];
feed = [json objectWithString:responseString error:&jsonError];
NSLog(@"feed = %@", feed);
if ([jsonError code]==0) {
    // get the array of "results" from the feed and cast to NSArray
    NSMutableArray *localObjects = [[[NSMutableArray all开发者_如何学JAVAoc] init] autorelease];
    NSArray *results= (NSArray *)[feed valueForKey:@"start"];

    // loop over all the results objects and print their names
    int ndx;

    for (ndx = 0; ndx < results.count; ndx++) 
    {
        [localObjects addObject:(NSDictionary *)[results objectAtIndex:ndx]];
    }
    NSLog(@"local objects = %@", localObjects);
}

the NSDictionary feed is getting nil value in NSLog..


Try the following one...

    SBJSON *json = [[SBJSON new] autorelease];  
    NSDictionary *dictResponse = (NSDictionary*) [json objectWithString:sitesResponse error:nil];


You can use Regular Expressions (wiki). Take sources from: RegexKit, add to linker flags "-fopenmp" and use in NSString message "stringByMatching".

For example, if you want take "element_name":

NSString* str = [your_string stringByMatching:@"element_name\".*:(.*?)," capture: 1L];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜