How to create an NSDictionary with multiple keys?

I'm not sure what what I'm going to ask is actually an NSDictionary with a few keys, but ok.

What I want to do is create an NSDictionary with the keys and values ​​for my data, and then convert them to JSON . The JSON will look something like this:

 { "eventData": { "eventDate": "Jun 13, 2012 12:00:00 AM", "eventLocation": { "latitude": 43.93838383, "longitude": -3.46 }, "text": "hjhj", "imageData": "raw data", "imageFormat": "JPEG", "expirationTime": 1339538400000 }, "type": "ELDIARIOMONTANES", "title": "accIDENTE" } 

I used only NSDictionaries as follows:

 NSArray *keys = [NSArray arrayWithObjects:@"eventDate", @"eventLocation", @"latitude" nil]; NSArray *objects = [NSArray arrayWithObjects:@"object1", @"object2", @"object3", nil]; dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 

But the above format applies not only to the key value. So my question is, how would NSDictionary be to fit the JSON ? Thank you for reading my post, and I'm sorry if there was any confusion.

+6
source share
5 answers

Do you know that you can have an NSDictionary inside another NSDictonary correctly?

 NSDictionary *eventLocation = [NSDictionary dictionaryWithObjectsAndKeys:@"43.93838383",@"latitude",@"-3.46",@"latitude", nil]; NSMutableDictionary *eventData = [NSDictionary dictionaryWithObjectsAndKeys:eventLocation,@"eventLocation", nil]; [eventData setObject:@"Jun 13, 2012 12:00:00 AM" forKey:@"eventDate"]; [eventData setObject:@"hjhj" forKey:@"text"]; . . . NSMutableDictionary *finalDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:eventData,@"eventData", nil]; [finalDictionary setObject:@"ELDIARIOMONTANES" forKey:@"type"]; [finalDictionary setObject:@"accIDENTE" forKey:@"title"]; 
+33
source

Now with Objective-C literals, there is a much better, simpler and more intuitive way to do this. Here is your exact dictionary with this new syntax:

 NSDictionary *dictionary = @{ @"eventData": @{ @"eventDate": @"Jun 13, 2012 12:00:00 AM", @"eventLocation": @{ @"latitude": @43.93838383, @"longitude": @-3.46 }, @"text": @"hjhj", @"imageData": @"raw data", @"imageFormat": @"JPEG", @"expirationTime": @1339538400000 }, @"type": @"ELDIARIOMONTANES", @"title": @"accIDENTE" }; // Prints: "43.93838383" NSLog(@"%@", dictionary[@"eventData"][@"eventLocation"][@"latitude"]); 
+13
source

How to create NSArray and with access for an object using NSDictionary?

... create NSArray

 NSArray *studentkeys = [NSArray arrayWithObjects:@"studentName", @"studentBirthDate", @"studentCity", @"studentMobile" nil]; NSArray *objects = [NSArray arrayWithObjects:@"Pravin", @"27/08/1990", @"Bhavnagar",@"7878007531", nil]; 

... to access an NSArray using NSDictionary

 NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; 
+2
source

Here is the structure:
Your root object is NSMutableDictionary
eventData - the key for the NSMutableDictionary object with keys and objects:
-> key eventDate object NSString
-> key eventLocation object NSMutableDictionary with keys and objects:
----> latitude key NSNumber object
----> longitude key NSNumber object
-> key text object NSString
-> key imageData object NSString later converted to NSData
-> key imageFormat object NSString
-> key expirationTime object NSNumber
type key for an NSString object
title key for an NSString object

+1
source

If you want several categories, you can follow this format

 NSDictionary *jsonObject = @{ @"data1":@[ @{ @"title":@"A" @"subData" : @[ @{ @"title":@"aa" }] } ] }; 
0
source

Source: https://habr.com/ru/post/919021/


All Articles