使用SBJson,如何从对象数组中检索字符串列表? [重复]

时间:2022-03-18 13:20:41

Possible Duplicate:
Parse JSON in Objective-C with SBJSON

可能重复:使用SBJSON在Objective-C中解析JSON

I have below JSON Response (String). I want to parse it into an NSArray with all the patient names.

我有JSON Response(String)。我想把它解析成一个带有所有患者姓名的NSArray。

[{"pat_reg_no":"111181031P1","app_start_time":"10.15","pat_firstname":"Will Smith"},
 {"pat_reg_no":"111181031P2","app_start_time":"11.15","pat_firstname":"Shane Watson"},
 {"pat_reg_no":"111181031P3","app_start_time":"12.15","pat_firstname":"Michael Hussey"},
 {"pat_reg_no":"111181031P1","app_start_time":"10.15","pat_firstname":"Will Smith"}]

How do I parse this?

我该如何解析这个?

4 个解决方案

#1


2  

Try Below Code.

尝试以下代码。

NSString* jsonString;
//jsonString suppose this String has That JSON Response.

SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *jsonResponse = (NSDictionary*)[parser objectWithString:jsonString error:nil];
NSArray *pat_reg_noArray = [jsonResponse valueForKey:@"pat_reg_no"] ;
NSArray *app_start_timeArray= [jsonResponse  valueForKey:@"app_start_time"] ;
NSArray*firstnameArray=[jsonResponse  valueForKey:@"pat_firstname"];

I hope It 'll work.

我希望它会起作用。

#2


3  

I write a demo for you.

我为你写了一个演示。

SBJsonParser *parser = [[SBJsonParser alloc] init];
id jsonObj = [parser objectWithString:@"[{\"pat_reg_no\":\"111181031P1\",\"app_start_time\":\"10.15\",\"pat_firstname\":\"Will Smith\"},{\"pat_reg_no\":\"111181031P2\",\"app_start_time\":\"11.15\",\"pat_firstname\":\"Shane Watson\"},{\"pat_reg_no\":\"111181031P3\",\"app_start_time\":\"12.15\",\"pat_firstname\":\"Michael Hussey\"},{\"pat_reg_no\":\"111181031P1\",\"app_start_time\":\"10.15\",\"pat_firstname\":\"Will Smith\"}]"];

if ([jsonObj isKindOfClass:[NSArray class]]) {
    for (id obj in jsonObj) {
        if ([obj isKindOfClass:[NSDictionary class]]) {
            NSString *name = [obj objectForKey:@"pat_firstname"];
            NSLog(@"name %@", name);
        }
    }
}
[parser release];

#3


1  

The Array which you have posted belongs to someKey, so do the following

您发布的数组属于someKey,请执行以下操作

SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary * dictionary = [jsonParser objectWithString:YourString];
NSArray * array = [dictionary objectForKey:someKey];
NSMutableArray *nameArray = [NSMutableArray new];
for (NSDictionary *dict in array)
{
    [nameArray addObject:[dict objectForKey:@"pat_firstname"];
}
NSLog(@"x is %@",nameArray);
[jsonParser release];

Hope this will solve your problem...

希望这能解决你的问题......

#4


0  

Try this:

NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSArray* array = [(NSDictionary*)[jsonString JSONValue] objectForKey:@"results"];

#1


2  

Try Below Code.

尝试以下代码。

NSString* jsonString;
//jsonString suppose this String has That JSON Response.

SBJSON *parser = [[[SBJSON alloc] init] autorelease];
NSDictionary *jsonResponse = (NSDictionary*)[parser objectWithString:jsonString error:nil];
NSArray *pat_reg_noArray = [jsonResponse valueForKey:@"pat_reg_no"] ;
NSArray *app_start_timeArray= [jsonResponse  valueForKey:@"app_start_time"] ;
NSArray*firstnameArray=[jsonResponse  valueForKey:@"pat_firstname"];

I hope It 'll work.

我希望它会起作用。

#2


3  

I write a demo for you.

我为你写了一个演示。

SBJsonParser *parser = [[SBJsonParser alloc] init];
id jsonObj = [parser objectWithString:@"[{\"pat_reg_no\":\"111181031P1\",\"app_start_time\":\"10.15\",\"pat_firstname\":\"Will Smith\"},{\"pat_reg_no\":\"111181031P2\",\"app_start_time\":\"11.15\",\"pat_firstname\":\"Shane Watson\"},{\"pat_reg_no\":\"111181031P3\",\"app_start_time\":\"12.15\",\"pat_firstname\":\"Michael Hussey\"},{\"pat_reg_no\":\"111181031P1\",\"app_start_time\":\"10.15\",\"pat_firstname\":\"Will Smith\"}]"];

if ([jsonObj isKindOfClass:[NSArray class]]) {
    for (id obj in jsonObj) {
        if ([obj isKindOfClass:[NSDictionary class]]) {
            NSString *name = [obj objectForKey:@"pat_firstname"];
            NSLog(@"name %@", name);
        }
    }
}
[parser release];

#3


1  

The Array which you have posted belongs to someKey, so do the following

您发布的数组属于someKey,请执行以下操作

SBJSON *jsonParser = [[SBJSON alloc] init];
NSDictionary * dictionary = [jsonParser objectWithString:YourString];
NSArray * array = [dictionary objectForKey:someKey];
NSMutableArray *nameArray = [NSMutableArray new];
for (NSDictionary *dict in array)
{
    [nameArray addObject:[dict objectForKey:@"pat_firstname"];
}
NSLog(@"x is %@",nameArray);
[jsonParser release];

Hope this will solve your problem...

希望这能解决你的问题......

#4


0  

Try this:

NSString *jsonString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSArray* array = [(NSDictionary*)[jsonString JSONValue] objectForKey:@"results"];