2011-08-25 29 views
0

我有这种格式的字符串: - {panel:{start:[{“element_id”:0,“element_name”:0,“element_image”:0,“element_desc”:0,“element_dob” “奖”:0},]}我该如何解析这个字符串。请帮我解决这个问题。我试图SBJSon - 代码: -如何解析iphone中的字符串,目标c?

NSLog
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 alloc] 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); 
} 

NSDictionary饲料越来越零值..

回答

0

试试下面的一个...

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

第一,我会尝试。感谢您的回复... – Lion

0

您可以使用正则表达式(wiki )。采取来源:RegexKit,添加到链接器标志“-fopenmp”并在NSString消息“stringByMatching”中使用。

例如,如果你想采取 “ELEMENT_NAME”:

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