Accessing an array with nested objects and displaying keys values - ios

I have this data set here: https://gist.github.com/ryancoughlin/8043604 - If you see tide.tideSummary it contains an array but inside, it contains multiple dictionaries. I am trying access and display tide.tideSummary.date.pretty and display (all 33 of them) in some type of table (but that I have working with dummy data).
I was going through a somewhat similar question here: Keypath for first element in embedded NSArray
Having some trouble finding a solution to access those nested values. Been through plenty of tutorials and posts here on StackOverflow that deal with very basic JSON strings and those work great for me.
UPDATE:
Came across this question: Parsing nested JSON objects with JSON Framework for Objective-C
In my case, I have the following:
NSArray *prettyDate = [[tide objectForKey:#"tideSummary"] valueForKey:#"date"];
prettyDate prints this from NSLog
Pretty date array: (
{
epoch = 1388388109;
hour = 02;
mday = 30;
min = 21;
mon = 12;
pretty = "2:21 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
},
{
epoch = 1388397506;
hour = 04;
mday = 30;
min = 58;
mon = 12;
pretty = "4:58 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
},
{
epoch = 1388405656;
hour = 07;
mday = 30;
min = 14;
mon = 12;
pretty = "7:14 AM EST on December 30, 2013";
tzname = "America/New_York";
year = 2013;
}
Then I would be able to loop through each, grab the object and display?
Would I do something like:
Grab parent item tideSummary - array
Grab the item above and store date - dictionary
Access pretty via objectForKey
I have this initWithDict
-(id)initWithDict:(NSDictionary *)json {
self = [super init];
if(self) {
NSDictionary *tide = [json valueForKeyPath:#"tide"];
NSArray *arrOfTideSummaryStats = [json valueForKeyPath:#"tide.tideSummaryStats"];
NSDictionary *dctOfTideSummaryStats = [arrOfTideSummaryStats objectAtIndex:0];
NSArray *arrOfTideSummary = [json valueForKeyPath:#"tide.tideSummary"];
// Loop through the date then...
// Loop and grab 'pretty'
// The "first" is the from the example link in my question above. Experimental oonly
id pretty = [arrOfTideSummary valueForKeyPath: #"#first.tideSummary.date.pretty"];
// This all works below
self.maxheight = [NSString stringWithFormat:#"%#", [dctOfTideSummaryStats valueForKey: #"maxheight"]];
self.minheight = [NSString stringWithFormat:#"%#", [dctOfTideSummaryStats valueForKey: #"minheight"]];
/*============================================================*/
NSArray *arrOfTideInfo = [json valueForKeyPath:#"tide.tideInfo"];
NSDictionary *dctOfTideInfo = [arrOfTideInfo objectAtIndex:0];
self.tideSite = [dctOfTideInfo valueForKey:#"tideSite"];
}
return self;
}
Does anyone have any examples or a direction to steer me in? Would love a resource or question to work off of.

-(void)parseJson:(id)json{
NSDictionary *tide = [json objectForKey:#"tide"];
NSArray *tideSummary = [tide objectForKey:#"tideSummary"];
for (int i = 0; i < tideSummary.count; i++) {
NSDictionary *eachTideSummary = [tideSummary objectAtIndex:i];
NSDictionary *dateDic = [eachTideSummary objectForKey:#"date"];
NSDictionary *utcdateDic = [eachTideSummary objectForKey:#"utcdate"];
NSLog(#"Preety from date dictionary: %#", [dateDic objectForKey:#"pretty"]);
NSLog(#"Preety from utcdate dictionary: %#", [utcdateDic objectForKey:#"pretty"]);
}
}

You can take a look at Initialize NSArray with data from NSdictionary as I have already answered this
Hope this helps.

If I'm understanding what you are trying to do, you should be able to replace this part:
// Loop through the date then...
// Loop and grab 'pretty'
with something like:
for (NSDictionary *tideSummary in arrOfTideSummary) {
NSDictionary *date = [tideSummary valueForKey:#"date"];
NSLog(#"Pretty date: %#", [date valueForKey:#"pretty"]);
}
If you want the date values in a table of some sort, then you could store them into a new array or some other data structure and then create a table view that uses that as its data source.

I have below data on JSON link...
{
"results":[
{
"address_components": [
{
"long_name": "Satadhar",
"short_name": "Satadhar",
"types":[
"point_of_interest",
"establishment"
]
} ]
]
I fetched data through ASIHTTPREQUEST so the code is below given
-(void)requestFinished:(ASIHTTPRequest *)request {
NSError *err;
NSData *data=[request responseData];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];
arr=[[dict valueForKey:#"results"]valueForKey:#"address_components"]; //to fetch the data till inside Starting point i.e address_component
_arrTable=[[NSMutableArray alloc]init]; //take a array to store the data of the index 0 [index 0 store the whole data that is start from [array] ]
_arrTable =[arr objectAtIndex:0]; //get the data of 0th index
NSLog(#" whole data%#",_arrTable);//print it u will get the idea
NSLog(#" my %#",[[arr objectAtIndex:0] valueForKey:#"long_name"]);
NSLog(#" my %#",[[arr objectAtIndex:0] valueForKey:#"short_name"]);
[self.tblview reloadData];
}

Related

How to get Date Value from array?

I want to store the date in a separate array…..How to do this???Am getting confused….Pls help me….
(
{
"2015-03-27" = {
amount = 13086;
date = "2015-03-26 18:30:00 +0000";
"date-day" = 27;
"date-month" = Mar;
"date-year" = 2015;
units = 2160;
};
},
{
"2015-08-03" = {
amount = 18300;
date = "2015-08-02 18:30:00 +0000";
"date-day" = 03;
"date-month" = Aug;
"date-year" = 2015;
units = 2950;
};
},
)
Your data is in Array (which is collection of Dictionary)
So, first get Dictionary from Array & then add key of particular dictionary in to new Array (which is resulted array of Dates)
Try this:
NSArray *arrData;
NSMutableArray *arrDates = [NSMutableArray array];
for (NSDictionary *dict in arrData) {
[arrDates addObject:[[dict allKeys] firstObject]];
}
NSLog(#"Dates :: %#", arrDates);
The outer object is an array, item 0 contains the two dictionaries.
NSDictionary *dictionary = array[0];
Then enumerate the dictionary and extract the date values
NSMutableArray *dates = [NSMutableArray array];
for (NSString *key in dictionary) {
[dates addObject: dictionary[key][#"date"]];
}

Retrieving keys from NSDictionary

I'm having a hard time managing to pull anything out of a JSON response when querying TMDb. Despite spending most of the weekend searching these forums and others, I am no nearer a solution.
In my header file I have:
#property (nonatomic,strong) NSDictionary * fetchedData;
The dictionary is populating correctly, since the first part of the JSON response looks like this when I use:
NSLog(#"fetchedData: %#", fetchedData);
fetchedData: {
page = 1;
results = (
{
adult = 0;
id = 1245;
"known_for" = (
{
adult = 0;
"backdroppath" = "/hbn46fQaRmlpBuUrEiFqv0GDL6Y.jpg";
"genreids" = ( 878, 28, 12 );
id = 24428;
"mediatype" = movie;
"original_language" = en;
"original_title" = "The Avengers";
I've been trying numerous ways to retrieve all instances of "original_title" (or any of the keys) in the response, but I have no hair left to pull out when every attempt is returning NULL, so any suggestions are welcome!
Try this.
NSString *title = fetchedData[#"results"][0][#"known_for"][0][#"original_title"];
Getting data from Response
I think fetchedData is dictionary.So
NSString *strOriginalTitle = [NSString stringWithFormat:#"%#",[[[[[fetchedData valueForKey:#"results"]objectAtIndex:0]valueForKey:#"known_for"]objectAtIndex:0]valueForKey:#"original_title"];
//OR By getting data step by step
NSArray *arrayResults = [fetchedData valueForKey:#"results"];
NSDictionary *dict = [[arrayResults objectAtIndex:0] copy];
NSArray *arrayKnownFor = [dict valueForKey#"known_for"] copy];
NSString *strOriginalTitle = [arrayKnownFor objectAtIndex:0]valueForKey:#"original_title"];
NSLog(#"The original_title is - %#",strOriginalTitle);

iOS comparing NSDictionary values

I'm fetching two JSONs and store them individually as NSArray and NSDictionary.
This is what the JSON data structures looks like
(
{
Cells = (
{
ValueName = "name";
Text = "John Appleseed";
},
{
"Option": {
"Text": "19"
},
ValueName = "age"
}, etc...
)
},
{
Cells = (
{
ValueName = "name";
Text = "John Appleseed";
},
{
ValueName = "age",
"Option": {
"Text": "19"
}
}, etc...
)
}
)
{
"map": {
"first": "name",
"second": "age"
}, etc...
}
The two has values that needs to be cross referenced (as one contains all data (allData), and the other contains a "map" (mapData) of what data is needed).
How may I compare all the values of allData with all the values of mapData while, if possible, keeping simplicity and memory in mind?
A great approach would be to use containsObject matching [allData allValues] with [mapData allValues] and being given back an NSArray with the object of the match. Alas, this is not the case. The approach perfectly illustrates what I wish to accomplish.
My initial approach is slow, memory hogging and ugly (wrote this from memory):
NSArray *allData = [NSJSONSerialization JSONObjectWithData:allDataJSON options:kNilOptions error:&JSONSerializationError];
NSDictionary *mapData = [NSJSONSerialization JSONObjectWithData:mapDataJSON options:kNilOptions error:&JSONSerializationError];
NSMutableDictionary *resultData = [[NSMutableDictionary alloc] init];
for (int count = 0; count<allData.count; count++) {
id dataValue = [allData objectAtIndex:count];
for (id key in mapData) {
if ([[mapData objectForKey:key] containsObject:dataValue]) {
[resultData setObject:[dataValue objectForKey:#"value"] forKey:key]
}
}
}
It's also important to be able to reference to each match of the values that match (as done in my approach).
This is not an opinion based question as optimisation and memory usage is not an opinion.
I found that using this data model is memory efficient and pretty fast (clocked in at an average time of 0.001 sec with allData's length of 1000 lines).
By extracting all wanted values from mapData and placing them in an NSArray the values can easily be matched with the values of allData.
When looping thru the mapDataArray a secondary loop can be created for going thru allData's values. If mapDataArray containsObject returns true, the data can be stored in a momentary storage dictionary.
This allows for easy management of what data is needed and what can be ignored. It is important that the momentary storage is created inside of the first loop (as you'll see in a moment).
If and when all the values from the mapDataArray is found (by checking mapDataArray.count with momentaryData.count) we can break the current loop (thus saving time and memory).
It's probably easier to just look at the code to understand what is really going on.
The data objects as referenced in the question.
NSArray *allData = [NSJSONSerialization JSONObjectWithData:allDataJSON options:kNilOptions error:&JSONSerializationError];
NSDictionary *mapData = [NSJSONSerialization JSONObjectWithData:mapDataJSON options:kNilOptions error:&JSONSerializationError];
‏
Creating the mapDataArray, looping thru it, creating the momentary dictionary, appending found objects and appending the momentary data to resultData.
NSArray *mapDataArray = [[NSArray alloc] init];
mapDataArray = [[mapDataArray arrayByAddingObjectsFromArray:[[mapData objectForKey:#"map"] objectForKey:#"first"]] arrayByAddingObjectsFromArray:[[mapData objectForKey:#"map"] objectForKey:#"second"]];
mapDataArray = [[NSOrderedSet orderedSetWithArray:mapDataArray] array]; //Remove duplicates
NSMutableDictionary *resultData = [[NSMutableDictionary alloc] init];
for (int count = 0; count<allData.count; count++) {
NSMutableDictionary *momentaryData = [[NSMutableDictionary alloc] init];
int dataNumber = count;
NSArray *literal = [[allData objectAtIndex:dataNumber] objectForKey:#"Cells"];
for (int count = 0; count<literal.count; count++) {
id literal = [[[allData objectAtIndex:dataNumber] objectForKey:#"Cells"] objectAtIndex:count];
if ([mapDataArray containsObject:[literal objectForKey:#"ValueName"]]) {
id value = [literal objectForKey:#"Text"];
if (!value) {
value = [literal objectForKey:#"Option"];
if (value && ![value isEqual:[NSNull null]]) {
value = [value objectForKey:#"Text"];
} else {
value = #"";
}
}
[momentaryData setObject:[NSString stringWithFormat:#"%#", value] forKey:[literal objectForKey:#"ValueName"]];
if (mapDataArray.count == momentaryData.count) {
break; //We have what we came for...
}
}
}
[resultData setObject:momentaryData forKey:[NSString stringWithFormat:#"%d", dataNumber]];
}
You can compare arrays or dictionaries using isEqual:.
For arrays it compares whether the arrays have the same number of elements or if the matching elements are equal.
For dictionaries it checks if both have the same number of keys, if set of keys is equal and if each key in one dictionary has the same value as the matching key in the other dictionary.

How to get JSON data parse from Arrays of dictionary iOS

list = ({
clouds = 24;
speed = "4.31";
temp = {
day = "283.84";
eve = "283.84";
night = "283.84";
};
}),
Please can anyone tell me what am I doing wrong - I want to display list-->temp-->day value in table first I am trying to get data in an array which is terminating.
Here is my code am I doing any wrong
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:dataBuffer options:-1 error:nil];
NSLog(#"%#",json);
NSMutableDictionary * list = [json objectForKey:#"list"];
NSMutableArray *arrays = [[NSMutableArray alloc]initWithCapacity:0];
for (NSDictionary *lists in [list allValues]) {
[arrays addObject:[list valueForKey:#"temp"]];
}
If you want to access day then use below line,
NSString *day = [json valueForKeyPath:#"list.temp.day"];
Your list is an array, so if you want to do your things without changing much, you can replace:
NSMutableDictionary * list = [json objectForKey:#"list"];
With:
NSMutableDictionary * list = [[json objectForKey:#"list"] objectAtIndex:0];

ios sort a mutable dictionary order [duplicate]

This question already has answers here:
NSDictionary with ordered keys
(9 answers)
Closed 8 years ago.
I saw many examples on SO but I'm not sure if it applies to this situation. Everywhere it says NSMutableDictionries are not guaranteed an order...but I'm getting my data from the server...so my NSMuteDic looks like this:
{
joe = (
{
fromName = joe;
id = 25;
theMessage = "this is going to be a really big message...";
timeAdded = "2014-04-07 21:08:12";
toName = "me";
},
{
fromName = joe;
id = 10;
theMessage = "why???";
timeAdded = "2014-04-05 20:10:04";
toName = "me";
}
);
bob = (
{
fromName = "me";
id = 24;
theMessage = "blah blah";
timeAdded = "2014-04-06 21:15:06";
toName = bob;
},
{
fromName = bob;
id = 22;
theMessage = message;
timeAdded = "2014-04-06 20:11:57";
toName = "me";
}
);
//more entries here
}
What I want to do is change the order...put bob first and joe second. Is this really impossible to do? I saw many very complex solutions...there's no easy way to do this with just a for loop?
cellForRowAtIndexPath:
NSMutableArray *temp = [myDict objectForKey:keys[indexPath.row]];
cell.Message.text = [[reverse lastObject] valueForKey:#"theMessage"];
cell.dateTime.text = [[reverse lastObject] valueForKey:#"timeAdded"];
return cell;
This is how I'm using it...and when a row is selected I pass the array to the next view controller. The reason why I want to reorder is if a new message will be inserted in the pushed view controller, I want that dictionary to be first in the list so the root view controller can be reordered.
NSArray *keys = [myDict allKeys];
[myDict removeObjectForKey:to];
NSMutableDictionary *temp = [NSMutableDictionary dictionaryWithCapacity:[myDict.count];
temp = myDict;
[myDict removeAllObjects];
[myDict setObject:currentDict forKey:to];
for (int i=0; i<temp.count; i++) {
[myDict setObject:[temp valueForKey:keys[i]] forKey:keys[i]];
}
That's not working because it looks like since myDict is a NSObject, temp gets changed every time myDict changes...from the looks of it the logic should work but it isn't...
NSMutableDictionary is not ordered. There is nothing you can do to guarantee the order of keys because NSDictionary makes no attempt to preserve any particular ordering. To go over a dictionary in a particular order, you have to make an array of the keys that is in the order you want, then iterate that and fetch the corresponding values.
// Get the keys
NSArray *keys = [myDict allKeys];
// Sort the keys
NSArray *sortedArray = [NSArray arrayWithArray:[keys sortedArrayUsingComparator:^(NSString* a, NSString* b) {
return [a compare:b];
}]];
// Iterate the dictionary
for (NSUInteger n = 0 ; < [sortedArray count]; n++) {
id value = [myDict objectForKey:[sortedArray objectAtIndex:n]];
}

Resources