0

嗨,我这里有这段代码

 - (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:Steffan 的派对 774 Hoodwinked Ave Sacramento, California

表格单元格 2:Dewan 的派对 2134 Statewide Lane New York, New York

我怎样才能做到这一点?

4

2 回答 2

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;
}
于 2012-06-09T22:07:57.717 回答
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;
}
于 2012-06-09T21:51:13.160 回答