1

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

}

Thank you in advance!

4
  • Can you please add a sample of the response you're getting from your url (the json text you're trying to parse)? Commented May 29, 2012 at 10:03
  • [{"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."}] Commented May 29, 2012 at 10:08
  • when i run it with the NSLog it gives the below result, so i wanted to put the result into a NSMutableArray Commented May 29, 2012 at 10:26
  • 2012-05-29 12:22:42.051 iShop[4829:f803] ( { 0 = "Samsung LCD 42 TV"; 1 = "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. "; Name = "Samsung LCD 42 TV"; Price = "7900.99"; } ) Commented May 29, 2012 at 10:27

2 Answers 2

3

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.

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];
Sign up to request clarification or add additional context in comments.

1 Comment

That's why I asked him as well :P This is the most evil pitfall with that kind of questions, we just 'assume' that the OP knows how the actual data structure is. Anyway +1 for being quicker than me :)
0

To get all values into an array

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

To get all keys into an array

NSMutableArray *jsonKeyArray = [[NSMutableArray alloc] initWithArray:[json allKeys]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.