将对象从NSMutableDictionary添加到NSMutableArray [英] Adding Objects from NSMutableDictionary to NSMutableArray

查看:50
本文介绍了将对象从NSMutableDictionary添加到NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in ios development, I want to add some object from database which I managed to put into a NSMutabledictionary and I want to put into a NSMutableArray to display in a UITableView. Below is my .m file code:

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

}

提前谢谢!

推荐答案

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

If you want the values of the dictionary then try:

jsonArray = json.allValues;

虽然不会以任何有趣的方式订购它,所以您可能还希望在以后订购它.

It won't be ordered by anything interesting though, so you might want to order it afterwards as well.

但是,如果这确实是您的JSON:

But, if this really is your 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."}]

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

Then that's actually an array anyway, so you probably just want:

jsonArray = [NSJSONSerialization JSONObjectWithData:response options:kNilOptions error:&error];

这篇关于将对象从NSMutableDictionary添加到NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆