myData
myData

Reputation: 215

JSON Parse iOS Array of Dictionaries parsed into array only displays last object in tableview

I have a JSON array of dictionaries that I'm parsing. My problem is where I want to display all the records that fit my required key-value pair (key is name) within the array, I'm only displaying the very last one in my tableview. It's almost 4am and I've been going around in circle since 10pm so any help would be greatly appreciated as I'm overlooking something. Thanks!

NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.someArray = [[NSMutableArray alloc]init];

for (NSMutableDictionary *dictionary in jsonDataArray)
{
    NSMutableArray* Response_array = [dictionary objectForKey:@"loans"];
    NSLog(@"Array: %@", Response_array);

    NSDictionary *dict = [Response_array objectAtIndex:0];
    NSLog(@"dict is %@",dict);

    self.someMutableArray = [dict objectForKey:@"name"];

    NSLog(@"what is name %@",self.someMutableArray);
}

and I tried:

NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];   

for ( int i = 0 ; i < jsonDataArray.count ; i++ )
{
    self.someDictionary = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
    self.someMutableArray = [self.someDictionary valueForKey:@"name"];
    NSLog(@"MutableArray inside is %@", self.someMutableArray);    
} 

NSLog(@"MutableArray outside is %@",self.someMutableArray);

On my last attempt I moved the location of valueForKey:@"name":

NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.someMutableArray = [NSMutableArray array];


for ( int i = 0 ; i < jsonDataArray.count ; i++ )
{
    self.someMutableArray = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
}

NSLog(@"someMutableArray outside is %@",self.someMutableArray);

Then in cellForRowAtIndexPath of my tableview, I have:

NSDictionary *dict2 = [self.someMutableArray objectAtIndex:indexPath.row];
NSLog(@"what is dict2 %@", dict2);
cell.textLabel.text = [dict2 valueForKey:@"name"];

Here's the setup of my JSON:

[
  -{
     id: 285,
     name: name 1,
    -loans: [
         -{
            id: 42,
            name: "Shark"
          }
     ]
   },
  -{
     id: 286,
     name: name 2,
     -loans: [
          -{
             id: 50,
             name: "Flipper"
           }
      ]
   }
]

Upvotes: 1

Views: 15522

Answers (4)

Hetal
Hetal

Reputation: 51

for ( int i = 0 ; i <jsonDataArray.count ; i++ )
{
    self.someDictionary = [[jsonDataArray objectAtIndex:i] objectForKey:@"loans"];
    [self.someMutableArray  addObject:[self.someDictionary valueForKey:@"name"]]
    NSLog(@"MutableArray inside is %@", self.someMutableArray);    
} 
NSLog(@"MutableArray outside is %@",self.someMutableArray);

----------

Upvotes: 0

Roby
Roby

Reputation: 69

self.dic_response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
if([self.dic_response valueForKey:@"success"]){
NSMutableArray *resultArray = [NSMutableArray alloc];
for(NSDictionary *tmpArr1 in self.dic_response){
{
    [resultArray addObject:tmpArr1];
    //this resultArray have whole array value.
}

}

Hope this is helps you

Upvotes: 0

Mohd Sadham
Mohd Sadham

Reputation: 435

From my example:

My JSON File:

{
status: "Success",
totalcount: "25",
currentcount: "25",
ticketreplies: [
  {
ticket_id: "5348CD97",
subject: "bA==",
posteduser: "10025",
postedby: "Admin (Paul)",
message: "PGRpdj4",
post_id: "75",
posted_on: "23 Apr 2014 12:58 IST",
photo: "10025.jpg"
},
{
ticket_id: "5348F754",
subject: "Y2FsbCBv",
posteduser: "10025",
postedby: "Admin (John)",
message: "PGRpdj4",
post_id: "74",
posted_on: "23 Apr 2014 11:46 IST",
photo: "10025.jpg"
}
    ]
}

My Implementation:

self.dic_response=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"Dictionary: %@",self.dic_response);
NSLog(@"Dictionary: %i",[self.dic_response count]);

NSString usrTicketId=[self.dic_response valueForKey:@"totalcount"];
NSLog(@"Ticket_Id: %@",usrTicketId);

NSArray arrTicketRplys=[self.dic_response valueForKey:@"ticketreplies"];
NSLog(@"Ticket_Id: %@",arrTicketRplys);

for(NSArray *tmpArr1 in arrTicketRplys){
    if([[tmpArr1 valueForKey:@"posteduser"] isEqualToString:@"10025"]){
        NSString *strPostUserName=[tmpArr1 valueForKey:@"postedby"];
        NSLog(@"Posted BY: %@", strPostUserName);
    }
}

// Sure, it'll logic work for read specific object keys and values. // Mainly, these code avoid static values like objectAtIndex:4.

Upvotes: 0

Retro
Retro

Reputation: 4005

Why are you going for a looping when already you have the Array? Just allocate your array with JSON response

NSArray *jsonDataArray = [[NSArray alloc] initWithArray:[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]];

this array will be your datasource so return its count from delegate and in you rowAtIndexPath

NSDictionary *dictObject = [jsonDataArray objectAtIndex:indexPath.row];
cell.textLabel.text = [dictObject valueForKey:@"Shark"];

For custom cell do like

NSDictionary *dictObject = [jsonDataArray objectAtIndex:indexPath.row];
cell.textLabel1.text = [dictObject valueForKey:@"Shark"];
cell.textLabel2.text = [dictObject valueForKey:@"Flipper"];

Upvotes: 4

Related Questions