简体   繁体   中英

How to Parse certain arrays in SBJson

Hi I have this code here

 - (void)viewDidLoad
{
    [super viewDidLoad];

    jsonURL = [NSURL URLWithString:@"http://oo.mu/json.php"];
    jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL usedEncoding:nil error:nil];
    self.jsonArray = [jsonData JSONValue];

    SBJsonParser *parser = [[SBJsonParser alloc] init];
    NSDictionary *jsonObject = [parser objectWithString:jsonData error:NULL];
    NSArray *list = [jsonObject objectForKey:@"Dewan's Party"];
    for (NSDictionary *lesson in list)
    {
        // 3 ...that contains a string for the key "stunde"
        NSString *content = [lesson objectForKey:@"Street"];
        NSLog(@"%@", content);
    }

    // Do any additional setup after loading the view, typically from a nib.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [jsonArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    return cell;
}

I'm using this JSon.php file.

{"Steffan's Party":{"Street":"774 Hoodwinked Avenue","City":"Sacramento","State":"California"},"Dewan's Party":{"Street":"2134 Statewide Lane","City":"New York","State":"New York"},"Austin's Party":{"Street":"9090 Gravink Court","City":"Woodland","State":"California"}}

This is a link to the website http://oo.mu/json.php .

What I'm trying to do here is parse each array in it's own UITableView cell. Example below, how can I do this?

Table Cell 1: Steffan's Party 774 Hoodwinked Ave Sacramento, California

Table Cell 2: Dewan's Party 2134 Statewide Lane New York, New York

How can I do this?

To get all keys and values from your current json do the following in your viewDidLoad

NSString *yourJson = @"{\"Steffan's Party\":{\"Street\":\"774 Hoodwinked Avenue\",\"City\":\"Sacramento\",\"State\":\"California\"},\"Dewan's Party\":{\"Street\":\"2134 Statewide Lane\",\"City\":\"New York\",\"State\":\"New York\"},\"Austin's Party\":{\"Street\":\"9090 Gravink Court\",\"City\":\"Woodland\",\"State\":\"California\"}}";
SBJSON *json = [[SBJSON alloc] init];

NSDictionary *dic = [json objectWithString:yourJson error:NULL];

//NSMutableArray * keys; is defined in your interface .h file
//NSMutableArray * values; is defined in your interface .h file
keys = [[NSMutableArray alloc] init];
values = [[NSMutableArray alloc] init];    
for(NSString *key in dic.keyEnumerator)
{
    [keys addObject:key];
    [values addObject:[dic objectForKey:key]];
}

now in your cellForRowAtIndexPath would look like this

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    //Steffan's Party 774 Hoodwinked Ave Sacramento, California
    NSString *partyName = [keys objectAtIndex:indexPath.row];
    NSDictionary *dicValues = [values objectAtIndex:indexPath.row];

    NSString *Street = [dicValues objectForKey:"Street"];
    NSString *City = [dicValues objectForKey:"City"];
    NSString *State = [dicValues objectForKey:"State"];

    //Steffan's Party 774 Hoodwinked Ave Sacramento, California
    NSString *fullString = [NSString stringWithFormat:@"%@ %@ %@ %@", partyName, Street, City, State];
    return cell;
}

Why are you using SBJsonParser ? It's easier using only -JSONValue ! Look:

NSDictionary *jsonDict = [jsonData JSONValue];
// you can declare it as ivar

Then, in -tableView:cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    // that's how you can get key by index
    NSString *key = [[jsonDict allKeys] objectAtIndex:indexPath.row];

    NSDictionary *party = [jsonDict objectForKey:key];
    NSString *street = [party valueForKey:@"Street"];
    NSString *city = [party valueForKey:@"City"];
    NSString *state = [party valueForKey:@"State"];

    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@ %@, %@", key, street, city, state];

    return cell;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM