如何解析SBJson中的某些数组

时间:2012-06-09 21:24:04

标签: iphone arrays json parsing sbjson

嗨,我这里有这段代码

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

我正在使用这个JSon.php文件。

{"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"}}

这是指向http://oo.mu/json.php网站的链接。

我在这里要做的是解析它自己的UITableView单元格中的每个数组。以下示例,我该怎么做?

表格单元格1: 斯蒂芬的派对 774 Hoodwinked Ave. 加利福尼亚州萨克拉门托

表格单元格2: 德万党 全州巷2134号 纽约,纽约

我该怎么做?

2 个答案:

答案 0 :(得分:1)

要从当前json获取所有键和值,请执行以下操作 在你的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]];
}

现在在你的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];
    }
    //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;
}

答案 1 :(得分:0)

您为什么使用SBJsonParser?仅使用-JSONValue更容易!看:

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

然后,在-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;
}