开发者

How to exract the string from Array in JSON Format in iPhone?

开发者 https://www.devze.com 2023-03-24 07:17 出处:网络
I have used web service and get the response as the JSON. I want to extract the details from the array.

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开发者_JAVA技巧.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!


for (NSDictionary *feedItem in self.dollarArray){

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

        [tempArray addObject:vijayTemp];

    }

 }


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
0

精彩评论

暂无评论...
验证码 换一张
取 消