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 }
Related
I want to change the Dictionary's all value to String, how to do with it?
Such as:
{ #"a":"a",
#"b":2,
#"c":{
#"c1":3,
#"c2":4
}
}
I want convert to :
{ #"a":"a",
#"b":"2",
#"c":{
#"c1":"3",
#"c2":"4"
}
}
How to do with it? I think all the day.
If I use below method to traverse the dictionary values:
NSArray *valueList = [dictionary allValues];
for (NSString * value in valueList) {
// change the value to String
}
If the value is a dictionary, how about it?
So, someone can help with that?
You could do this with a recursive method, it changes all NSNumber values to NSString and calls itself for nested dictionaries. Since a dictionary cannot be mutated while being enumerated a new dictionary is created and populated:
- (void)changeValuesOf:(NSDictionary *)dictionary result:(NSMutableDictionary *)result
{
for (NSString *key in dictionary) {
id value = dictionary[key];
if ([value isKindOfClass: [NSDictionary class]]) {
NSMutableDictionary * subDict = [NSMutableDictionary dictionary];
result[key] = subDict;
[self changeValuesOf:value result:subDict];
} else if ([value isKindOfClass: [NSNumber class]]) {
result[key] = [NSString stringWithFormat:#"%#", value];
} else {
result[key] = value;
}
}
}
NSDictionary *dictionary = #{#"a": #"a", # "b":#2, #"c": #{#"c1": #3, #"c2":#4 }};
NSMutableDictionary *result = [NSMutableDictionary dictionary];
[self changeValuesOf:dictionary result:result];
NSLog(#"%#", result);
You can create category for a dictionary and add method some like stringValueForKey:.
The realisation can be something like this:
- (NSString)stringValueForKey:(NSString*)key
{
id value = self[key];
if( [value respondsToSelector:#selector(stringValue)])
return [value performSelector:#selector(stringValue)]
return nil;
}
I have 100 key and value in nsmutabledictornary and i want to check that any value have null or not. Do you have any short function or technique?
I don't want to multiple line code like check every key and value. Your answer would be appreciated.
This code will give you the set of keys which have (non)null values. You can't store actual nil values in a dictionary, so [NSNull null] is assumed. The predicate is trivially alterable to any other condition.
NSDictionary *d = #{ #"a" : #"1", #"b" : [NSNull null] };
NSSet *nullKeys = [d keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
return [d[key] isKindOfClass:[NSNull class]];
}];
NSSet *nonnullKeys = [d keysOfEntriesPassingTest:^BOOL(NSString *key, id obj, BOOL *stop) {
return [d[key] isKindOfClass:[NSNull class]] == NO;
}];
From here, you can use the keys to generate a corresponding dictionary, if needed.
NSMutableDictionary *nonNullDict = [NSMutableDictionary dictionary];
[d enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if ([nonnullKeys contains:key]) {
nonNullDict[key] = obj;
}
}];
If you don't need a separate list of keys, and just need the filtered dictionary, skip the first step and modify the second part to read as follows:
NSMutableDictionary *nonNullDict = [NSMutableDictionary dictionary];
[d enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
if ([obj isKindOfClass:[NSNull null]] == NO) {
nonNullDict[key] = obj;
}
}];
Write category on NSDictionary it will provide you null free dictionary. Here is the category I have written for myself.
code for .h file (interface)
#import <Foundation/Foundation.h>
#interface NSDictionary (CheckNull)
{
}
- (NSDictionary *)nullFreeDictionary;
#end
Code for .m file. (implementation)
#import "NSDictionary+CheckNull.h"
#implementation NSDictionary (CheckNull)
- (NSDictionary *) nullFreeDictionary
{
NSMutableDictionary *tempDictionary = [self mutableCopy];
for (NSString *key in tempDictionary.allKeys) {
NSString *value = [tempDictionary valueForKey:key];
if ([value isKindOfClass:[NSString class]]) {
if (value == (id)[NSNull null] || value == nil || value.length == 0) {
[tempDictionary setValue:#"" forKey:key];
}
}
}
return tempDictionary;
}
Call null free method on your dictionary using above category.
NSDictionary *dict = [dict nullFreeDictionary];
//To remove NULL from Dictionary
-(NSMutableDictionary *)removeNullFromDictionary : (NSMutableDictionary *)dict
{
// if (![dict isKindOfClass:[NSMutableDictionary class]])
// {
// }
dict = [[NSMutableDictionary alloc] initWithDictionary:dict];
for (NSString * key in [dict allKeys])
{
if ([dict[key] isKindOfClass:[NSNull class]])
{
[dict setValue:#"" forKey:key];
}
else if ([dict[key] isKindOfClass:[NSMutableDictionary class]]||[dict[key] isKindOfClass:[NSDictionary class]])
{
dict[key] = [self removeNullFromDictionary:[NSMutableDictionary dictionaryWithDictionary:dict[key]]];
}
else if ([dict[key] isKindOfClass:[NSMutableArray class]]||[dict[key] isKindOfClass:[NSArray class]])
{
dict[key] = [self removeNullFromArray:[NSMutableArray arrayWithArray:dict[key]]];
}
}
return dict;
}
//To remove NULL from Array
-(NSMutableArray *)removeNullFromArray : (NSMutableArray *)arr
{
// if (![arr respondsToSelector:#selector(addObject:)])
// {
// arr = [[NSMutableArray alloc] initWithArray:arr];
// }
arr = [[NSMutableArray alloc] initWithArray:arr];
for (int cnt = 0; cnt<[arr count]; cnt++)
{
if ([arr[cnt] isKindOfClass:[NSNull class]])
{
arr[cnt] = #"";
}
else if ([arr[cnt] isKindOfClass:[NSMutableDictionary class]]||[arr[cnt] isKindOfClass:[NSDictionary class]])
{
arr[cnt] = [self removeNullFromDictionary:[NSMutableDictionary dictionaryWithDictionary:arr[cnt]]];
}
else if ([arr[cnt] isKindOfClass:[NSMutableArray class]]||[arr[cnt] isKindOfClass:[NSArray class]])
{
arr[cnt] = [self removeNullFromArray:[NSMutableArray arrayWithArray:arr[cnt]]];
}
}
return arr;
}
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;
}
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! :)
NSDictionary *tokenData = [[responseDict objectForKey:#"data"] objectForKey:#"multipass"];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *card = [NSString stringWithFormat:#"%#-%#",
[[UserAccount sharedInstance] cardNumber],
[tokenData objectForKey:#"CardPhoneToken"]]; //<-- problem
});
I've been fighting with this line of code since yesterday.I'm geting a JSON feed from the server nad My problem is that one time out of ten tokenData returns with 0 instead of a Dictionary and I can't figure out why. When it returns as 0 it is also treated as a boolean by the compiler (can't figure out why either) and i'm getting the error -[NSCFBoolean objectForKey:]: unrecognized selector sent to instance 0x81aa20
I've tried doing something like this
if(!tokenData)
{
NSLog(#"This is going to crash");
} But when tokenData returns 0 the if isn't getting called.
esponseDict JSON I'm getting :
{ code = 200; data = {
multipass = {
CardPhoneBarcodeToken = 562431;
CardPhoneToken = 23221;
errorMessage = "";
operationResult = 0;
};
};
}
This is how it looks right before it crashes: { code = 200; data = { multipass = 0; }; }
Update: I've managed to take action if tokenData is not NSDictionary now I'm facing another problem with the else section. How can I reload the tokenData if it's not a NSDictionary ? I tried calling *tokenData = [[responseDict objectForKey:#"data"] objectForKey:#"multipass"]; inside the else but I'm keep getting tokenData = 0
Thanks
I would check if tokenData is a NSDictionary before parsing it.
-(void)loadData
{
NSDictionary *tokenData = [[responseDict objectForKey:#"data"] objectForKey:#"multipass"];
dispatch_async(dispatch_get_main_queue(), ^{
NSString *cardPhoneToken = #"";
if( [tokenData isKindOfClass:[NSDictionary class]] ){
retryCount = 0;
cardPhoneToken = [tokenData objectForKey:#"CardPhoneToken"];
} else {
NSLog(#"failed to load data, retrying...");
retryCount++;
if( retryCount < 5 ){
[self loadData];
return;
} else {
NSLog(#"failed after 5 retries");
}
}
NSString *card = [NSString stringWithFormat:#"%#-%#",
[[UserAccount sharedInstance] cardNumber],
cardPhoneToken];
});
}
Sounds familiar. :)
Change the block to:
^{
NSString *card;
if ([tokenData isKindOfClass [NSDictionary class]) {
card = [NSString stringWithFormat:#"%#-%#",
[[UserAccount sharedInstance] cardNumber],
[tokenData objectForKey:#"CardPhoneToken"]]; // <<-- No Problem anymore
} else {
// deal with the situation. Probably do:
card = [NSString stringWithFormat:"%#-no phone number given", [UserAccount sharedInstance] cardNumber]];
}
Alternative:
^{
id cardPhoneToken;
if ([tokenData]) {
cardPhoneToken = [tokenData objectForKey:#"CardPhoneToken"];
} else {
cardPhoneToken = #"no Phone Token"; // Or what ever you think is appropriate
}
NSString *card = [NSString stringWithFormat:#"%#-%#",
[[UserAccount sharedInstance] cardNumber],
cardPhoneToken]; // <<-- No Problem anymore
}