Querying an NSDictionary for values in an if/else statement - ios

My parse cloud code function is setup to return data in the form of JSON, like so:
response.success
({
"results": [
{ "Number of top categories": top2.length },
{ "Top categories": top2 },
{ "Number of matches": userCategoriesMatchingTop2.length },
{ "User categories that match search": userCategoriesMatchingTop2 }
]
});
What I want to do is query this JSON array in my Objective-C code, and perform certain actions based on what's being returned, through the use of the if statement on the bottom. For example, where it says:
if ([result intValue] == 1){
[self performSegueWithIdentifier:#"ShowMatchCenterSegue" sender:self];
}
I want to replace result intValue with a statement that says the value of "Number of matches" from the JSON data is equal to 1.
- (IBAction)nextButton:(id)sender
{
if (self.itemSearch.text.length > 0) {
[PFCloud callFunctionInBackground:#"eBayCategorySearch"
withParameters:#{#"item": self.itemSearch.text}
block:^(NSString *result, NSError *error) {
NSLog(#"'%#'", result);
NSData *returnedJSONData = result;
NSError *jsonerror = nil;
NSDictionary *categoryData = [NSJSONSerialization
JSONObjectWithData:returnedJSONData
options:0
error:&jsonerror];
if(error) { NSLog(#"JSON was malformed."); }
// validation that it's a dictionary:
if([categoryData isKindOfClass:[NSDictionary class]])
{
NSDictionary *jsonresults = categoryData;
/* proceed with jsonresults */
}
else
{
NSLog(#"JSON dictionary wasn't returned.");
}
if (!error) {
// if 1 match found clear categoryResults and top2 array
if ([result intValue] == 1){
[self performSegueWithIdentifier:#"ShowMatchCenterSegue" sender:self];
}
// if 2 matches found
else if ([result intValue] == 2){
[self performSegueWithIdentifier:#"ShowUserCategoryChooserSegue" sender:self];
//default to selected categories criteria -> send to matchcenter -> clear categoryResults and top2 array
}
// if no matches found, and 1 top category is returned
else if ([result intValue] == 2) {
[self performSegueWithIdentifier:#"ShowCriteriaSegue" sender:self];
}
// if no matches are found, and 2 top categories are returned
else if ([result intValue] == 2) {
[self performSegueWithIdentifier:#"ShowSearchCategoryChooserSegue" sender:self];
}
}
}];
}
}

From what I understand, the response JSON data that you get, is an array of dictionaries within a dictionary.
To retrieve each of those values, you may use the following steps:
Step 1:
Separate the array of dictionaries from the result dictionary into an NSArray object.
NSArray *resultArray = [resultDictionary objectForKey:#"results"];
Step 2:
Now that you have the resultArray, you can extract the values that you want as follows:
Suppose you want the value of NSNumber object "Number of matches",
You know that its the 3rd object in the resultArray, so its index is 2.
NSDictionary *dictionary = [resultArray objectAtIndex:2];
NSNumber *numberOfMatches = [dictionary objectForKey:#"Number of matches"];
Now you can use the [numberOfMatches intValue] wherever you want.
Hope this helps! :)

Related

Handle object of different types

From my backend API, I get a json of objects consisting of array, dictionary, number, bool, string etc. For eg.
{
data:[
{
id : 1,
name : "abcd"
},
{
id : 2,
name : "abcde"
},
{
id : 3,
name : "abcde"
},
]
total_count : 10
}
Sometimes the value in total_count comes as a number and sometimes it comes as a string. In my code I have coded
[lbl setText:[jsonObject valueForKey:#"total_count"]]
This crashes because when the total_count key value is a number. Obviously I can do this
[lbl setText:[NSString stringWithFormat:#"%d",[[jsonObject valueForKey:#"total_count"] intValue]]];
but this happens at a lot of places in the API. A string is coming instead of a bool.
data:false instead of data:[]
[EDIT]
[[AFHTTPRequestOperationManager manager] GET:[URLString attachToken] parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
if([[[responseObject valueForKey:#"response"] valueForKey:#"status"] boolValue]) {
NSLog(#"success");
}
if(success)success(operation, **responseObject**);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
if(failure)failure(operation, error);
if(operation.response.statusCode == 0) {
ATAFNetworkingRequestObject *obj = [[ATAFNetworkingRequestObject alloc] init];
obj.urlString = URLString;
obj.paramters = parameters;
obj.successBlock = success;
obj.failureBlock = failure;
obj.type = ATNetworkingRequestGET;
if(![self duplicateRequestExists:obj])[pendingAPICalls addObject:obj];
}
[self logAPIFailedWithOperation:operation parameters:parameters error:error];
} autoRetry:5 retryInterval:7];
do like after serilization based on your A string is coming instead of a bool. data:false instead of data:[]
if([datajsonObject isKindOfClass:[NSArray class]]){
//Is array
}else if([datajsonObject isKindOfClass:[NSDictionary class]]){
//is dictionary
}else if([datajsonObject isKindOfClass:[NSString class]])
{
//is String
}
else{
//is something else
}
You can check server value is Number or string as this
NSString *newString = [NSString stringWithFormat:#"%#",[[jsonObject valueForKey:#"total_count"]
if ([newString isKindOfClass:[NSNumber class]])
{
NSLog(#"It is number");
}
if ([newString isKindOfClass:[NSString class]])
{
NSLog(#"It is string");
}
Swift code :
lblCount.text = String(datajsonObject["total_count"] as AnyObject)
Objective c :
NSString *strCount = [NSString stringWithFormat:#"%#",[jsonObject valueForKey:#"total_count"]]
if ([strCount isKindOfClass:[NSString class]])
{
// Write your code to show on label
}

Parse JSON array in Objective-C

I have managed to extract the following array (which I am dumping to console) from some json. How can I get and print out the value for one of the elements, i.e. task?
Objective-C:
NSArray *array = [dict objectForKey:#"row"];
NSLog(#"array is: %#",array);
Console output:
array is: {
0 = 1;
1 = "send email";
2 = "with attachment";
ltask = "with attachment";
task = "send email";
userid = 1;
}
array looks like it is actually an NSDictionary, so reference the key to get the value for it.
NSLog(#"Task: %#", array[#"task"]);
the variable array doesn't seem to be NSArray . Does this work for you?
id array = [dict objectForKey:#"row"];
if([array isKindOfClass:[NSDictionary class]]){
NSLog(#"Value of task %#",array[#"task"]);
}
From the log, it looks like the output is an NSDictionary object, so to get the value of task key just do this
NSDictionary *myDict = dict[#"row"];
NSString *task = myDict[#"task"];
NSLog(#"task = %#", task);
if you want to confirm just check the class type using isKindOfClass: method
if([dict[#"row"] isKindOfClass:[NSDictionary class]]) {
NSDictionary *myDict = dict[#"row"];
NSString *task = myDict[#"task"];
NSLog(#"task = %#", task);
} else if([dict[#"row"] isKindOfClass:[NSArray class]]) {
NSArray *myArray = dict[#"row"];
NSDictionary *myDict = myArray[0];
NSString *task = myDict[#"task"];
NSLog(#"task = %#", task);
}
try
if ([[dictionary allKeys] containsObject:#"row"]) {
NSObject *objRow = dictionary[#"row"];
if(objRow){
if([objRow isKindOfClass:[NSArray class]]){
NSArray *arr = (NSArray *)objRow;
....
}
if([objRow isKindOfClass:[NSDictionary class]]){
NSDictionary *dic = (NSDictionary *)objRow;
....
}
}
}

Convert all values of Container into NSString

I have a problem to communicate with a server. The webserver expects all parameters in the JSON object to be a string. So every number and every boolean in every container needs to be a string.
For my example I have a NSDictionary full of key values (values are all kinds of types - numbers, arrays etc.). For example:
{
"AnExampleNumber":7e062fa,
"AnExampleBoolean":0,
"AnExampleArrayOfNumber":[17,4,8]
}
Has to become:
{
"AnExampleNumber":"7e062fa",
"AnExampleBoolean":"0",
"AnExampleArrayOfNumber":["17","4","8"]
}
I tried the standard NSJSONSerializer but it doesn't give me any option to do what I need. I then tried to transform everything in the dictionary manually to be a string but that seems to be overhead. Does anyone have hint for me? Maybe a serializer that does just that or a function to convert any objects in a container to strings?
This is one way you could do it. It's non-optimized and has no error handling. It only supports the kinds of objects that NSJSONSerializer supports.
#import <Foundation/Foundation.h>
#interface NSObject(SPWKStringify)
- (id)spwk_stringify;
#end
#implementation NSObject(SPWKStringify)
- (id)spwk_stringify
{
if ([self isKindOfClass:[NSDictionary class]]) {
NSDictionary *dict = (NSDictionary *)self;
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
for (NSString *key in [dict allKeys]) {
newDict[key] = [dict[key] spwk_stringify];
}
return newDict;
} else if ([self isKindOfClass:[NSArray class]]) {
NSMutableArray *newArray = [[NSMutableArray alloc] init];
for (id value in ((NSArray *)self)) {
[newArray addObject:[value spwk_stringify]];
}
return newArray;
} else if (self == [NSNull null]) {
return #"null"; // representing null as a string doesn't make much sense
} else if ([self isKindOfClass:[NSString class]]) {
return self;
} else if ([self isKindOfClass:[NSNumber class]]) {
return [((NSNumber *)self) stringValue];
}
return nil;
}
#end
int main(int argc, char *argv[]) {
#autoreleasepool {
NSDictionary *dict = #{
#"AnExampleNumber": #1234567,
#"AnExampleBoolean": #NO,
#"AnExampleNull": [NSNull null],
#"AnExampleArrayOfNumber": #[#17, #4, #8],
#"AnExampleDictionary": #{#"innerKey": #[#55, #{#"anotherDict": #[#"foo", #[#1, #2, #"three"]]}]}
};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:[dict spwk_stringify] options:NSJSONWritingPrettyPrinted error:NULL];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"result: %#", jsonString);
}
}
The output will be:
result: {
"AnExampleNumber" : "1234567",
"AnExampleNull" : "null",
"AnExampleDictionary" : {
"innerKey" : [
"55",
{
"anotherDict" : [
"foo",
[
"1",
"2",
"three"
]
]
}
]
},
"AnExampleBoolean" : "0",
"AnExampleArrayOfNumber" : [
"17",
"4",
"8"
]
}
Note: Please keep in mind that turning [NSNull null] into a string doesn't make any sense and might actually be misleading and dangerous.
Enjoy.
(I assume you mean NSJSONSerializer, not NSSerializer.)
I doubt you'll find a pre-rolled solution to this. It's not a general problem. As you note, this is incorrect JSON, so JSON serializers shouldn't do it.
The best solution IMO is just write the code to transform your NSDictionary into another NSDictionary that is in the form you want. If you really want to make it a generic solution, I suspect that a custom NSDictionary walker with isKindOfClass: is your best bet. Something like this should work:
NSDictionary *myStringDictForDict(NSDictionary *dict); // forward decl if needed
NSArray *myStringArrayForArray(NSArray *array) {
NSMutableArray *result = [NSMutableArray new];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if ([obj isKindOfClass:[NSArray class]]) {
[result addObject:myStringArrayForArray(obj)];
} else if ([obj isKindOfClass:[NSDictionary class]]) {
[result addObject:myStringDictForDict(obj)];
} else {
[result addObject:[obj description]];
}
}];
return result;
}
NSDictionary *myStringDictForDict(NSDictionary *dict) {
NSMutableDictionary *result = [NSMutableDictionary new];
[dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([obj isKindOfClass:[NSArray class]]) {
result[key] = myStringArrayForArray(obj);
} else if ([obj isKindOfClass:[NSDictionary class]]) {
result[key] = myStringDictForDict(obj);
} else {
result[key] = [obj description];
}
}];
return result;
}

Error in connectionDidFinishLoading that I can resolve

I have a simple JSON array that is returned from a zip code passed to a third party service.
http://api.geonames.org/findNearbyPostalCodes?postalcode=94115&country=US&radius=5&username=frequentz
I get an unknown error when trying to deserialize the results and I'm not sure what is going wrong.
Here is my connectionDidFinishLoading method, which fires as anticiapated but always fails...and I get the error in the last else if. Ideas?
-(void) connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(#"connectionDidFinishLoading...");
self.zipCodes = [NSMutableArray array];
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingAllowFragments error:&error];
if (jsonObject != nil && error == nil) {
NSLog(#"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(#"Deserialized JSON Dictionary = %#", deserializedDictionary);
for (NSDictionary *item in jsonObject) {
NSString *city = [item objectForKey:#"adminName2"];
NSString *stateAbbreviation = [item objectForKey:#"adminCode1"];
NSString *postalCode = [item objectForKey:#"postalCode"];
NSString *distance = [item objectForKey:#"distance"];
NSString *country = [item objectForKey:#"country"];
NSString *stateName = [item objectForKey:#"stateName"];
ZipCodes *zipCode = [[ZipCodes alloc] initWithName:city stateAbbrev:stateAbbreviation postalCode:postalCode distanceFromGPSZip:distance country:country stateFullName:stateName];
[self.zipCodes addObject:zipCode];
}
}
else if ([jsonObject isKindOfClass:[NSArray class]]){
NSArray *deserializedArray = (NSArray *)jsonObject;
NSLog(#"Deserialized JSON Array = %#", deserializedArray);
}
else {
/* Some other object was returned. We don't know how to deal
with this situation as the deserializer returns only dictionaries or arrays */
}
}
else if (error != nil){
NSLog(#"An error happened while deserializing the JSON data.");
}
}
I think you're using the wrong service --it should be ...findNearbyPostalCodesJSON.... To use the JSON service as far as I can tell from their website. This is their example URL:
http://api.geonames.org/findNearbyPostalCodesJSON?postalcode=8775&country=CH&radius=10&username=demo

ios compare boolean from json

I got a boolean value from a json object using dictionary. at console, I can see 0 or 1.
But my comparision always return true.
BOOL *result =[dictionary valueForKey:#"result"];
if (result == YES) {
[parser release];
}
else {
errorLbl.text = #"Login failed";
}
have you tried adding a boolValue at the end of [dictionary valueForKey:#"result"]boolValue];
You need to convert the value from the dictionary. You can't translate it directly to a BOOL * (BOOL pointer).
Suppose you get a NSString* from your dictionary, you can do:
NSString *result = [dictionary valueForKey:#"result"];
if ([result boolValue]) {
[parser release];
} else {
errorLbl.text = #"Login failed";
}
Assuming you can use boolValue message with your protocol.
Try with the below code, I got
BOOL success = [[jsonDict objectForKey:#"success"] boolValue];
if (success == YES){ //Do some stuff } else{ //Do some stuff }

Resources