Reputation: 567
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?
Upvotes: 0
Views: 1181
Reputation: 20564
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.
Upvotes: 1
Reputation: 1808
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];
Upvotes: 1
Reputation: 1465
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.
Upvotes: 0