1

我是 ios 开发的新手,我想从数据库中添加一些对象,我设法将其放入 a 中NSMutabledictionary,并且我想放入 aNSMutableArray中以显示在 a 中UITableView。下面是我的 .m 文件代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    jsonURL = [NSURL URLWithString:@"http://localhost:8888/read_product_list.php"];

    //jsonData = [[NSString alloc] initWithContentsOfURL:jsonURL];

    NSData *data = [NSData dataWithContentsOfURL:jsonURL];
    [self getData:data];    
}

-(void) getData:(NSData *) response {
    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
    //NSLog(@"%@", json);


    jsonArray = [[NSMutableArray alloc] init];
    /*for(int i = 0; i < json.count; i++){
        [jsonArray addObject:[json objectForKey:];
    }*/

    //NSLog(@"%@", jsonArray);

}

先感谢您!

4

2 回答 2

3

如果您想要字典的值,请尝试:

jsonArray = json.allValues;

不过,它不会被任何有趣的东西订购,因此您可能也想在之后订购。

但是,如果这真的是你的 JSON:

[{"0":"Samsung LCD 42 TV","Name":"Samsung LCD 42 TV","1":"7900.99","Price":"7900.99","2":"Buy the new Samsung 42 nth TV from Hi-if. available only this coming weekend. ","Description":"Buy the new Samsung 42 nth TV from Hi-if. available only this coming weekend. "},{"0":"iPhone 4s","Name":"iPhone 4s","1":"7000","Price":"7000","2":"Buy a brand new iPhone 4s from Orange with a lot of Features such as Siri, 3G and Wi-Fi.","Description":"Buy a brand new iPhone 4s from Orange with a lot of Features such as Siri, 3G and Wi-Fi."}]

那么这实际上是一个数组,所以你可能只想要:

jsonArray = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];
于 2012-05-29T10:08:29.087 回答
0

将所有值放入数组

NSMutableArray *jsonArray = [[NSMutableArray alloc] initWithArray:[json allValues]];

将所有键放入数组

NSMutableArray *jsonKeyArray = [[NSMutableArray alloc] initWithArray:[json allKeys]];
于 2012-05-29T10:09:14.953 回答