Accessing the xml values from NSDictionary - ios

I am using this xmlreader. Here is my code
NSDictionary *xmlDict = [XMLReader dictionaryForXMLString:responseString error:&error1];
NSLog(#"XMLData: %#",xmlDict);
I can save and log the data and it looks like this.
response = {
Gpn0 = {
text = 10000;
};
Gsn0 = {
text = 4;
};
btn0 = {
text = up;
};
};
}
But how can I access a single element from this dictionary?

NSDictionary *gpn0 = response[#"Gpn0"];
NSNumber *gpn0_text = gpno[#"text"]; // note this is a numeric value
NSDictionary *btn0 = response[#"btn0"];
NSString *btn0_text = gpno[#"text"]; // note this is a string value
so on and so forth

Related

how to store data in object and that object store in array in ios

How can I store these three dictionaries in an object and those three objects store in an array? how can I retrive this data from array?
[
{
"gender_desc" = Male;
"gender_id" = 1;
"gender_isactive" = 1;
},
{
"gender_desc" = Female;
"gender_id" = 2;
"gender_isactive" = 1;
},
{
"gender_desc" = Other;
"gender_id" = 3;
"gender_isactive" = 1;
}
]
Using this,
NSError *error;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
you can parse your json data into dictionaries.
First of all this json structure is wrong. Json should be like :
{
"gender_detail":[
{
"gender_desc" = Male;
"gender_id" = 1;
"gender_isactive" = 1;
},
{
"gender_desc" = Female;
"gender_id" = 2;
"gender_isactive" = 1;
},
{
"gender_desc" = Other;
"gender_id" = 3;
"gender_isactive" = 1;
}
]
}
Then create a mutable array and store these values :
NSMutableArray *dataArray = [[NSMutableArray alloc] init];
[dataArray addObjectsFromArray:[dictionary valueForKey:#"gender_details"]];
After this array is ready.
Now suppose you want gender_desc value of first object.
NSString *gender_desc = [[dataArray objectAtIndex:0] valueForKey:#"gender_desc"];
Do likewise for other details.
So you want to parse JSON into NSDictionaries, NSArrays and the like? The are many libraries that will happily accommodate this task, or Apple's docs can help you roll your own. Looking on SO, here just one place to start, Convert JSON feed to NSDictionary

Downloading calendar data in Objective-C

I am trying to download google calendar data.
I am following a tutorial this link according to which implementing some of the GoogleOAuth delegate methods will let me get my desired data.
First I converted the response JSON data into an NSDictionary object and after that, I, NSLog this dictionary, to see the way the returned data is formed which is as shown below.
calendarInfoDict is {
etag = "\"1436255371893000\"";
items = (
{
accessRole = owner;
backgroundColor = "#9a9cff";
colorId = 17;
defaultReminders = (
{
method = popup;
minutes = 30;
}
);
etag = "\"1436255371893000\"";
foregroundColor = "#000000";
id = "sabiranthapa#gmail.com";
kind = "calendar#calendarListEntry";
notificationSettings = {
notifications = (
{
method = email;
type = eventCreation;
},
{
method = email;
type = eventChange;
},
{
method = email;
type = eventCancellation;
},
{
method = email;
type = eventResponse;
}
);
};
primary = 1;
selected = 1;
summary = "sabiranthapa#gmail.com";
timeZone = "Asia/Calcutta";
},
{
accessRole = reader;
backgroundColor = "#92e1c0";
colorId = 13;
defaultReminders = (
);
description = "Displays birthdays of people in Google Contacts and optionally \"Your Circles\" from Google+. Also displays anniversary and other event dates from Google Contacts, if applicable.";
etag = "\"1436255358367000\"";
foregroundColor = "#000000";
id = "#contacts#group.v.calendar.google.com";
kind = "calendar#calendarListEntry";
summary = Birthdays;
timeZone = "Asia/Calcutta";
}
);
kind = "calendar#calendarList";
nextSyncToken = 00001436255371893000;
}
According to tutorial there is a block containing a bunch of information regarding every calendar I have created in Google Calendars inside curly bracket. But I am not getting any events that I have saved which can be seen in Google Calendar after signing in gmail but not in my apps where I need to work with it.
My code is
-(void)responseFromServiceWasReceived:(NSString *)responseJSONAsString andResponseJSONAsData:(NSData *)responseJSONAsData{
NSError *error;
if ([responseJSONAsString rangeOfString:#"calendarList"].location != NSNotFound) {
NSDictionary *calendarInfoDict = [NSJSONSerialization JSONObjectWithData:responseJSONAsData options:NSJSONReadingMutableContainers error:&error];
NSLog(#"calendarInfoDict is %#", calendarInfoDict);
if (error) {
NSLog(#"%#", [error localizedDescription]);
}
else{
NSArray *calendarsInfo = [calendarInfoDict objectForKey:#"items"];
if (_arrGoogleCalendars == nil) {
_arrGoogleCalendars = [[NSMutableArray alloc] init];
}
for (int i=0; i<[calendarsInfo count]; i++) {
NSDictionary *currentCalDict = [calendarsInfo objectAtIndex:i];
NSArray *values = [NSArray arrayWithObjects:[currentCalDict objectForKey:#"id"],
[currentCalDict objectForKey:#"summary"],
nil]; NSArray *keys = [NSArray arrayWithObjects:#"id", #"summary", nil];
[_arrGoogleCalendars addObject:
[[NSMutableDictionary alloc] initWithObjects:values forKeys:keys]];
}
_dictCurrentCalendar = [[NSDictionary alloc] initWithDictionary:[_arrGoogleCalendars objectAtIndex:0]];
[_barItemPost setEnabled:YES];
[_barItemRevokeAccess setEnabled:YES];
[self showOrHideActivityIndicatorView];
But I always end up with condition
if (_arrGoogleCalendars == nil) {
_arrGoogleCalendars = [[NSMutableArray alloc] init];
}
without able to access my events.
How can I download (or access) my Events from google calendar?

Weathermap API not able to parse completely in iOS

I'm able to retrieve data from the weather map api, however I can't figure out how to exactly parse the data. I'm able to do it only for a certain part of it.
This is the JSON data:
{
base = "cmc stations";
clouds = {
all = 56;
};
cod = 200;
coord = {
lat = "29.66";
lon = "-82.3";
};
dt = 1403641995;
id = 4156404;
main = {
humidity = 74;
pressure = 1018;
temp = "304.08";
"temp_max" = "306.48";
"temp_min" = "302.15";
};
name = Gainesville;
rain = {
3h = 0;
};
sys = {
country = US;
message = "0.2087";
sunrise = 1403605821;
sunset = 1403656392;
};
weather = (
{
description = "broken clouds";
icon = 04d;
id = 803;
main = Clouds;
}
);
wind = {
deg = 153;
gust = "1.54";
speed = "0.51";
};
}
Now I am able to get only one part of it :
base = "cmc stations"
like this:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSLog(#"values %#",json);
NSLog(#"Checking values ------------ %#",[json objectForKey:#"cloud"]);
}
But when I try to do the same for other fields like
clouds
coord
main
I can't. I get a null value.
I'm guessing I need an additional NSDictionary or NSArray but just not sure how to go about it. Can someone please tell how can I do this? I'm mainly looking to get data from the main block :
humidity
temp
temp_max
temp_min
rain
sunrise
sunset
I think I have found a solution:
Here's how I'm getting the data:
NSString* base = [json objectForKey:#"base"];
NSLog(#"Value of first base variable: %#",base);
// NSArray* base = [json objectAtIndex:0];
NSArray *clouds = [json objectForKey:#"clouds"];
NSLog(#"Value of first clouds‹ variable: %#",clouds);
NSArray *coord = [json objectForKey:#"coord"];
NSLog(#"Value of first coord variable: %#",coord);
NSDictionary *main = [json objectForKey:#"main"];
NSLog(#"Value of first coord variable: %#",main);
NSArray* humidity = [main objectForKey:#"humidity"];
NSLog(#"humidity levels found manually : %#",humidity);
NSArray* temp_max = [main objectForKey:#"temp_max"];
NSLog(#"max temp levels found manually : %#",temp_max);
The problem is that most of those values are dictionaries, not arrays. When you see { } and colons (:) that will generally indicate the presence of a dictionary of key-value pairs, even though some of them may only have one such pair which might make it appear like an array or a stand-alone object.
To get clouds for instance:
NSDictionary *clouds = [json objectForKey:#"clouds"];
NSNumber *allClouds = [clouds objectForKey:#"all"];
NSLog(#"Value of first clouds‹ variable: %#",allClouds);
To get coord:
NSDictionary *coords = [json objectForKey:#"coord"];
NSNumber *lon = [coords objectForKey:#"lon"];
NSNumber *lat = [coords objectForKey:#"lat"];
NSLog(#"Value of lon is: %#",lon);

how to make a new NSDictionary from a NSDictionary where key = x

i have a app that gets some json data from a mysql server via a web api like this:
[[API sharedInstance] commandWithParams:[NSMutableDictionary dictionaryWithObjectsAndKeys:#"streamGames", #"command", nil] onCompletion:^(NSDictionary *json) {
//got stream
[[API sharedInstance] setGames:[json objectForKey:#"result"]];
this gives me :
{
awayScor = 3;
data = "2014-04-11";
gameType = 1;
homeScor = 2;
homeTeam = "Herning Blue Fox";
time = "21:00";
},
{
awayScor = 1;
data = "2014-04-08";
gameType = 2;
homeScor = 2;
homeTeam = "SønderjyskE";
time = "19:00";
},
now what i want to do i make a new NSDictionary where i only add data where lets say gameType = 1 so i have a new NSDictionary for etch gameType.
If I've understood the question correctly!
NSArray *games; // Get this from somewhere
NSMutableDictionary *gameTypes = [NSMutableDictionary dictionary];
for (NSDictionary *game in games) {
NSNumber *gameType = game[#"gameType"];
NSMutableArray *gamesForType = gameTypes[gameType];
if (!gamesForType) {
gamesForType = [NSMutableArray array];
gameTypes[gameType] = gamesForType;
}
[gamesForType addObject:game];
}
Now gameTypes will be a dictionary of game types to an array games of that type.

Parse plist file and get value for key

I have this file below:
I have a question, how can I get url value from this plist file.
I have convert this file to a NSDictionary and it has all keys and values.
I've added this line to check how it works
NSLog(#"%#", [[[[source objectForKey:#"Root"] objectForKey:#"updates"] objectForKey:#"Item 0"] objectForKey:#"url"]);
but it returns (null) instead of someurl1.
source - this is my NSDictionary instance variable.
This log for my NSDictionary object
(NSDictionary *) $1 = 0x0ab38b60 {
plist = {
dict = {
array = {
dict = (
{
integer = 4;
key = (
id,
url,
compression
);
string = "http://someurl1";
true = "";
},
{
integer = 5;
key = (
id,
url,
compression
);
string = "http://someurl2";
true = "";
}
);
};
key = updates;
};
version = "1.0";
};
}
Try:
for (NSDictionary *item in [source objectForKey:#"updates"]) {
NSLog(#"%#", [item objectForKey:#"url"]);
}
Note that 'Root' isn't a key, it's just the way Xcode displays the plist. And you need to loop over an array, again 'Item 0' is just the way it's displayed in the IDE.
Very well.
Try this code.
NSDictionary *rootDictionary = [NSDictionary dictionaryWithContentsOfFile: [[NSBundle mainBundle] pathForResource:#"PListFile" ofType:#"plist"]];
NSLog(#"%#", [[[rootDictionary objectForKey:#"updates"] objectAtIndex:0] objectForKey:#"url"]);

Resources