Iphone 如何解析SBJson中的某些数组

Iphone 如何解析SBJson中的某些数组,iphone,arrays,json,parsing,sbjson,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 JSONV

嗨,我这里有这个密码

 - (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"}}
这是该网站的链接

我在这里要做的是解析它自己的UITableView单元格中的每个数组。下面的示例,我如何才能做到这一点

表1单元: 斯蒂芬的派对 蒙骗大道774号 加利福尼亚州萨克拉门托

表2单元: 德万的聚会 全州车道2134号 纽约,纽约


我该怎么做?

为什么要使用
SBJsonParser
?只使用
-JSONValue
更容易!看:

NSDictionary *jsonDict = [jsonData JSONValue];
// you can declare it as ivar
然后,在
-tableView:cellforrowatinexpath:

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

要从当前json获取所有键和值,请执行以下操作 在你的视野里

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]];
}
现在在您的
单元格中,for行索引路径将如下所示

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