2

i am newBie in iOS Development. i want to add my JSON Parsing Data Dictionary Array Key Value in to another array But it only Add My Last Array index in to New Array.

My Code like as

-(void)fetchedData:(NSData *)responsedata
{
 if (responsedata.length > 0)
{
    NSError* error;

    self.json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    if ([[_json objectForKey:@"data"] isKindOfClass:[NSArray class]])
    {
        NSArray *arr = (NSArray *)[_json objectForKey:@"data"];
        [self.imageArray addObjectsFromArray:arr];
        [self.storeViewTable reloadData];
    }
    self.storeViewTable.hidden=FALSE;
    }
    NSMutableArray *imagearray=[[NSMutableArray alloc]init];
    for (index=0; index <[self.imageArray count]; index ++)
    {
        for(NSDictionary *dict in self.imageArray )
        {
            imagearray = [dict valueForKey:@"demopage"];
            self.imagesa = imagearray;
        }
    }

     NSLog(@"Array Count %d",[self.imagesa count]);
     NSLog(@"Another array Count %d",[self.imageArray count]);
}

Here self.imagearray is my main array that contain my all JSON Data but i want to Parse A new Data From old Data And add it in to self.imagesa array here For self-images Value for key is demo page and Self.imagearray count is Three(3) So i want all my Three Index value in to self.imagesa but it contain only Last index Value please Give me Solution For that.And my Webservices link is Here. link

2
  • remove imagearray = [dict valueForKey:@"demopage"]; self.imagesa = imagearray; and and add [self.imagesa addObject: [dict valueForKey:@"demopage"]]; and i think you don't need for (index=0; index <[self.imageArray count]; index ++) this loop. Commented Nov 13, 2014 at 10:30
  • @Chinttu-Maddy-Ramani i write for(NSDictionary *dict in self.imageArray ){ [self.imagesa addObject: [dict valueForKey:@"demopage"]]; } NSLog(@"Array Count %@",self.imagesa); then it return null Commented Nov 13, 2014 at 10:36

1 Answer 1

1

your code should look like below.

id json = [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
if ([[json objectForKey:@"data"] isKindOfClass:[NSArray class]])
{
    NSArray *arr = (NSArray *)[json objectForKey:@"data"];
    [imageArray addObjectsFromArray:arr];
}

//do not alloc init if you have already alloc init array.
NSMutableArray *imagesa=[[NSMutableArray alloc]init];
for(NSDictionary *dict in imageArray )
{
    [imagesa addObject:@{@"demopage":[dict valueForKey:@"demopage"]}];
}

NSLog(@"Array Count %lu",(unsigned long)[imagesa count]);
NSLog(@"Another array Count %lu",(unsigned long)[imageArray count]);

and i got below output

Array Count 3

Another array Count 3

NOTE

and if you want to add all demopage dictionary to imagesa array then replace your for loop

for(NSDictionary *dict in imageArray )
{
    NSArray *arr = (NSArray *)[dict valueForKey:@"demopage"];
    [imagesa addObjectsFromArray:arr];
}

and output is

Array Count 69

Another array Count 3

Sign up to request clarification or add additional context in comments.

10 Comments

Here i want My three index array By index into self.imagesa means my array self.imegesa also contain three index because i want to print Three link images into DifferentView Controller
@AshishGabani can not understand what you're trying to say. on 1st type you'll get 3 counts for array in which. at 0 index you'll get 0 index demopagedata.
Bro here my self.image array Contain three index so each index have -demopage Key And i want to Add this -demopage Key value Based On my self.image index into self.imagesa means my main Array Have Three index so i want My self.imagearray have also three index and Here my Webservices link you show it then you understand truemanindiamagazine.com/webservice/magazine_list.php
yes i understand i get First index data but i want all data based on index . means my Data also Contain Three index
@AshishGabani you mean you want array like (demopage=(index 0),demopage=(index 1),demopage=(index 2))?
|

Your Answer

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