Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Web service response in string Format

I am using Afnetworking Framework in my project but I am getting the response in string format. I want to get the value of "Data" in the response below -

"{\"Result\":\"Success\",\"Data\":\"intro-1898-1449000428650.mp4\"}"

I have used the code below:

[Helper PostWebServiceRequest:kIntrovideo InputParameters:parameters competion:^(BOOL result, NSDictionary *response) {
    if (result){
        NSLog(@"response : %@",response);

        NSString *data=  [NSString stringWithFormat:@"%@",response ];

        NSError *jsonError;
        NSData *objectData = [data dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];
like image 931
Jatin Vashisht Avatar asked Oct 19 '22 11:10

Jatin Vashisht


1 Answers

since your response object seems to be an array and not (as stated) a dictionary, try the following:

NSString *jsonString = ((NSArray *)response).firstObject;
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
NSString *data = jsonObject[@"Data"];
like image 74
André Slotta Avatar answered Oct 22 '22 02:10

André Slotta