extracting information from NSDictionary - objective-c

I have a NSDictionary response from a Flickr api request and it's description looks like this:
self.userInfo: {
"_text" = "\n\n";
person = {
"_text" = "\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n\t\n";
description = {
};
iconfarm = 0;
iconserver = 0;
id = "83943196#N02";
ispro = 0;
location = {
};
"mbox_sha1sum" = {
"_text" = 7b61c5d24f12345678be82c31234567830540;
};
mobileurl = {
"_text" = "http://m.flickr.com/photostream.gne?id=12345678";
};
nsid = "12345678#N02";
"path_alias" = "";
photos = {
"_text" = "\n\t\t\n\t\t\n\t\t\n\t\t\n\t";
count = {
"_text" = 2;
};
firstdate = {
"_text" = 12345678;
};
firstdatetaken = {
"_text" = "2012-08-01 12:46:38";
};
views = {
"_text" = 0;
};
};
photosurl = {
"_text" = "http://www.flickr.com/photos/12345678#N02/";
};
profileurl = {
"_text" = "http://www.flickr.com/people/12345678#N02/";
};
realname = {
"_text" = "Me TheUser";
};
timezone = {
label = "Pacific Time (US & Canada); Tijuana";
offset = "-08:00";
};
username = {
"_text" = metheuser;
};
};
stat = ok;
}
I'm having truoble figuring out how to extract the realname value into a NSString.
I've tried:
NSString * temp = [self.userInfo valueForKey:#"realname"];
NSLog (#"FL: nameOfSignedInUser. nameself.userInfo: %#", temp.debugDescription);
but that comes back as nil.
thank you!

NSString *name = [self.userInfo valueForKeyPath:#"person.realname._text"];

NSString *realName = [[[self.userInfo objectForKey:#"person"] objectForKey:#"realname"] objectForKey:#"_text"];

Related

how can i parse and foreach json tree on objective c

2015-10-13 16:42:55.391 JsonProject[30384:1471391]
requestReply: (
{
DNumb = 512421421;
DTempData = "";
DUUID = 12;
Id = 1;
},
{
DTempData = "";
Id = 2;
},
{
DTempData = "";
Id = 3;
},
{
DTempData = "";
Id = 4;
},
{
DTempData = "";
Id = 5;
},
{
DTempData = "";
Id = 6;
},
{
DTempData = "";
Id = 7;
},
{
DNumb = String;
DTempData = "";
DUUID = String;
Id = 8;
},
{
DNumb = 1234;
DTempData = "";
DUUID = 4567;
Id = 9;
},
{
DTempData = "";
Id = 10;
},
{
DTempData = "";
Id = 11;
},
{
DTempData = "";
Id = 12;
},
{
DNumb = Message;
DTempData = "";
DUUID = isNotReal;
Id = 13;
} )
This is json data after post method,
NSDictionary* responseA = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(#"requestReply: %#", responseA[#"GetVerifiedDevices"]);
how can i reach members? also check is available any number or character?
i was working on C# and i do use foreach, is there parse this data?
Thanks

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]

My XML response:
{
"S:Envelope" = {
"S:Body" = {
"ns2:getMatchListResponse" = {
item = (
{
answerTime = {
text = 30;
};
challengerAppealsGranted = {
text = "0 of 16";
};
challengerHandle = {
text = manish;
};
challengerMatchesPlayed = {
text = 93;
};
challengerPic = {
text = "image.png";
};
challengerScore = {
text = 0;
};
challengerlosses = {
text = 0;
};
challengerwins = {
text = 4;
};
handle1 = {
text = sahni;
};
handle1AppealsGranted = {
text = 5;
};
handle1MatchesPlayed = {
text = 84;
};
handle1Response = {
text = Pending;
};
handle1Score = {
text = 0;
};
handle1losses = {
text = 0;
};
handle1wins = {
text = 1;
};
handle2MatchesPlayed = {
text = 0;
};
handle2Score = {
text = 0;
};
handle2losses = {
text = 0;
};
handle2wins = {
text = 0;
};
handle3MatchesPlayed = {
text = 0;
};
handle3Score = {
text = 0;
};
handle3losses = {
text = 0;
};
handle3wins = {
text = 0;
};
idMatch = {
text = 750;
};
isAppeal = {
text = false;
};
isFreeForm = {
text = false;
};
isMultichoiceQuestion = {
text = false;
};
isPublic = {
text = false;
};
matchName = {
text = ewtwert;
};
matchStartThresholdTime = {
text = "4 days 7 hrs";
};
matchStatus = {
text = wait;
};
matchType = {
text = Private;
};
noOfPlayers = {
text = 2;
};
priorityInList = {
text = 0;
};
scoreToWin = {
text = 5;
};
timeDuration = {
text = "5 days";
};
turnChangesIn = {
text = 0;
};
},
{
answerTime = {
text = 30;
};
challengerAppealsGranted = {
text = "0 of 16";
};
challengerHandle = {
text = manish;
};
challengerMatchesPlayed = {
text = 93;
};
challengerPic = {
text = "image.png";
};
challengerScore = {
text = 0;
};
challengerlosses = {
text = 0;
};
challengerwins = {
text = 4;
};
handle1MatchesPlayed = {
text = 0;
};
handle1Score = {
text = 0;
};
handle1losses = {
text = 0;
};
handle1wins = {
text = 0;
};
handle2MatchesPlayed = {
text = 0;
};
handle2Score = {
text = 0;
};
handle2losses = {
text = 0;
};
handle2wins = {
text = 0;
};
handle3MatchesPlayed = {
text = 0;
};
handle3Score = {
text = 0;
};
handle3losses = {
text = 0;
};
handle3wins = {
text = 0;
};
idMatch = {
text = 749;
};
isAppeal = {
text = false;
};
isFreeForm = {
text = false;
};
isMultichoiceQuestion = {
text = false;
};
isPublic = {
text = false;
};
matchName = {
text = gfhf;
};
matchStartThresholdTime = {
text = "4 days 6 hrs";
};
matchStatus = {
text = wait;
};
matchType = {
text = Public;
};
noOfPlayers = {
text = 2;
};
priorityInList = {
text = 0;
};
scoreToWin = {
text = 5;
};
timeDuration = {
text = "5 days";
};
turnChangesIn = {
text = 0;
};
}
);
"xmlns:ns2" = "http://services.tgs.com/";
};
};
"xmlns:S" = "http://schemas.xmlsoap.org/soap/envelope/";
};
}
I need value of key "matchStartThresholdTime".
I do:
NSDictionary *dictResult = [XMLReader dictionaryForXMLString:responseString error:nil];
NSDictionary *Enveloper = [dictResult objectForKey:#"S:Envelope"];
NSDictionary *Body = [Enveloper objectForKey:#"S:Body"];
NSDictionary *profileDetails = [Body objectForKey:#"ns2:getMatchListResponse"];
NSMutableArray *items = [profileDetails objectForKey:#"item"];
// NSLog(#"items===>%#",items);
NSDictionary *temp1;
for(temp1 in items)
{
thresholdTime = [NSString stringWithFormat:#"%#",[[temp1 objectForKey:#"matchStartThresholdTime"]objectForKey:#"text"]];...thresholdTime is NSString object.......and this line causes exception
//NSLog(#"time is===>%#",[[temp1 objectForKey:#"matchStartThresholdTime"]objectForKey:#"text"]);
}
What is the problem here?
Temp1 contains an NSString Object and not the NSDictionary, that you expect.
When dealing with JSON it is wise always to (double) check if a received object is really of the type that you expect. Always use
if ([temp1 isKindOfClass:[NSDictionary class]]) ...
or so.

how to get pi_image from array in table view

(
{
property = {
"p_address" = sdfsdf;
"p_asking_price" = "50000.00";
"p_category_type" = 1;
"p_created" = "0000-00-00 00:00:00";
"p_description" = sdfsdf;
"p_id" = 2;
"p_is_new_property" = 1;
"p_modified" = "2013-04-08 15:40:25";
"p_no_of_bedroom" = 1;
"p_no_of_otherroom" = 1;
"p_no_of_wc" = 2;
"p_title" = sadf;
"p_type" = demo;
};
"property_images" = (
);
"property_local_information" = (
{
"li_label" = "Nearby Transport";
"li_order" = 1;
"pli_distance" = "";
"pli_logo" = "";
"pli_title" = "";
}
);
"property_sale_history" = (
{
"psh_history" = sdsdfsd;
},
{
"psh_history" = sdfdfds;
}
);
"property_videos" = (
);
},
{
property = {
"p_address" = dfgsdfg;
"p_asking_price" = "44222.00";
"p_category_type" = "";
"p_created" = "0000-00-00 00:00:00";
"p_description" = "Updated by a staff meber (demo2#gmail.com)";
"p_id" = 3;
"p_is_new_property" = 1;
"p_modified" = "2013-04-09 03:05:09";
"p_no_of_bedroom" = 0;
"p_no_of_otherroom" = 0;
"p_no_of_wc" = 0;
"p_title" = "Test Update";
"p_type" = sdfgsdfgsdg;
};
"property_images" = (
{
"pi_image" = "prop_img_1594785781_3.png";
}
);
"property_local_information" = (
{
"li_label" = "Nearby College";
"li_order" = 4;
"pli_distance" = utyutyu;
"pli_logo" = "";
"pli_title" = tyutyu;
}
);
"property_sale_history" = (
{
"psh_history" = testing;
},
{
"psh_history" = tyutyutut;
}
);
"property_videos" = (
{
"pv_video" = "prop_vid_730342365_3.flv";
}
);
},
Try this if your using json concept
NSString *urlString = [NSString stringWithFormat:"your url link"];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSMutableArray *json = (NSMutableArray*)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSMutableArray *piimage=[json valueForKey:#"pi_image"];
NSLog(#"%#",piimage);

Parsing Poorly formed JSON in objective-C

My task is to parse some JSON which was created by running an XML to JSON conversion tool on an XML WADL. I'm able to parse the data returned in the response into JSON and I'm able to obtain the top level NSDictionary from the data. I can get an array from the dictionary and an NSDictionary from the Array ( it includes the key value but thats it) and thats as deep as I can go. I've done parsing in the past but this particular JSON format I am having no luck? Here is the code I'm using..
// Decode the data
NSError *parseError = nil;
NSData *jsonData = [request responseData];
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&parseError];
if (parseError) {
NSLog(#"Error: %#", [parseError localizedDescription]);
// other stuff..
return;
}
NSLog(#"JSON=: %#", responseDict.description);
NSArray *messageBoxResponse = [responseDict objectForKey:#"messageBoxResponse"];
NSLog(#"messageBoxResponse=: %#", messageBoxResponse);
NSUInteger count = messageBoxResponse.count;
for (NSDictionary* messages in messageBoxResponse) {
NSLog(#"Messages=: %#", messages.description);
NSArray *message = [messages objectForKey:#"message"];
// get SIGABRT
count = message.count;
// If I comment out above line and introduce this I get SIGABRT also
for (NSDictionary* something in message) {
NSLog(#"Somthing=: %#", something.description);
}
Here is the JSON I'm attempting to parse.. IPs have been changed :):
{
messageBoxResponse = {
messages = {
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages";
rel = self;
};
message = (
{
flags = {
answered = 0;
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<4F3131A3.6040204#sc-msg1-msg.sc.sc>/flags";
rel = self;
};
taggedForDeletion = 0;
unread = 0;
};
from = "Suren.1#sc-msg1-msg.sc.sc";
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<4F3131A3.6040204#sc-msg1-msg.sc.sc>";
rel = self;
};
parts = {
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<4F3131A3.6040204#sc-msg1-msg.sc.sc>/parts";
rel = self;
};
part = {
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<4F3131A3.6040204#sc-msg1-msg.sc.sc>/parts/0";
rel = self;
};
mimeType = "TEXT/PLAIN";
name = "text.txt";
size = 564;
};
};
receivedDate = 1328624061000;
sentDate = 1328624035000;
subject = test;
},
{
flags = {
answered = 0;
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/flags";
rel = self;
};
taggedForDeletion = 0;
unread = 0;
};
from = "Suren.1#sc-msg1-msg.sc.sc";
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>";
rel = self;
};
parts = {
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/parts";
rel = self;
};
part = (
{
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/parts/0";
rel = self;
};
mimeType = "TEXT/PLAIN";
name = "text.txt";
size = 6;
},
{
content = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/parts/1/2c3a3400620f218d5378607260dc2749.wav";
rel = content;
};
duration = 3;
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/parts/1";
rel = self;
};
mimeType = "AUDIO/WAV";
name = "Audio_Recording_S000551_002.wav";
size = 31190;
},
{
link = {
href = "http://1.1.1.1:80/services/messageboxes/63358/messages/<24346739.35.1328562602976.JavaMail.mango#sc-msg1-msg.sc.sc>/parts/2";
rel = self;
};
mimeType = "APPLICATION/MS-TNEF";
name = "winmail.dat";
size = 656;
}
);
};
receivedDate = 1328562604000;
sentDate = 1328562596000;
subject = "Voice Message from Suren 1 (63357)";
}
);
};
};
}
Its because the json is not valid. It should look more like this:
{
"messages": {
"link": {
"href": "http: //1.1.1.1: 80/services/messageboxes/63358/messages",
"rel": "self"
}
}
}
the '=' need to be ':' there are semicolons that shouldn't be there.

How best to traverse API information with iOS

Is there any easier way of traversing array/dictionaries without creating a lot of separate NSArrays/NSDictionaries? I know you can traverse nested dictionaries with dot notation and value at keypath, but what about when arrays are involved?
For example:
At the moment if I want to get at the object at feed.entry.link[4].href in the API result below, I have to define an array at keypath "feed.entry", then assign its first entry as a dictionary, then define an array at keypath "link" and access its fourth entry as a dictionary, and then access its value at "href".
Is this normal?
received {
encoding = "UTF-8";
feed = {
entry = (
{
author = (
{
name = {
"$t" = swdestiny;
};
uri = {
"$t" = "https://gdata.youtube.com/feeds/api/users/swdestiny";
};
}
);
category = (
{
scheme = "http://schemas.google.com/g/2005#kind";
term = "http://gdata.youtube.com/schemas/2007#video";
},
{
label = Entertainment;
scheme = "http://gdata.youtube.com/schemas/2007/categories.cat";
term = Entertainment;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = Star;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = Wars;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = Episode;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = 3;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = Revenge;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = of;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = the;
},
{
scheme = "http://gdata.youtube.com/schemas/2007/keywords.cat";
term = Sith;
}
);
content = {
"$t" = "sw-destiny.net Trailer for Revenge of the Sith";
type = text;
};
"gd$comments" = {
"gd$feedLink" = {
countHint = 1567;
href = "https://gdata.youtube.com/feeds/api/videos/9kdEsZH5ohc/comments";
rel = "http://gdata.youtube.com/schemas/2007#comments";
};
};
"gd$rating" = {
average = "4.7729683";
max = 5;
min = 1;
numRaters = 1132;
rel = "http://schemas.google.com/g/2005#overall";
};
id = {
"$t" = "http://gdata.youtube.com/feeds/api/videos/9kdEsZH5ohc";
};
link = (
{
href = "https://www.youtube.com/watch?v=9kdEsZH5ohc&feature=youtube_gdata";
rel = alternate;
type = "text/html";
},
{
href = "https://gdata.youtube.com/feeds/api/videos/9kdEsZH5ohc/responses";
rel = "http://gdata.youtube.com/schemas/2007#video.responses";
type = "application/atom+xml";
},
{
href = "https://gdata.youtube.com/feeds/api/videos/9kdEsZH5ohc/related";
rel = "http://gdata.youtube.com/schemas/2007#video.related";
type = "application/atom+xml";
},
{
href = "https://m.youtube.com/details?v=9kdEsZH5ohc";
rel = "http://gdata.youtube.com/schemas/2007#mobile";
type = "text/html";
},
{
href = "https://gdata.youtube.com/feeds/api/videos/9kdEsZH5ohc";
rel = self;
type = "application/atom+xml";
}
);
"media$group" = {
"media$category" = (
{
"$t" = Entertainment;
label = Entertainment;
scheme = "http://gdata.youtube.com/schemas/2007/categories.cat";
}
);
"media$content" = (
{
duration = 151;
expression = full;
isDefault = true;
medium = video;
type = "application/x-shockwave-flash";
url = "https://www.youtube.com/v/9kdEsZH5ohc?version=3&f=videos&app=youtube_gdata";
"yt$format" = 5;
},
{
duration = 151;
expression = full;
medium = video;
type = "video/3gpp";
url = "rtsp://v2.cache4.c.youtube.com/CiILENy73wIaGQkXovmRsURH9hMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
"yt$format" = 1;
},
{
duration = 151;
expression = full;
medium = video;
type = "video/3gpp";
url = "rtsp://v2.cache5.c.youtube.com/CiILENy73wIaGQkXovmRsURH9hMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp";
"yt$format" = 6;
}
);
"media$description" = {
"$t" = "sw-destiny.net Trailer for Revenge of the Sith";
type = plain;
};
"media$keywords" = {
"$t" = "Star, Wars, Episode, 3, Revenge, of, the, Sith";
};
"media$player" = (
{
url = "https://www.youtube.com/watch?v=9kdEsZH5ohc&feature=youtube_gdata_player";
}
);
"media$thumbnail" = (
{
height = 360;
time = "00:01:15.500";
url = "http://i.ytimg.com/vi/9kdEsZH5ohc/0.jpg";
width = 480;
},
{
height = 90;
time = "00:00:37.750";
url = "http://i.ytimg.com/vi/9kdEsZH5ohc/1.jpg";
width = 120;
},
{
height = 90;
time = "00:01:15.500";
url = "http://i.ytimg.com/vi/9kdEsZH5ohc/2.jpg";
width = 120;
},
{
height = 90;
time = "00:01:53.250";
url = "http://i.ytimg.com/vi/9kdEsZH5ohc/3.jpg";
width = 120;
}
);
"media$title" = {
"$t" = "Star Wars Episode 3 Revenge of the Sith Trailer";
type = plain;
};
"yt$duration" = {
seconds = 151;
};
};
published = {
"$t" = "2007-05-23T03:31:54.000Z";
};
title = {
"$t" = "Star Wars Episode 3 Revenge of the Sith Trailer";
type = text;
};
updated = {
"$t" = "2012-02-20T17:14:37.000Z";
};
"yt$statistics" = {
favoriteCount = 763;
viewCount = 796719;
};
}
);
xmlns = "http://www.w3.org/2005/Atom";
"xmlns$gd" = "http://schemas.google.com/g/2005";
"xmlns$media" = "http://search.yahoo.com/mrss/";
"xmlns$yt" = "http://gdata.youtube.com/schemas/2007";
};
version = "1.0";
}