如何在 iphone、objective c 中解析字符串?

发布于 2024-12-01 08:00:05 字数 1292 浏览 0 评论 0原文

我有一个这种格式的字符串:- { panel : {start : [{"element_id" : 0, "element_name" :0, "element_image" : 0, "element_desc" : 0, "element_dob" : 0, "awards" :0},]} 我如何解析这个字符串。请帮助我克服这个问题。我尝试了 SBJSon - 代码: -

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 feed 在 NSLog 中获取 nil 值。

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 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);
}

the NSDictionary feed is getting nil value in NSLog..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-12-08 08:00:05

尝试以下一个...

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

Try the following one...

    SBJSON *json = [[SBJSON new] autorelease];  
    NSDictionary *dictResponse = (NSDictionary*) [json objectWithString:sitesResponse error:nil];
情深已缘浅 2024-12-08 08:00:05

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

例如,如果您想采用“element_name”:

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文