In My code I want to convert NSData to NSDictionary but it returns nil I don't know what mistake I made,I Used NSJSONSerialization for convert data to dictionary, The NSData was received from server response.
Here I show my Full code what I am trying.
-(void)SendPushNotification:(NSString*)getUrl :(NSMutableDictionary *)getData withCompletionBlock:(void(^)(NSDictionary *))completionBlock
{
NSError *error;
NSLog(#"dict val: %#",getData);
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:getData options:NSJSONWritingPrettyPrinted error:&error];// Pass 0 if you don't care about the readability of the generated string
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *postData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLengthas = [NSString stringWithFormat:#"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:getUrl]
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:100.0];
NSString *chkRegDevice= [[NSUserDefaults standardUserDefaults] stringForKey:#"bearer"];
NSString *strfds=[NSString stringWithFormat:#"bearer %#",chkRegDevice];
[request setHTTPMethod:#"POST"];
[request setValue:postLengthas forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setValue:strfds forHTTPHeaderField:#"Authorization"];
[request setHTTPBody:postData];
NSURLSessionConfiguration *configg=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession*sessionn=[NSURLSession sessionWithConfiguration:configg delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *taskk=[sessionn dataTaskWithRequest:request completionHandler:^(NSData *data,NSURLResponse *responce,NSError *error){
if(error)
{
NSLog(#"%#", [error localizedDescription]);
completionBlock(nil);
}else{
NSError *jsonError;
NSString *clientDetail = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"clientDetail: %#", clientDetail);
NSData *objectDataaaaa = [clientDetail dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectDataaaaa options:NSJSONReadingMutableContainers error:&jsonError];
NSLog(#"json %#",json);
if (![clientDetail isEqualToString:#"Object reference not set to an instance of an object."]) {
if (completionBlock) {
completionBlock(json);
}
}
else
{
completionBlock(nil);
}
}
}];
[taskk resume];
}
Here the following response I get to convert NSData to NSString.
"{\"multicast_id\":8856529321585625357,\"success\":1,\"failure\":0,\"canonical_ids\":0,\"results\":[{\"message_id\":\"0:1534479035021563%1dbdaa031dbdaa03\"}]}"
Pass NSData object(data) directly to JSONObjectWithData.
Also, to check the error, you can print jsonError.
Try the following code:
NSError* error;
NSData *objectDataaaaa = [clientDetail dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:objectDataaaaa
options:kNilOptions
error:&error];
NSLog(#"JSON DICT: %#", json);
Try this.
NSString* str = your string data;
NSData* data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *decodeString = [[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding];
NSDictionary *dict = [self dictionaryWithJsonString:decodeString];
/////////////////////
- (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
options:NSJSONReadingMutableContainers
error:&err];
if(err) {
return nil;
}
return dic;
}
I am having problem in using the Microsoft Emotion API i have read the documentation but not able to use it. Whenever i use the below code it gives Bad body JSON parsing error. I am not able to detect whats the problem in code.
i have created a method
- (IBAction)clickToGenerateEmotion:(id)sender
NSString* path = #"https://api.projectoxford.ai/emotion/v1.0/recognize";
NSArray* array = #[
#"entities=true",
];
NSString* string = [array componentsJoinedByString:#"&"];
path = [path stringByAppendingFormat:#"?%#", string];
NSLog(#"%#", path);
UIImage *yourImage= _image;
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSMutableURLRequest* _request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:path]];
[_request setHTTPMethod:#"POST"];
[_request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[_request setValue:#"9b118d1587ce40899598b48a6c29b51a" forHTTPHeaderField:#"Ocp-Apim-Subscription-Key"];
NSDictionary *params = #{#"\"url\"" : #"\"http://engineering.unl.edu/images/staff/Kayla_Person-small.jpg\""};
NSMutableArray *pairs = [[NSMutableArray alloc] initWithCapacity:0];
for (NSString *key in params) {
[pairs addObject:[NSString stringWithFormat:#"%#=%#", key, params[key]]];
}
NSString *requestParams = [pairs componentsJoinedByString:#"&"];
[_request setHTTPBody:imageData];
NSURLResponse *response = nil;
NSError *error = nil;
NSData* _connectionData = [NSURLConnection sendSynchronousRequest:_request returningResponse:&response error:&error];
NSLog(#"responseData = %#", [[NSString alloc] initWithData:_connectionData encoding:NSUTF8StringEncoding]);
if (nil != error)
{
NSLog(#"Error: %#", error);
}
else
{
NSError* error = nil;
NSMutableDictionary* json = nil;
NSString* dataString = [[NSString alloc] initWithData:_connectionData encoding:NSUTF8StringEncoding];
NSLog(#"%#", dataString);
if (nil != _connectionData)
{
json = [NSJSONSerialization JSONObjectWithData:_connectionData options:NSJSONReadingMutableContainers error:&error];
}
if (error || !json)
{
NSLog(#"Could not parse loaded json with error:%#", error);
}
NSLog(#"%#", json);
_connectionData = nil;
}
`
Thanks in advance!!!
I have gotten this JSON data back and I would like to parse it into the 3 categories: "guid", "exponent", and "modulus". How would I do that? Thank you for the help in advance!
2015-07-01 11:02:51.972 Acculunk KeyPad[4717:1667358] Response Body:
{"error_code":0,"error_message":"","exponent":"010001","guid":"855fd04f-0016-1805-a3be-84dbef17ffd6","modulus":"C44274FBD65D79B7F9ADF5255A563A5B8B8438D30F8E2CAD16950BE8675827B94F4F8040D4A9563811F405F8E94A20A69DCC0CA590F8731803AB4682497C0DC2520AD2AEB2CC4ED159276335C83B4FB4CB44966448081C625DF88D019118B7448684743EFB6D6704F8F8BD79875ACAEFC541DA3661D0D00BDDF115382A64C5C5","tran_id":"cb2e8149-4961-458a-a6b2-7443bdb01509"}
2015-07-01 11:03:37.175 Acculunk KeyPad[4717:1674710] Terminating since there is no system app.
Here's the code:
NSString *temp2 = [NSString stringWithFormat:#"{\n \"partner_key\": \"%#\",\n \"auth_token\": \"QaU9QcFZ6xE7aiRRBge0wZ4p6E01GEbl\",\n \"payment_account_id\": \"%#\",\n \"card_number\": \"%#\",\n \"card_exp_date\": \"%#\",\n \"amount\": \"%#\",\n \"memo\": \"%#\",\n \"recipient\": {\n \"email\": \"%#\",\n \"mobile_phone\": \"%#\"\n }\n}",[Partner_Key text], [Payment_Account_ID text], [Card_Number text], [Card_Exp_Date text], [Amount text],[Memo text], [Recipient_Email text], [Recipient_Phone_Number text]];
NSLog(temp2);
NSURL *URL = [NSURL URLWithString:#"https://cert.payzur.com/payzurservices.svc/payment/send/initiate"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[temp2 dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(#"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(#"Response HTTP Headers:\n%#\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response Body:\n%#\n", body);
NSData *jsonData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:body options:nil error:&e];
if (res) {
NSNumber *errorCode = res[#"error_code"];
NSString *errorMessage = res[#"error_message"];
NSString *guid = res[#"guid"];
NSString *exponent = res[#"exponent"];
NSString *modulus = res[#"modulus"];
}
else {
NSLog(#"Error: %#", error);
}
}];
[task resume];
Assuming, this data comes as type NSData, you can do the following:
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:apiReturn options:0 error:&myError];
NSNumber *errorCode = res[#"error_code"];
NSString *errorMessage = res[#"error_message"];
NSString *guid = res[#"guid"];
NSString *exponent = res[#"exponent"]; // Maybe also a NSNumber?
NSString *modulus = res[#"modulus"];
The Data will be available in the five variables:
errorCode
errorMessage
guid
exponent
modulus
Use + JSONObjectWithData:options:error: to create a NSDictionary of the JSON.
Then access the elements in the usual manner of accessing dictionary items.
Answer by Christopher Mäuer using the literal syntax:
NSError *error;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:apiReturn options:0 error:&error];
if (res) {
NSNumber *errorCode = res[#"error_code"];
NSString *errorMessage = res[#"error_message"];
NSString *guid = res[#"guid"];
NSString *exponent = res[#"exponent"]; // Maybe also a NSNumber?
NSString *modulus = res[#"modulus"];
}
else {
NSLog(#"Error: %#", error);
}
Updated for new question code:
Here is sample code, I have re-constructed the data received from the log out put in the question:
NSString *responseBody = #"{\"error_code\":0,\"error_message\":\"\",\"exponent\":\"010001\",\"guid\":\"855fd04f-0016-1805-a3be-84dbef17ffd6\",\"modulus\":\"C44274FBD65D79B7F9ADF5255A563A5B8B8438D30F8E2CAD16950BE8675827B94F4F8040D4A9563811F405F8E94A20A69DCC0CA590F8731803AB4682497C0DC2520AD2AEB2CC4ED159276335C83B4FB4CB44966448081C625DF88D019118B7448684743EFB6D6704F8F8BD79875ACAEFC541DA3661D0D00BDDF115382A64C5C5\",\"tran_id\":\"cb2e8149-4961-458a-a6b2-7443bdb01509\"}";
NSData *data = [responseBody dataUsingEncoding:NSUTF8StringEncoding];
// The above was just to get `data` setup.
// The only function of the following two statements is to print the data as a string.
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response Body:\n%#\n", body);
//
// NSData *jsonData = [body dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(#"res: \n%#", res);
if (res) {
NSNumber *errorCode = res[#"error_code"];
NSString *errorMessage = res[#"error_message"];
NSString *guid = res[#"guid"];
NSString *exponent = res[#"exponent"];
NSString *modulus = res[#"modulus"];
NSLog(#"errorCode: %#\nerrorMessage: %#\nguid: %#\nexponent: %#\nmodulus: %#", errorCode, errorMessage, guid, exponent, modulus);
}
else {
NSLog(#"Error: %#", error);
}
Output:
Response Body:
{"error_code":0,"error_message":"","exponent":"010001","guid":"855fd04f-0016-1805-a3be-84dbef17ffd6","modulus":"C44274FBD65D79B7F9ADF5255A563A5B8B8438D30F8E2CAD16950BE8675827B94F4F8040D4A9563811F405F8E94A20A69DCC0CA590F8731803AB4682497C0DC2520AD2AEB2CC4ED159276335C83B4FB4CB44966448081C625DF88D019118B7448684743EFB6D6704F8F8BD79875ACAEFC541DA3661D0D00BDDF115382A64C5C5","tran_id":"cb2e8149-4961-458a-a6b2-7443bdb01509"}
res:
{
"error_code" = 0;
"error_message" = "";
exponent = 010001;
guid = "855fd04f-0016-1805-a3be-84dbef17ffd6";
modulus = C44274FBD65D79B7F9ADF5255A563A5B8B8438D30F8E2CAD16950BE8675827B94F4F8040D4A9563811F405F8E94A20A69DCC0CA590F8731803AB4682497C0DC2520AD2AEB2CC4ED159276335C83B4FB4CB44966448081C625DF88D019118B7448684743EFB6D6704F8F8BD79875ACAEFC541DA3661D0D00BDDF115382A64C5C5;
"tran_id" = "cb2e8149-4961-458a-a6b2-7443bdb01509";
}
errorCode: 0
errorMessage:
guid: 855fd04f-0016-1805-a3be-84dbef17ffd6
exponent: 010001
modulus: C44274FBD65D79B7F9ADF5255A563A5B8B8438D30F8E2CAD16950BE8675827B94F4F8040D4A9563811F405F8E94A20A69DCC0CA590F8731803AB4682497C0DC2520AD2AEB2CC4ED159276335C83B4FB4CB44966448081C625DF88D019118B7448684743EFB6D6704F8F8BD79875ACAEFC541DA3661D0D00BDDF115382A64C5C5
I suggest you replacing the following two lines:
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"Response Body:\n%#\n", body);
With the two code lines I provide:
NSError *error;
NSDictionary* responseData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
or with the following:
NSDictionary *responseData = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)data];
So now you have a NSDictionary, which is responseData, so now we can decode your JSON response as follows (I will put the whole code as follows):
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *responseData = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)data];
NSString *guid = [responseData valueForKey:#"guid"];
NSString *exponent = [responseData valueForKey:#"exponent"];
NSString *modulus = [responseData valueForKey:#"modulus"];
NSLog(#"Decoded Response :\n guide : %#,\n exponent : %#,\n modulus : %#", guid, exponent, modulus);
So your whole code which you have pasted above in your Question will look like following:
NSString *temp2 = [NSString stringWithFormat:#"{\n \"partner_key\": \"%#\",\n \"auth_token\": \"QaU9QcFZ6xE7aiRRBge0wZ4p6E01GEbl\",\n \"payment_account_id\": \"%#\",\n \"card_number\": \"%#\",\n \"card_exp_date\": \"%#\",\n \"amount\": \"%#\",\n \"memo\": \"%#\",\n \"recipient\": {\n \"email\": \"%#\",\n \"mobile_phone\": \"%#\"\n }\n}",[Partner_Key text], [Payment_Account_ID text], [Card_Number text], [Card_Exp_Date text], [Amount text],[Memo text], [Recipient_Email text], [Recipient_Phone_Number text]];
NSLog(temp2);
NSURL *URL = [NSURL URLWithString:#"https://cert.payzur.com/payzurservices.svc/payment/send/initiate"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:#"POST"];
[request setValue:#"application/json" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:[temp2 dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(#"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(#"Response HTTP Headers:\n%#\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSDictionary *responseData = [[NSDictionary alloc] initWithDictionary:(NSDictionary *)data];
NSString *guid = [responseData valueForKey:#"guid"];
NSString *exponent = [responseData valueForKey:#"exponent"];
NSString *modulus = [responseData valueForKey:#"modulus"];
NSLog(#"Decoded Response :\n guide : %#,\n exponent : %#,\n modulus : %#", guid, exponent, modulus);
}];
[task resume];
Well, you didn't say anything about how you got the data back, like if you already have it in a NSString or still in NSData, so I'm going to assume you have it in NSData.
NSData *json <- somehow I magically got jSON data into this
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:json options:kNilOptions error:&error];
NSString guid = [NSString stringWithString:jsonDict[#"guid"];
NSString exponent = [NSString stringWithString:jsonDict[#"exponent"];
NSString modulus = [NSString stringWithString:jsonDict[#"modulus"];
I am fetching data from JSON. It gives response in two dictionaries. How to differentiate that dictionaries and paste the data in table view. I am using segment control in table view. One is for receiver and other is for sender.
NSUserDefaults *uidSave = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *get = [[NSMutableDictionary alloc]init];
[get setObject:[uidSave valueForKey:#"uid"]forKey:#"uid"];
NSLog(#"Dictionary Data which is to be get %#",get);
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:#"r=%#",jsonInputString];
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"%#",caseInfoUrl]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (responseData != nil)
{
jsonArray = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(#"Values =======%#",jsonArray);
sender = [jsonArray objectForKey:#"sender"];
NSLog(#"Sender^^^^%#",sender);
receiver = [jsonArray objectForKey:#"reciever"];
NSLog(#"Reciever$$$$%#",receiver);
}
if (error)
{
NSLog(#"error %#",error.description);
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Server not responding" delegate:self cancelButtonTitle:#"cancel" otherButtonTitles:nil, nil];
[alertView show];
}
// Set up names array
sendArray = [[NSMutableArray alloc]init];
// Loop through our json Array
for (int i = 0 ; i <sender.count; i++)
{
//create object
NSString *dateTime = [[sender objectAtIndex:i]objectForKey:#"datetime"];
NSString *proNumber = [[jsonArray objectAtIndex:i]objectForKey:#"pro_number"];
NSString *statuses = [[jsonArray objectAtIndex:i]objectForKey:#"status"];
[sendArray addObject:[[DataObjects alloc]initWithDate1:dateTime andCaseName1:proNumber andStatus1:statuses]];
}
[sendList reloadData];
}
Define a BOOL variable
BOOL senderSectionIsSelected;
If you want to load sender's data initially then in ViewDidLoad:-
senderSectionIsSelected=YES;
If you want to load receiver's data then set it to NO;
senderSectionIsSelected=NO;
Modify this
if (responseData != nil)
{
jsonArray = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(#"Values =======%#",jsonArray);
sender = [jsonArray objectForKey:#"sender"];
NSLog(#"Sender^^^^%#",sender);
receiver = [jsonArray objectForKey:#"reciever"];
NSLog(#"Reciever$$$$%#",receiver);
[yourTableView reloadData];
}
In your numberOfRow and cellForRow load data accordingly:-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (senderSectionIsSelected)
{
return sender.count;
}
else
{
return receiver.count;
}
}
In cellForRow show data accordingly:-
if (senderSectionIsSelected)
{
yourDataTimeLabel.text=[[sender objectAtInder:indexPath.row]objectForKey:#"datetime"];
yourProNumberLabel.text=[[sender objectAtInder:indexPath.row]objectForKey:#"pro_number"];
yourStatusLabel.text=[[sender objectAtInder:indexPath.row]objectForKey:#"status"];
}
else
{
yourDataTimeLabel.text=[[receiver objectAtInder:indexPath.row]objectForKey:#"datetime"];
yourProNumberLabel.text=[[receiver objectAtInder:indexPath.row]objectForKey:#"pro_number"];
yourStatusLabel.text=[[receiver objectAtInder:indexPath.row]objectForKey:#"status"];
}
Now in your segmentController's action methods:-
If its segment for sender then:-
senderSectionIsSelected=YES;
If its segment for receiver then:-
senderSectionIsSelected=NO;
//don't forget to reload tableView
senderSectionIsSelected=YES;
i have this method to login via json array , it's all ok but it's not entering in
if(json2 == 0)
but in NSLog show me that it's value is 0 , how can i resolve this ? the method is working but it doesent enter in that if.
-(void)loginAPICall
{
NSString *device_name = #"Iphone";
NSString *device_modelname = #"5";
NSString *gcm_id = #"1234567890";
NSString *user = _user.text;
NSString *pass = _pass.text;
// SENDING A POST JSON
NSString *post = [NSString stringWithFormat:#"device_name=%#&device_modelname=%#&gcm_id=%#&username=%#&password=%#", device_name, device_modelname, gcm_id, user, pass];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[[NSURL alloc] initWithString:#"http://192.168.1.110/project/api"]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:postData];
NSURLResponse *requestResponse;
NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil];
NSError *error;
NSMutableDictionary *getJsonData = [NSJSONSerialization
JSONObjectWithData:requestHandler
options:NSJSONReadingMutableContainers
error:&error];
if( error )
{
NSLog(#"%#", [error localizedDescription]);
}
else {
NSArray *json = getJsonData[#"login"];
for ( NSDictionary *jsn in json )
{
NSLog(#"id: %# ", jsn[#"id_user"] );
}
NSArray *json2 = getJsonData[#"status"];
if(json2 == 0){
NSLog(#"Cannot login");
}
NSLog(#"value:%#",json2);
}
}
You are saying that the object return by getJsonData[#"status"]; is an array, but then you are check if with an integer :
NSArray *json2 = getJsonData[#"status"];
if(json2 == 0){
NSLog(#"Cannot login");
}
If you need to check whether the array return by status has values do it like thisL:
NSArray *json2 = getJsonData[#"status"];
if([json2 count] == 0){
NSLog(#"Cannot login");
}
If the object return for the node status is an integer then try this:
NSNumber *json2 = getJsonData[#"status"];
if([json2 integerValue] == 0){
NSLog(#"Cannot login");
}