Naveen
Naveen

Reputation: 1251

How can I parse the JSON string output from HTTP POST in iOS

I send some parameters using POST method to my server and from there i got my response and i store it in a string "serverResponse" .This is my server response that i stored in string

{ 
    "carIdentifier":[
        {"carId":"91"},
        {"carId":"93"},
        {"carId":"114"},
        {"carId":"117"}
    ]
}

I have separate json parsing code,but i dont know how can i give this as an input to my json parsing codes. Following are my json parsing code

-(void)getcarList:(NSString *)serverResponse
{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSObject *recommendedcarJSON = nil;
        NSData *recommendedcarsData = [NSData dataWithContentsOfURL:[NSURL URLWithString:serverResponse]];
        NSLog(@"%@",recommendedcarsData);
        if(recommendedcarsData)
            recommendedcarJSON = [NSJSONSerialization JSONObjectWithData:recommendedBooksData options:NSJSONReadingMutableContainers error:nil];

        if(!recommendedcarsData){
            NSLog(@"Error with JSON Serialization");
        } else{
            [self saveRecommendedcarData:(NSDictionary *)recommendedcarJSON];
        }
    } );
}

In saveRecommendedcarData method i am creating one plist and saving this parsed data. How can i give my server response string as an input to json parsing ??

Upvotes: 1

Views: 312

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

NSError *jsonError = nil;
id allValues = [NSJSONSerialization JSONObjectWithData:[serverResponse dataUsingEncoding:NSUTF8StringEncoding]
                                               options:0
                                                 error:&jsonError];

if(jsonError!=nil)
    NSLog(@"Json_Err: %@",jsonError);

NSArray *carIdentifier = [(NSDictionary*)allValues objectForKey:@"carIdentifier"];
NSLog(@"%@",carIdentifier);

for(NSDictionary *dict in carIdentifier)
        NSLog(@"carId: %@",[dict valueForKey:@"carId"]);

Try this.

UPDATE

-(void)getcarList:(NSString *)serverResponse
{

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        NSError *jsonError = nil;
        id allValues = [NSJSONSerialization JSONObjectWithData:[serverResponse dataUsingEncoding:NSUTF8StringEncoding]
                                                   options:0
                                                     error:&jsonError];

        if(jsonError!=nil)
            NSLog(@"Json_Err: %@",jsonError);

        NSArray *carIdentifier = [(NSDictionary*)allValues objectForKey:@"carIdentifier"];
        NSLog(@"%@",carIdentifier);

        for(NSDictionary *dict in carIdentifier)
                NSLog(@"carId: %@",[dict valueForKey:@"carId"]);

        } );
}

Upvotes: 2

iPatel
iPatel

Reputation: 47089

If your want to get Value from your date Then Use Following code:

for(i=0 ; i < myDic.count; i++)
{
  NSLog(@"%@",[[myDic objectForKey:@"carIdentifier"] objectAtIndex:i] objectForKey:@"carId"])
}

Upvotes: 0

Related Questions