将对象从NSMutableDictionary添加到NSMutableArray
我是新来的ios开发,我想从数据库中添加一些对象,我把它放到一个NSMutabledictionary
,我想放入一个NSMutableArray
显示在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); }
先谢谢你!
如果你想要字典的值然后尝试:
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];
将所有值存入数组
NSMutableArray *jsonArray = [[NSMutableArray alloc] initWithArray:[json allValues]];
获取所有的密钥到一个数组中
NSMutableArray *jsonKeyArray = [[NSMutableArray alloc] initWithArray:[json allKeys]];