0

I am using AFNetworking framework in my app, I am able to make HTTP request and able to get response from server but i am not able to parse the JSON.

The following is my code:

I have created a singleton class called WebServices and have created a method, which makes HTTP request.

+(void)getCompaniesc:(NSString *)companyID onSucess:(PSACompletionBlock2)onSucess
{
    NSDictionary *params = @{@"companyId": companyID};

    [[[WebServices sharedInstance] operationQueue] cancelAllOperations];

    [[WebServices sharedInstance] postPath:@"GetCompany?" parameters:params success:^(AFHTTPRequestOperation *operation, id response)
     {
         NSString *st = [NSString stringWithFormat:@"%@",response];
         NSLog(@"st=%@",st);

         if (onSucess)
         {
             onSucess(YES, response);
         }


     }failure:^(AFHTTPRequestOperation *operation, NSError *error){

         NSLog(@"%s: AFHTTPRequestOperation error: %@", __FUNCTION__, error);
     }];
}

and from my ViewController I am calling the above function using the following code:

[WebServicesClass getCompaniesc:companyID onSucess:^(BOOL success, id jsonresponse) {

}];

I'm getting the response from the server which is type id.

And the following is my response from Server:

[{"ExistsInDB":"False","CanSave":"True","EntityName":"ACCOUNT","TypeDescription":"Company","TypePluralDescription":"Companies","RequiredProperties":"Chiever.Data.SAQueryFieldSet","MetaData":"","ReadOnly":"False","ACCTNAME":"","AREA_ID": "","ACCT_TYPE_ID":"","ADDR1":"","ADDR2":"","ADDR3":"","TOWN”:””,”COUNTY":"","POSTCODE":"","COUNTRY":"","TEL":"","FAX":"","EMAILORWEB":"","BUYGRP_ID": "","STATUS":"","SIC_CODE_ID”:””,”CURRENCY_ID":"","CALL_FREQ": "0","DORMANT":"False","CREATOR_ID": "","CREATED_ON":"01/01/0001 00:00:00","LAST_EDITOR_ID":"","LAST_EDITED":"01/01/0001 00:00:00","LAST_ACTION_BY":"","LAST_ACTION":"01/01/0001 00:00:00","NEXT_ACTION_BY":"","NEXT_ACTION":"01/01/0001 00:00:00","LOCALE_ID":"","BusinessUnits": "System.Collections.Generic.List`1[chiever.Platform.LookupIdValuePair]","ACCT_ID":"0000000320"}]

Can any one help me out with this?

6
  • Please format your code. Do you get an error / exception? Does the server return correct headers? Have you told AFNetworking to expect JSON? Commented Feb 11, 2014 at 12:05
  • i have set the headers: [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"application/json"]]; Commented Feb 11, 2014 at 12:32
  • Which version of AFNetworking are you using ? Commented Feb 11, 2014 at 12:43
  • 1.0 because the app should support iOS5 and above Commented Feb 11, 2014 at 12:50
  • So how are you "not able to parse" the JSON? Have you tried using a JSON parser?? Commented Feb 11, 2014 at 13:06

3 Answers 3

1

Just do this check:

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData: jsonresponse options:NSJSONReadingMutableContainers error:&error];

if your return is an array, use this:

NSArray *arr = [NSJSONSerialization JSONObjectWithData: jsonresponse options:NSJSONReadingMutableContainers error:&error];
Sign up to request clarification or add additional context in comments.

6 Comments

Sorry @Nilesh.S.Joshi, I fix my answer. Please, try again!!
m getting null as NSDictionary
The outermost layer of the JSON is an array (not that that would cause a null return value). @Nilesh.S.Joshi - What is the return value in error?
by using this NSDictionary* json=(NSDictionary*)response; i am getting the data in NSDictionary* json. in this format: <227b5c72 5c6e2020 5c224578 69737473 496e4442 5c223a20 5c224661 6c73655c 222c5c72 5c6e2020 5c224361 6e536176 655c223a 205c2254 7275655c>
@Nilesh.S.Joshi - That appears to be an NSData object. It would need to be parsed with NSJSONSerialization to obtain the NSArray containing the JSON data.
|
1

A.) If you're using AFHTTPRequestOperation make sure you're setting the responseSerializer:

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];
operation.responseSerializer = [AFJSONParserResponseSerializer serializer];

Also, make sure that you're setting request "Content-Type" to be "text/json". Now, if you're getting JSON response from the server, then you probably should get a dictionary responseObject.

B.) If you're still getting an NSData, you can convert the NSData to NSString to see/debug what you're getting from the server:

NSString *stringResponse = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // Use appropriate encoding
NSLog(@"String Response: %@", stringResponse);

C.) If you're sure that you're getting json response from the server, you can always convert the raw NSData response to NSDictionary using json serialization:

id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"JSON: %@", json);

Hope this helps.

Comments

0

If your server returns the "Content-Type: application/json" header, you will get a NSDictionary with the json response from AFNetworking. Or in your case, a NSArray with NSDictionaries included.

In PHP, the headers can be modified by adding

header('Content-Type: application/json');

to the top of your script.

Comments

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.