0

I have used web service and get the response as the JSON. I want to extract the details from the array.

Here my sample code,

    NSString *urlDataString = [[NSString alloc] initWithData:receiveData encoding:NSUTF8StringEncoding];
    SBJSON *parser = [[SBJSON alloc] init];
    NSError *error = nil;
    self.customArray = [parser objectWithString:urlDataString error:&error];

    for (NSDictionary *feedItem in customArray)
    {        
        NSString *listDeals = [feedItem objectForKey:@"ListOfDeals"];
        [tempArray addObject:listDeals];
    }

If i have used the above code all the strings are stored into the temp array and the array value 1. But i want to store the data are to be separate index.So that i have to get the details using the index position.

if i am using like this,

for (NSDictionary *feedItem in self.dollarArray)
    {        
        NSString *listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"Description"];
        [tempArray addObject:listDeals];
    }

I get all the description values from tempArray, but i want to get all the strings from the array.

Actually i am expecting the temp array count 3 and want to get values like

 NSLog(@"%@",[tempArray ObjectAtIndex:0]);

           {
                    BusId = 14;
                    Description = "Sample data"
                    Id = 60;
                    StartTime = "7/28/2011 8:30:00 PM";
                },

Here my sample response,

 (
            ListOfDetails =         (
                            {
                    BusId = 14;
                    Description = "Sample data"
                    Id = 60;
                    StartTime = "7/28/2011 8:30:00 PM";
                },
                            {
                     BusId = 16;
                    Description = "Test data"
                    Id = 62;
                    StartTime = "7/29/2011 8:30:00 PM";
                },
                            {
                     BusId = 15;
                    Description = "Test"
                    Id = 61;
                    StartTime = "7/27/2011 8:30:00 PM";
                },
      )

So how can i do that? Please guide me.

Thanks!

2 Answers 2

2
for (NSDictionary *feedItem in self.dollarArray){

    for (NSMutableArray *vijayTemp in [feedItem objectForKey:@"ListOfDeals"]) {

        [tempArray addObject:vijayTemp];

    }

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

Comments

1
for (NSDictionary *feedItem in self.dollarArray)
    {        
      NSDictionary *listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"Description"];
     [tempArray addObject:listDeals]; // Gets description
     listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"BusId"];
     [tempArray addObject:listDeals]; // Gets BusId
     listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"Id"];
     [tempArray addObject:listDeals]; // Gets Id
     listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"StartTime"];
     [tempArray addObject:listDeals]; // Gets StartTime

    }

Alternatively you can use the return from [feedItem objectForKey:@"ListOfDeals"] which is a Dictionary for all the 4 values. You can save them as above or print them using NSLog and %@ format specifier.

Edit:

You can create a array from dictionary using the allvalues method. I guess this should answer your question.

NSArray *tempArray = [dictionary allvalues];


allValues
Returns a new array containing the dictionary’s values.

- (NSArray *)allValues

3 Comments

Thanks for the reply. I know that way, but i have more than 15 strings from that array, so it would be take more code. SO is any possible way to store all the string into the array index position? without using like, listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"Id"]; [tempArray addObject:listDeals]; // Gets Id
Actually i want to store all the strings to the array, so that i could access the strings wherever i have to use like this, [[tempArray objectAtIndex:0] objectForKey:@"Description"] and [[tempArray objectAtIndex:2] objectForKey:@"StartTime"]
But your answer is exactly correct, i don't want to add the strings one by one like listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"BusId"]; [tempArray addObject:listDeals]; // Gets BusId listDeals = [[feedItem objectForKey:@"ListOfDeals"] valueForKey:@"Id"]; [tempArray addObject:listDeals]; // Gets Id So is any possible way to put all the dictionary values into array.

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.