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
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.
I have an NSArray created from a JSON file.
If I just Print the Array to the console I get the following output:
Click me.
First part of file:
{
result = {
"attribute_controlled_attached_particles" = (
{
"attach_to_rootbone" = 0;
id = 1;
name = "Partikel 1";
system = "burningplayer_red";
},
{
"attach_to_rootbone" = 0;
id = 2;
name = "Fliegende Teile";
system = "burningplayer_flyingbits";
},
{
"attach_to_rootbone" = 0;
attachment = muzzle1;
id = 3;
name = "Nemesis-Explosion";
system = "particle_nemesis_burst_blue";
},
{
"attach_to_rootbone" = 0;
id = 4;
name = "Community-Glitzer";
system = "community_sparkle";
},
{
"attach_to_rootbone" = 0;
attachment = halopoint1;
id = 5;
name = Heiligenschein;
system = halopoint;
},
{
"attach_to_rootbone" = 0;
id = 6;
name = "Gr\U00fcnes Konfetti";
system = "superrare_confetti_green";
},
{
"attach_to_rootbone" = 0;
id = 7;
name = "Lila Konfetti";
system = "superrare_confetti_purple";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 8;
name = "Spukende Geister";
system = "superrare_ghosts";
},
{
"attach_to_rootbone" = 1;
id = 9;
name = "Gr\U00fcne Energieladung";
system = "superrare_greenenergy";
},
{
"attach_to_rootbone" = 1;
id = 10;
name = "Lila Energieladung";
system = "superrare_purpleenergy";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 11;
name = "Kreisendes TF-Logo";
system = "superrare_circling_tf";
},
{
"attach_to_rootbone" = 1;
id = 12;
name = Fliegenschwarm;
system = "superrare_flies";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 13;
name = "Brennende Flammen";
system = "superrare_burning1";
},
{
"attach_to_rootbone" = 1;
attachment = unusual;
id = 14;
name = "Sengende Flammen";
system = "superrare_burning2";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 15;
name = "Gl\U00fchendes Plasma";
system = "superrare_plasma1";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 16;
name = "Lebendiges Plasma";
system = "superrare_plasma2";
},
{
"attach_to_rootbone" = 1;
id = 17;
name = Sonnenstrahlen;
system = "superrare_beams1";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 18;
name = "Kreisendes Friedenszeichen";
system = "superrare_circling_peacesign";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 19;
name = "Kreisendes Herz";
system = "superrare_circling_heart";
},
{
"attach_to_rootbone" = 1;
id = 20;
name = "Karten-Briefmarken";
system = "stamp_spin";
},
{
"attach_to_rootbone" = 0;
attachment = "cig_drg_smoke";
id = 28;
name = "Eleganter Rauch";
system = "drg_pipe_smoke";
},
{
"attach_to_rootbone" = 1;
id = 29;
name = "St\U00fcrmischer Sturm";
system = "unusual_storm";
},
{
"attach_to_rootbone" = 1;
id = 30;
name = "Eisiger Sturm";
system = "unusual_blizzard";
},
{
"attach_to_rootbone" = 1;
id = 31;
name = "Muttern & Bolzen";
system = "unusual_orbit_nutsnbolts";
},
{
"attach_to_rootbone" = 1;
id = 32;
name = "Kreisende Planeten";
system = "unusual_orbit_planets";
},
{
"attach_to_rootbone" = 1;
id = 33;
name = "Kreisendes Feuer";
system = "unusual_orbit_fire";
},
{
"attach_to_rootbone" = 1;
id = 34;
name = Blubbern;
system = "unusual_bubbles";
},
{
"attach_to_rootbone" = 1;
id = 35;
name = Rauchen;
system = "unusual_smoking";
},
{
"attach_to_rootbone" = 1;
id = 36;
name = Dampfen;
system = "unusual_steaming";
},
{
"attach_to_rootbone" = 1;
id = 37;
name = "Brennende Laterne";
system = "unusual_orbit_jack_flaming";
},
{
"attach_to_rootbone" = 1;
id = 38;
name = "Bew\U00f6lkter Mond";
system = "unusual_fullmoon_cloudy";
},
{
"attach_to_rootbone" = 1;
id = 39;
name = "Blubbernder Kessel";
system = "unusual_bubbles_green";
},
{
"attach_to_rootbone" = 1;
id = 40;
name = "Schauriges, kreisendes Feuer";
system = "unusual_orbit_fire_dark";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 43;
name = Messersturm;
system = "unusual_storm_knives";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 44;
name = "Nebliger Sch\U00e4del";
system = "unusual_skull_misty";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 45;
name = Herbstmond;
system = "unusual_fullmoon_cloudy_green";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 46;
name = "Es ist ein Geheimnis f\U00fcr jeden";
system = "unusual_fullmoon_cloudy_secret";
},
{
"attach_to_rootbone" = 1;
attachment = muzzle;
id = 47;
name = "St\U00fcrmische 13. Stunde";
system = "unusual_storm_spooky";
}
);
attributes = (
{
"attribute_class" = "mult_dmg";
defindex = 1;
"description_format" = "value_is_percentage";
"description_string" = "%s1%\U00a0geringerer Schaden";
"effect_type" = negative;
hidden = 0;
"max_value" = "0.85";
"min_value" = "0.95";
name = "damage penalty";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_dmg";
defindex = 2;
"description_format" = "value_is_percentage";
"description_string" = "+%s1%\U00a0Schadensbonus";
"effect_type" = positive;
hidden = 0;
"max_value" = "1.1";
"min_value" = "1.05";
name = "damage bonus";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_clipsize";
defindex = 3;
"description_format" = "value_is_percentage";
"description_string" = "%s1%\U00a0Magazingr\U00f6\U00dfe";
"effect_type" = negative;
hidden = 0;
"max_value" = "0.75";
"min_value" = "0.9";
name = "clip size penalty";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_clipsize";
defindex = 4;
"description_format" = "value_is_percentage";
"description_string" = "+%s1%\U00a0Magazingr\U00f6\U00dfe";
"effect_type" = positive;
hidden = 0;
"max_value" = "1.1";
"min_value" = "1.05";
name = "clip size bonus";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_postfiredelay";
defindex = 5;
"description_format" = "value_is_inverted_percentage";
"description_string" = "%s1%\U00a0niedrigere Schussfrequenz";
"effect_type" = negative;
hidden = 0;
"max_value" = "1.25";
"min_value" = "1.05";
name = "fire rate penalty";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_postfiredelay";
defindex = 6;
"description_format" = "value_is_inverted_percentage";
"description_string" = "+%s1%\U00a0h\U00f6here Schussfrequenz";
"effect_type" = positive;
hidden = 0;
"max_value" = "0.9";
"min_value" = "0.95";
name = "fire rate bonus";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_medigun_healrate";
defindex = 7;
"description_format" = "value_is_percentage";
"description_string" = "%s1%\U00a0Heilungsrate";
"effect_type" = negative;
hidden = 0;
"max_value" = "0.8";
"min_value" = "0.95";
name = "heal rate penalty";
"stored_as_integer" = 0;
},
{
"attribute_class" = "mult_medigun_healrate";
defindex = 8;
"description_format" = "value_is_percentage";
"description_string" = "+%s1%\U00a0Heilungsrate";
"effect_type" = positive;
hidden = 0;
"max_value" = "1.15";
"min_value" = "1.05";
name = "heal rate bonus";
"stored_as_integer" = 0;
},
My Question is: How do I get the name of an item at an index ?
I tried
[[[[news objectAtIndex:0]objectForKey:#"items"]objectAtIndex:2] objectForKey:#"name"];
News is the array that contains the Data !
Hope you can help
Tim
You seem to have a dictionary consisting of several arrays of dictionaries. I think you want something like this... (edit: like mentioned in the comments, it depends which array of dictionaries you need to get the name from, as there seem to be several to chose from in the results).
NSArray *array = [resultDictionary objectForKey:#"attribute_controlled_attached_particles"];
for (NSDictionary *aDictionary in array){
NSString *name = [aDictionary objectForKey:#"name"];
// do what you want with the string...
// could be useful if you for instance need to make an array of all the names
}
Or of course:
NSString *someString = [[array objectAtIndex:3] objectForKey:#"name"];
You have got a dictionary containing another dictionary (not a smart idea), so you should get the dictionary inside it using the objectForKey: selector. Now if you are able to get the key-value of a dictionary, you wouldn't have problems doing it N times.
The [] operator allows you to access the values of an array/dictionary and it works like objectAtIndex:/valueForKey: . Here is how to do it:
NSDictionary* result= json[#"result"];
NSArray* acabArray= result[#"attribute_controlled_attached_particles"];
for(NSDictionary* acabDict in acabArray)
{
NSNumber* num= acabDict[#"attach_to_rootbone"];
NSString* name= acabDict[#"name"];
// Obtain also the other key-values in a similar way
// Do what you want with these values, I suggest to build a custom
// object to hold all these values, and put every object in a mutable array.
}
You have an NSDictionary containing a NSDictionary (keyed by "result") containing an NSArray (keyed by "attribute_controlled_attached_particles"). To get this containg array, you need to do:
NSArray *resultArray = [[jsonDictionary objectForKey:#"result"] objectForKey:#"attribute_controlled_attached_particles"];
Inside the resultArray, you have an ordered list of dictionaries representing news items. So, to get the "name" string for the first index, do
NSDictionary *itemDictionary = [resultArray objectAtIndex:0];
NSString *name = [itemDictionary objectForKey:#"name"];
To enumerate all items, you need to iterate through the resultArray.
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"];
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.