>> self.arrayObj is nil at this point. It's empy array that's ready to store the object "ob"
No, no! 🙂
A nil object isn't an empty array. You have to start by creating an actual array with 0 elements.
>> I found out that my json send all the elements "id" together at once to ob.id , all elements "name" together at once to "ob.name" and same goes to age.
I'm just guessing, but I suspect this isn't true. Since you reference '[json count]', it looks like 'json' is actually a NSArray, and I bet it's an array of NSDictionary objects. (That's the sort of thing you would normally get back from [NSJSONSerialization JSONObjectWithData:…].
If you send a '[valueForKeyPath: @"name"]' message to a NSArray, it actually tries sending a 'name' message to each element of the array, for reasons that have nothing to do with JSON. If the array elements are dictionaries, that will retrieve the value for key "name" from the dictionary. The results are combined into a new NSArray containing the names.
If I'm right, your code should actually look something like this:
for(int i = 0; i<[json count]; i++)
{
NSDictionary* j = [json objectAtIndex: i];
gg *ob = [[gg alloc] init];
ob.id = [j objectForKey: @"id"];
ob.name = [j objectForKey: @"name"];
ob.age = [j objectForKey: @"age"];
[self.arrayObj addObject: ob];
}
Actually, it would be even better to "modernize" your syntax, and rewrite in this equivalent form:
for(NSUInteger i = 0; i<json.count; i++)
{
NSDictionary* j = json [i];
gg *ob = [[gg alloc] init];
ob.id = j [@"id"];
ob.name = j [@"name"];
ob.age = j [@"age"];
[self.arrayObj addObject: ob];
}
It means exactly the same thing, but it's more readable.