Directly accessing nested dictionary values in Objective-C - iphone

Is there a way to directly access an inner-array of an an outer array in Objective-C? For example, a call to an external data source returns the following object:
{
bio = "this is the profile.bio data";
"first_name" = John;
"last_name" = Doe;
location = {
name = "Any Town, Any State";
};
metadata = {
pictures = {
picture = "https://picture.mysite.com/picture.jpeg";
}
}
}
I want to be able to access, for example, the location.name or the metadata.pictures.picture data. Dot notation, however, does not seem to work. For example:
_gfbLocation = [result objectForKey:#"location.name"];
_gfbPicture = [result objectForKey:#"metadata.pictures.picture"];
The only way I have been able to access this data is by first setting the contents of the inner arrays to objects. Thoughts?

For nested keys like that you can use a keyPath. A keyPath is just a series of keys joined with dots. You can use them to retrieve nested values from objects that support Key-Value Coding - including NSDictionary objects like yours. So in your case this should work:
[result valueForKeyPath:#"location.name"];
For more detail on Key-Value Coding, see Apple's Key-Value Coding Programming Guide.
See also this related StackOverflow question.

Using the correct answer by Simon Whitaker, I was able to build a hierarchy of constants by embedding a Dictionary in a Dictionary in a Dictionary. Below is example source code, modified from my real source code.
This is a real-world problem-solution. In my particular case, the goal was organizing the strings that identify products accessed via StoreKit for In-App Purchase in Apple's App Store for iOS. Imagine our app presents content from a pair of books, one on cats, the other dogs. Furthermore, our app sells an abridged version of the content as well as unabridged. Upgrading from the abridged to the unabridged means a third product, "upgrade". Each pair of books might be translated, in this case English and Italian.
Looking at the strings I'm trying to track, you might think "Why doesn't that guy just use the strings themselves rather than going through this KVC nonsense?". Well, notice the 2nd string, English > Cats > Unabridged. The string ends with an appended underscore. That's because when I used iTunesConnect to create the In-App Purchase products, I accidentally created that item as "Consumable" instead of "Non-Consumable". Apple does not allow changing the ID, even if you delete said product. So the original string could not be used; alternatively, I appended the underscore as a workaround. So the point is, these strings are arbitrary and messy.
Another similar need for this approach would by if these string values might occasionally change at compile-time, so you don't want to be copy-pasting into more than one place in your source-code. A hierarchy of constants, in other words.
Inside Xcode, I want a better way of referring to these product identifiers.
// Using new literals syntax in later versions of Xcode 4 (& 5) to declare and populate a dictionary nested in a dictionary also in a dictionary.
NSDictionary *productIdentifiersHierarchy = #{
#"en" : #{
#"cats" : #{
#"abridged" : #"com.example.My_App.cats_abridged_en",
#"unabridged" : #"com.example.My_App.cats_unabridged_en_",
#"upgrade" : #"com.example.My_App.cats_upgrade_en"
},
#"dogs" : #{
#"abridged" : #"com.example.My_App.dogs_abridged_en",
#"unabridged" : #"com.example.My_App.dogs_unabridged_en",
#"upgrade" : #"com.example.My_App.dogs_upgrade_en"
}
},
#"it" : #{
#"cats" : #{
#"abridged" : #"com.example.My_App.cats_abridged_it",
#"unabridged" : #"com.example.My_App.cats_unabridged_it",
#"upgrade" : #"com.example.My_App.cats_upgrade_it"
},
#"dogs" : #{
#"abridged" : #"com.example.My_App.dogs_abridged_it",
#"unabridged" : #"com.example.My_App.dogs_unabridged_it",
#"upgrade" : #"com.example.My_App.dogs_upgrade_it"
}
}
};
Here's how to access these triple-nested dictionaries.
// Use KVC (Key-Value Coding) as a convenient way to access the nested dictionary structure.
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.cats.abridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.cats.unabridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.cats.upgrade"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.dogs.abridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.dogs.unabridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"en.dogs.upgrade"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.cats.abridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.cats.unabridged"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.cats.upgrade"],
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.dogs.abridged"] );
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.dogs.unabridged"] );
NSLog( [productIdentifiersHierarchy valueForKeyPath:#"it.dogs.upgrade"] );

gfbPicture = [[[result objectForKey:#"metadata"] objectForKey:#"pictures"] objectForKey:#"picture"];

Related

Present JSON string in sorted order from NSDictionary

I made one JSON string from the NSDictionary. The JSON string which I created doesn't come present the items in the order I entered keys and value pair in the NSDictionary .
Now I need all keys and value pairs returned as JSON string in alphabetic order. I tried lot of ways but not getting sorting of JSON string in alphabetic order.
Here's an example of the way the final string is being presented:
{
"SubscriberID" : "603",
"Amount" : "1",
"MerchantID" : "100012",
"Channel" : "Wallet",
"RequestCode" : "0331",
"PosID" : "0465F35F5577CUST",
"TID" : "0000014",
"Stan" : "NA"
}
How do I ensure the items are presented the way I entered them? Or how can I specify the items are alphabetically sorted?
SBJson is able to do this, set sortKeys to YES on SBJsonWriter
http://superloopy.io/json-framework/
SBJsonWriter *writer = [[SBJsonWriter alloc] init];
writer.sortKeys = YES;
// Turn on humanReadable if you also need it pretty-printed
// writer.humanReadable = YES;
NSData *result = [writer dataWithObject:myDictionary];
Like many key-value storage classes, NSDictionary does not guarantee order of the elements you've added. It is a mistake to assume that the key/value pairs, or keyset, will be returned with any particular order.
Further, JSON objects are unordered in the same fashion. You should not care about the order in which the objects are added. Nor should you (or your recipient) rely on being provided a JSON object with 'sorted' keys, as that's not really a valid concept with these particular structures. While ordering the keys might result in an iterator traversing the keys in the order you expect, it's not a guarantee, and should not be relied on.
I think you should revisit why you need the keys sorted in the first place, and see if you can find a way to avoid a dependency on their alphabetical ordering.
Edit: You mention the server requires an SHA hash of the JSON string. If you must, you can create a sorted JSON string by sorting the keys in an NSMutableArray, then creating the JSON string from those keys.
NSMutableArray *sortedKeys = [NSMutableArray arrayWithArray:[myDict allKeys]];
[sortedKeys sortedArrayUsingSelector:#selector(caseInsensitiveCompare:)];
NSMutableString *jsonString = [[NSMutableString alloc] init];
[jsonString appendString:#"{"];
for (NSString *key in sortedKeys) {
[jsonString appendFormat:#"\"%#\"", key];
[jsonString appendString:#":"];
[jsonString appendFormat:#"\"%#\"", [myDict objectForKey:key]];
[jsonString appendString:#","];
}
if ([jsonString length] > 2) {
[jsonString deleteCharactersInRange:NSMakeRange([jsonString length] - 1, 1)];
}
[jsonString appendString:#"}"];
This code hasn't been tested, so you might need to play with it a bit. Thought it would be much better if you could find a JSON library to do this for you, although you might not have as much control over the key ordering.
You can't do it (or at least you can't rely on that). JSON objects are unordered sets as you can read on JSON.org.
Actually,the system method [NSString stringWithFormat:] is what you need.So here is the easy way:
NSDictionary* inputDictionary = #{
#"SubscriberID" : #"603",
#"Amount" : #"1",
#"MerchantID" : #"100012",
#"Channel" : #"Wallet",
#"RequestCode" : #"0331",
#"PosID" : #"0465F35F5577CUST",
#"TID" : #"0000014",
#"Stan" : #"NA"
};
NSString* sortedJsonStr = [NSString stringWithFormat:#"%#", inputDictionary];
NSLog(#"sorted json string is %#", sortedJsonStr);
The result is:
{
Amount = 1;
Channel = Wallet;
MerchantID = 100012;
PosID = 0465F35F5577CUST;
RequestCode = 0331;
Stan = NA;
SubscriberID = 603;
TID = 0000014;
}
The question does not make sense, and therefore cannot be answered in its current form.
If you encode some JSON as {"keyB":"valueB","keyA":"valueA"}, take it's checksum, and then transmit the encoded JSON and the checksum to the remote site, the remote site has little choice but to take the checksum of the JSON as received and compare that. To do the checksum on sorted values it would have to decode the received JSON string into an NSDictionary, re-encode into "sorted" JSON, and then take the checksum of the reconstituted JSON, and that would be a lot of extra effort for no reason.
Far more likely is that there is some difference in either the way the checksum is being computed (padding of the SHA256 input, eg) or some difference in the JSON strings being used -- code page differences, escaped characters, one end is doing it on Base64 and the other not, etc.

NSDictionary id key value

I recently try to change the Json library in my applicaiton from SBJson to the NSJSONSerialization.
When I do this job, I find there are some key value that I can not get out.
Here is an example of the NSDictionary I get after NSJSONSerialization:
{
id = 4028;
"novel_author" = "XYZ";
"novel_pub" = "ABC";
"novel_title" = "DATE LIVE";
updatedate = "2013-01-13 22:31:13";
"vol_click" = 7563;
}
The original Json data string is:
{
"id":"4028",
"vol_click":"7563",
"updatedate":"2013-01-13 22:31:13",
"novel_author":"XYZ",
"novel_pub":"ABC",
"novel_title":"DATE LIVE"
}
I can not get the value of the key "id" out.
[NSDictionary objectForKey#"id"] is useless.
Is there anyone have idea how to get the value out?
As you can tell by the output of what looks like NSLog("%#", dict);, the JSON deserialization process works fine.
The dictionary contains a key called "id", so [dict objectForKey:#"id"] should also work fine.
I can only conclude that this isn't the actual cause of the trouble you're having.
Either of these should work:
[yourDictName valueForKey:#"id"]
[yourDictName objectForKey:#"id"]
If you can see the value using NSLog to display the dictionary, then it is there. Beyond that, make sure your object is not getting nil'd or released or freed.

Why does SBJson JSON parsing only get the last key of interest?

I am using the following JSON: http://www.kb.dk/tekst/mobil/aabningstider_en.json
When I try to parse it by the key "location" as such:
// get response in the form of a utf-8 encoded json string
NSString *jsonString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
// get most parent node from json string
NSDictionary *json = [jsonString JSONValue];
// get key-path from jason up to the point of json object
NSDictionary *locations = [json objectForKey:#"location"];
NSLog( #"%#", locations );
// iterate through all of the location objects in the json
for (NSDictionary *loc in locations )
{
// pull library name from the json object
NSString *name = [loc valueForKey:#"name"];
// add library data table arrays respectively
[ libraryNames addObject: ( ( name == nil | name.length > 0 ) ? name : #"UnNamed" ) ];
}
When I print the the object locations via NSLog:
{
address = "Universitetsparken 4, 3. etage, 2100 K\U00f8benhavn \U00d8";
desc = "";
lastUpdated = "";
latlng = "55.703124,12.559596";
link = "http://www.farma.ku.dk/index.php?id=3742";
name = "Faculty of Pharmaceutical Sciences Library";
parts = {
part = {
hour = {
day = "5.June Constitution Day (Denmark)";
open = Closed;
};
hours = {
hour = {
day = Friday;
open = "10-16";
};
};
name = main;
};
};
}
Which is only the last value for the "location" keys. Am I doing something wrong?
I tried validating the JSON via http://jsonlint.com/, however when I'd put in the JSON URL as above, it said "valid" - still only the last "locations" key was shown", however if I copy-paste it, it will not validate the JSON, and has to be fixed by removing new-lines from the string.
Also, when i try to parse the JSON and get the "name" fields, I get the following exception:
2012-05-08 15:37:04.941 iPhone App Tabbed[563:f803] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x68bfe70> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(0x13dc052 0x156dd0a 0x13dbf11 0x9d2f0e 0x941841 0x940ca9 0x4593 0xf964e 0x114b89 0x1149bd 0x112f8a 0x112e2f 0x1148f4 0x13ddec9 0x365c2 0x3655a 0x25b569 0x13ddec9 0x365c2 0x3655a 0xdbb76 0xdc03f 0xdbbab 0x25dd1f 0x13ddec9 0x365c2 0x3655a 0xdbb76 0xdc03f 0xdb2fe 0x5ba30 0x5bc56 0x42384 0x35aa9 0x12c6fa9 0x13b01c5 0x1315022 0x131390a 0x1312db4 0x1312ccb 0x12c5879 0x12c593e 0x33a9b 0x281d 0x2785)
terminate called throwing an exception(lldb)
It would make more sense if the "locations" tag was an array object enclosed by square brackets ([]), however right now it's only an sequence of normal key-value pairs... Sadly, that's the JSON I have to work with.
Please help and thanks a great deal! :)
Sincerely,
Piotr.
The JSON you've got to work with may be valid, but it doesn't make much sense. It has one big dictionary with the location key repeated many times. Most JSON parser will simply return the last value for the repeated key. It would be best if you could change the structure to use an array instead, but if you cannot there's still hope. You can read the stream and stuff the values from the location keys into an array as they come out of it. This is how you'd do that:
#interface BadJsonHelper : NSObject
#property(strong) NSMutableArray *accumulator;
#end
#implementation BadJsonHelper
- (void)parser:(SBJsonStreamParser *)parser foundArray:(NSArray *)array {
// void
}
- (void)parser:(SBJsonStreamParser *)parser foundObject:(NSDictionary *)dict {
[accumulator addObject:dict];
}
#end
You can drop that little helper class at the top of your file, outside the #implementation section of the class where you're doing your work. (There's no need for the #interface and #implementation being in different files.)
In your code, you would use it like this:
BadJsonHelper *helper = [[BadJsonHelper alloc] init];
helper.accumulator = [NSMutableArray array];
SBJsonStreamParserAdapter *adapter = [[SBJsonStreamParserAdapter new] init];
adapter.delegate = helper;
adapter.levelsToSkip = 1;
SBJsonStreamParser *parser = [[SBJsonStreamParser alloc] init];
parser.delegate = adapter;
switch ([parser parse: responseData]) {
case SBJsonStreamParserComplete:
NSLog(#"%#", helper.accumulator);
break;
case SBJsonStreamParserWaitingForData:
NSLog(#"Didn't get all the JSON yet...");
break;
case SBJsonStreamParserError:
NSLog(#"Error: %#", parser.error);
break;
}
This example was originally adapted from the following test:
https://github.com/stig/json-framework/blob/master/Tests/StreamParserIntegrationTest.m
Update: I created a fully functional example project that loads the JSON asynchronously and parses it. This is available from github.
The JSON is valid, however there is a basic problem regarding the definition of the array of items.
Instead of defining an array of locations using brackets, the JSON redefines the same location key/value pair over and over again. In other words JSON initially says the value of location is the collection with name "The Black Diamond", but immediately after it redefines it with the collection with name "Faculty Library of Humanities" and so on till the last location Faculty of Pharmaceutical Sciences Library".
The same is true for parts and hours.
If you can't fix the result of the JSON and you really need to get it working you may want to modify the JSON removing the "location" keys and adding brackets properly.
Edit
Alternatively you may use an NSScanner and process the JSON result manually. Kinda hacky but it will work as long as the JSON format doesn't change significantly.
Edit
This snipped of code should do the work...
NSString *jsonString = [[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
int indx = 1;
for (;;)
{
NSRange locationRange = [jsonString rangeOfString:#"\"location\":"];
if (locationRange.location == NSNotFound) break;
jsonString = [jsonString stringByReplacingCharactersInRange:locationRange
withString:[NSString stringWithFormat:#"\"location%d\":", indx++]];
}
NSDictionary *locations = [json objectForKey:#"location"];
As you can see, the result of JSON parsing by SBJson is a NSDictionary. A dictionary contains key/value pairs, and the keys are unique identifiers for the pairs.
The JSON data you need to handle is valid but not a good one. Per RFC 4627 - 2.2:
An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members). A name is a string. A single colon comes after each name, separating the name from the value. A single comma separates a value from a following name. The names within an object SHOULD be unique.
Things like jQuery can parse the JSON also, but the result is the same as SBJson (the last one as the one). See Do JSON keys need to be unique?.
It is not a MUST, but it's still not a good practice. It would be much easier if you are able to change the structure of the JSON data on the server side (or even on the client side after receiving it) rather than parsing it as is.

iPhone SDK: Problems Parsing JSON

I'm using the SBJSONParser for my iphone app. Up to now, i've been parsing simple json strings such as: ["Business1","Business2"]
I'm now using PHP to get both the business name and business ID from the database within the same json string, so my PHP is giving me a result like this:
{"business_1A" : "ABC_1","businees_2A": "ABC_2" }
Here's the code that i'm currently using to process the first JSON output which works fine:
businessNames is an NSMutableArray in the following code.
NSString *businessNamesJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"businessNamesJSON.php"]]];
SBJsonParser *parser = [[SBJsonParser alloc]init];
businessNames = [[parser objectWithString:businessNamesJSON error:nil]copy];
Basically, I want to split the second JSON output so that I can have two separate NSMutableArrays, one which contains the business Names and the other which holds the IDs.
How do I extract or split the second JSON output so I can do this?
Thanks in advance.
Hy there
Let me take a step back. Since you have a list of companies wouldn't it be a better way to represent your data with an array in json like so:
[
{
"identifier": "ABC_1",
"name": "business_1A"
},
{
"identifier": "ABC_2",
"name": "businees_2A"
}
]
I believe this would make the parsing of the data easier for you and it would allow you to add more attributes in the future.
So once you have this structure you can parse the json data and then loop over the entries and extract the values for the keys identifier and name (in this case) respectively.
{"business_1A" : "ABC_1","businees_2A": "ABC_2" } defined an object in JSON terms, which will be returned by any sane JSON parser as an NSDictionary in Objective-C, being a collection of mappings from one object to another.
You seem then to want all the keys and all the values separately. In that case you can just get them from the NSDictionary:
SBJsonParser *parser = [[SBJsonParser alloc] init];
businessNamesDictionary = [parser objectWithString:businessNamesJSON error:nil];
NSLog(#"names: %#", [businessNamesDictionary allKeys]);
NSLog(#"values: %#", [businessNamesDictionary allValues]);
Take mutableCopys if you want them. Use objectsForKeys:notFoundMarker: if you want to guarantee that the values come out in the same order as the keys — the order of each is explicitly undefined in the documentation so don't rely on whatever order you happen to get on whichever version of the OS you happen to test against.

Facebook graph api data inconsistent application data IOS

In my app i'm using a users feeds to retrieve al the post done by my app. I simply retrieve al the posts, and the compare on every post the id number of the app.
This work ok. But i've found a bug in this method. Since the application node isn't always consistent. Normally when there is a post which is not done by an app, the entry in the dictionary just says (null), there isn't any data. This doesn't give any problems.
But there is an app which has other data in the this application node. This one has data in this node which specifically says (note the difference between () and <> ). But I can't seem any way to check if the dictionary with that post has in it. i've tried the following:
NSDictionary *resultPost1 =[resultPost objectForKey:#"application"];
NSLog(#"result%#", [resultPost objectForKey:#"application"]);
if ([resultPost1 count] != 0) {
This one gives a sigabrt, with the following nslog before the sigabrt:
result(null)
result{
id = 1957711133323244365557378;
name = "app";
}
result< null > (added space for visibility)
I've also tried isEqualtoString:#"< null>" Also without success.
It looks like sometimes, their is an dictionary in the application node, and sometimes a string .
Anyone has clue??? Thanks!!!
You will have to do some checking as you don't have a guarantee as to what sort of object is returned from the dictionary.
NSDictionary *resultPost1 = [resultPost objectForKey:#"application"];
if ([[resultPost1 class] isKindOfClass:[NSDictionary class]) {
//Treat as a dictionary
}
else if ([[resultPost1 class] isKindOfClass:[NSString class]) {
//Treat as a string
}
else if ([resultPost1 isEqual:[NSNull null] || !resultPost) {
//Treat as Null, note the json library Facebook uses might set
//a json NULL into a NSNull object instead of nil
}