Nitya
Nitya

Reputation: 859

Store App names in NSArray from JSON DATA which Stored in NSDictionary

I have retrive following response form Server in JSON format which I have sucessfully store in NSDictionary.

{
    "@companyName" = "MobileAPPSDeveloper";
    "@generatedDate" = "8/6/13 2:41 AM";
    "@version" = "1.0";
    application =     (

            {
        "@apiKey" = 234FXPQB36GHFF2334H;
        "@createdDate" = "2013-03-01";
        "@name" = FirstApp;
        "@platform" = iPhone;
    },
            {
        "@apiKey" = 3PF9WDY234546234M3Z;
        "@createdDate" = "2013-02-01";
        "@name" = SecondAPP;
        "@platform" = iPhone;
    },
            {
        "@apiKey" = NXGQXM2347Y23234Q4;
        "@createdDate" = "2013-05-22";
        "@name" = ThirdApp;
        "@platform" = Android;
    }
);

}

This is method I have used.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    [self convertDataIntoDictionary:responseData];
}

-(void)convertDataIntoDictionary:(NSData*)data{
    NSDictionary *dictionary;
    if (data.length>0) {
        NSError *parsingDataError;
        dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&parsingDataError];
  
        if (parsingDataError) {
            //            [ErrorAlert showError:parsingDataError];
            NSString *recievedString = [[NSString  alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"Data Conversion Error When Parsing: %@", recievedString);
        }
    }

    NSLog(@"Data back from server\n %@",dictionary);
    NSArray *applicationDetails = dictionary[@"application"];

}

I can not go further then this?

I would like to get all NAME of APPS in Array.

Any help would be appreciate.

Upvotes: 0

Views: 432

Answers (2)

Puneet Sharma
Puneet Sharma

Reputation: 9484

The basics of parsing JSON is:

{ }- dis represents dictionary
( )- dis represents array

So in your JSON, there is a dictionary at the root and inside it has a key application which cbntains an Array.

NSArray *applicationArray = [dictionary objectForKey:@"application"];
NSMutableArray *mutableAppNamesArray = [[NSMutableArray alloc]init];

Inside this array there are three NSDictionary at every index:

for(NSDictionary *dict in applicationArray){
  NSString *appName = [dict objectForKey:@"@name"];
  NSLog(@"app name : %@", appName);
  [mutableAppNamesArray addObject:appName];
}

NSArray * appNamesArray = [NSArray arrayWithArray:mutableAppNamesArray];

Upvotes: 1

Parth Patel p1nt0z
Parth Patel p1nt0z

Reputation: 583

I think this is not a proper format for JSON. JSON objects usually contain ':' instead of '='

Example :-

{
products_id: "476",
products_model: "XOLO Play",
products_price: "15499.0000",
products_image: "xolo-play-play-t1000-.jpg",
products_date_available: null,
products_last_modified: "2013-08-04 06:04:11",
products_date_added: "2013-08-04 06:03:10",
manufacturers_id: "17",
products_status: "1",
products_rating: null,
category_id: "11"
},
{
products_id: "18",
products_model: "Nokia Lumia 820",
products_price: "8990.0000",
products_image: "samsung-galaxy-ace-duos-gsm-mobile-phone-large-1.jpg",
products_date_available: null,
products_last_modified: "2013-07-30 03:45:28",
products_date_added: "2013-01-23 00:52:00",
manufacturers_id: "2",
products_status: "1",
products_rating: "3",
category_id: "11"
}, 

But if you still need to parse it then you might try as below:

First get into the array by NSDictionary *array = [allValues objectForKey:@"application"]; then each index to NSDictionary and then object for key. If its your server response than try to make it in the proper JSON format.

Upvotes: 0

Related Questions