NSDictionary array of same key - ios

I'm requesting a SOAP Webservice and I parse the response to a NSDictionary. The XML response has multiple unique keys. Here's an example (already parse on the dictionary):
"typ:partits" = {
"typ:dadesPartit" = {
"typ:aforament" = 1;
"typ:codiEsdeveniment" = 2;
"typ:competicio" = GAMPER;
"typ:dataHoraConfirmada" = false;
"typ:dataPartit" = "08/18/14";
"typ:descripcioPartit" = "FCBARCELONA - CLUB LEON F.C.";
"typ:horaPartit" = "9:30:00 PM";
"typ:jornada" = 99;
"typ:partitActiuMenor" = true;
"typ:temporada" = "2014-2015";
"typ:tipusEsdeveniment" = 0;
},
"typ:dadesPartit" = {
"typ:aforament" = 1;
"typ:codiEsdeveniment" = 2;
"typ:competicio" = GAMPER;
"typ:dataHoraConfirmada" = false;
"typ:dataPartit" = "08/26/14";
"typ:descripcioPartit" = "FCBARCELONA - REAL MADRID";
"typ:horaPartit" = "9:30:00 PM";
"typ:jornada" = 101;
"typ:partitActiuMenor" = true;
"typ:temporada" = "2014-2015";
"typ:tipusEsdeveniment" = 0;
};
};
How can I iterate through these keys?, they are the same :( ...
I tried with "allObjects" but when I receive only one "typ:dadesPartit" object it treats it like an array instead of a NSDictionary.

it is returning one type as both your keys are same,
Try renaming the keys to separate names, and the values associated with those keys will come when u type allKeys

Related

how to store that json data into array, after using jsonserialization iam getting that data

let jsonResult1:NSDictionary = NSJSONSerialization.JSONObjectWithData(da!, options:NSJSONReadingOptions.MutableContainers , error: &error) as NSDictionary
println(jsonResult1)
getting below data in console
{
0 = {
"consulting_dr" = "DR.Appaji .";
"current_bed_nr" = 0;
"current_room_nr" = 0;
"discharge_date" = "03/03/2015 00:00";
"encounter_date" = "02/03/2015 12:45";
"encounter_nr" = 201503024000;
info = "";
"item_description" = "";
name = "Mrs. mythily S";
pdob = "01/08/1976";
pid = 100004;
psex = f;
pyear = "38 Years";
};
1 = {
dosage = 1;
drdate = "25/08/2014";
drnotes = "";
drugclass = Tablet;
duration = "5 day";
frequency = "";
medicine = "ACECLOFENAC+PARACETAMOL";
route = Oral;
tcomplients = "";
};
2 = {
BMI = "A:1:{s:4:\"SPO2\";s:1:\"1\";}";
BSA = "A:1:{s:4:\"SPO2\";s:1:\"1\";}";
"Dystolic_bp" = 29;
Height = 24;
Pulse = 26;
Respiration = 27;
"Systolic_bp" = 28;
Temp = 25;
Weight = 22;
dosage = 1;
drdate = "25/08/2014";
drnotes = "";
drugclass = Tablet;
duration = "5 day";
frequency = "";
medicine = RABEPRAZOLE;
route = Oral;
tcomplients = "";
};
}
how to store this in array
That is a Dictionary. It is easy to get just the data without the keys.
In Objective-C it would be :
NSArray *allData = [jsonResult1 allValues];
For swift it should be like: (not sure about syntax)
var allData = jsonResult1.allValues()
If you're up for it, you should give SwiftyJSON a try.
https://github.com/SwiftyJSON/SwiftyJSON
I've recently used it for an application that deals with a ton of JSON responses from a web service and SwiftyJSON has made it super easy for me to deal with JSON data in Swift. It will convert NSData to Dictionaries or Arrays seamlessly for you.

iOS - How to parse Json array in xcode and save results are strings

Hi I am trying to parse a Json string as an NSArray and save certain results as strings to set permissions for different users in my app. My current code is:
NSError *jsonParsingError1 = nil;
accountData = [NSJSONSerialization JSONObjectWithData:jsonAccount
options:NSJSONReadingMutableContainers error:&jsonParsingError1];
accountData is an NSMutableArray created in the .h file.
jsonAccount is NSData created by converted an NSString
The NSLog out put for the array is;
{
account = "XXXX";
companyName = XXXXX;
id = XXXXX;
websites = (
{
account = "XXXXX";
accountId = XXXXX;
anonymiseIP = 0;
companyName = XXXXX;
XXXX = 0;
domains = (
"XXXXX"
);
features = {
advancedSegmentation = 1;
attentionHeatmaps = 1;
domains = 0;
dotHeatmaps = 1;
goalConversionTracking = 1;
interactionHeatmaps = 1;
leadInfo = 0;
scrollHeatmaps = 1;
timeHeatmaps = 1;
users = 0;
valueHeatmaps = 1;
visitorPlayback = 1;
visitorScoring = 1;
visitors = 1;
};
fixedElementSelector = "";
flagClicksReceived = 0;
flagDataReceived = 0;
flagGoalsReceived = 0;
flagInteractionsReceived = 0;
flagScrollsReceived = 0;
id = XXXXX;
interactionSelector = "";
name = "XXXX";
permissions = (
segments,
heatmaps,
visitors,
campaigns,
support,
globalSettings,
websiteSettings
);
setCookies = 1;
status = 1;
statusMessage = "";
statusString = OK;
trialling = 1;
}
);
},
When I try and create a sting from one of the results and display it in the log I get this error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray:]: unrecognized selector sent to instance 0x6a950f0'
How can I save different keys as strings?
Looks like that your JSON is a Dictionary, not an Array.
Your JSON is not array you should parse it like NSDictionary
After parsing the JSON, you should test what kind of object it returned. E.g.:
if ([accountData isKindOfClass:[NSArray class]]) {
// handle like an array
} else if ([accountData isKindOfClass:[NSDictionary class]]) {
// handle like a dictionary
}
Your JSON is a dictionary with four keys: account, companyName, id, and websites.
The key "websites" will give you an array.
You can iterate through the "websites" array, and each element is a dictionary.
Each of the dictionaries in the "websites" array has lots of keys like account, accountId, anonymiseIP and so on. Some of these keys have values that are dictionaries or arrays.
In the NSLog statement, (a, b, c) would be an array, while { a = x; b = y; c = z; } would be a dictionary.
Your server response is a dictionary so change accountData to dictionary
Write NSLog for below and you will get information in it
[accountData objectForKey:#"account"];
[accountData objectForKey:#"companyName"];
[accountData objectForKey:#"id"];
[[accountData objectForKey:#"websites"] count];//array
[[[accountData objectForKey:#"websites"] objectAtIndex:0]objectForKey:#"account"];
[[[accountData objectForKey:#"websites"] objectAtIndex:0]objectForKey:#"domains"];
[[[[accountData objectForKey:#"websites"] objectAtIndex:0]objectForKey:#"domains"]count]; //array
[[[[accountData objectForKey:#"websites"] objectAtIndex:0]objectForKey:#"domains"]objectAtIndex:0];

Extracting an NSDictionary from within another NSDictionary

My application has an NSDictionary containing many other NSDictionary inside it. If I print out this dictionary it reads as follows:
oxip = {
created = "2014-02-10 14:42:59";
lastMsgId = "";
requestTime = "1.6434";
response = {
code = 001;
debug = "";
message = success;
request = getHierarchyByMarketType;
text = "\n";
williamhill = {
class = {
id = 1;
maxRepDate = "2014-02-10";
maxRepTime = "07:31:48";
name = "UK Football";
text = "\n";
type = (
{
id = 2;
lastUpdateDate = "2013-12-26";
lastUpdateTime = "13:32:54";
market = (
{
betTillDate = "2014-02-15";
betTillTime = "15:00:00";
date = "2014-02-15";
id = 140780553;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South v Dundee - Match Betting";
participant = (
{
handicap = "";
id = 496658381;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Dundee;
odds = "11/8";
oddsDecimal = "2.38";
text = "\n\n\n\n\n\n";
},
{
handicap = "";
id = 496658380;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = Draw;
odds = "5/2";
oddsDecimal = "3.50";
text = "\n";
},
{
handicap = "";
id = 496658379;
lastUpdateDate = "2014-02-10";
lastUpdateTime = "14:09:13";
name = "Queen of the South";
odds = "11/8";
oddsDecimal = "2.38";
text = "\n";
}
);
text = "\n";
time = "15:00:00";
}
What is the best possible way for my application to reach the NSDictionary with the name of:
name = "Queen of the South v Dundee - Match Betting"
without the need of going through each individual dictionary and finding its object for key?
You can use valueForKeyPath for that. It accepts a path, separated by dots. Example:
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"https://dl.dropboxusercontent.com/u/1365846/21680479.json"]]
options:0
error:nil];
NSLog(#"%#", [dict valueForKeyPath:#"response.williamhill.class.type.market.name"]);
This depends on the representation of dictionary. If the williamhill part is changing, then it does not work, of course.
There is no way to obtain a reference within a map data type (in this case, an NSDictionary) without traversing it. Think of the simplified version of this problem: You have a linked list with N elements and you wish to reach the N'th element. Because this is a linked list, you'll have to go through all other N-1 nodes in order to obtain the last reference.
An NSDictionary is a hash based data type in which keys and values are stored. In the case you describe, you have no reference to the nested object (an NSDictionary itself) so you must also traverse all of the dictionaries containing it.
Hope this helps point you in the right direction.

Parsing Vimeo Json response in ios

I got this json response
{
"generated_in" = "0.0283";
stat = ok;
videos = {
"on_this_page" = 3;
page = 1;
perpage = 50;
total = 3;
video = (
{
"embed_privacy" = anywhere;
id = 73198189;
"is_hd" = 1;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-27 01:29:16";
owner = 20303618;
privacy = anybody;
title = Untitled;
"upload_date" = "2013-08-27 00:57:36";
},
{
"embed_privacy" = anywhere;
id = 73197989;
"is_hd" = 0;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-27 01:24:17";
owner = 20303618;
privacy = anybody;
title = sample2;
"upload_date" = "2013-08-27 00:52:40";
},
{
"embed_privacy" = anywhere;
id = 72961770;
"is_hd" = 0;
"is_watchlater" = 0;
license = 0;
"modified_date" = "2013-08-23 05:57:48";
owner = 20303618;
privacy = anybody;
title = sample;
"upload_date" = "2013-08-23 05:25:44";
}
);
};
}
when i am trying to parse it for the video id.
The technique i used is
i converted that json into NSDictionary jsondata and
NSString *videoid = [[[jsondata objectForKey:#"videos"]valueForKey:#"video"]valueForKey:#"id"];
NSLog(#"video string is %#",videoid);
the result is:
(
73198189,
73197989,
72961770
)
but i am not able to access the normal string functions on that string to retrieve the id's.
it is saying an error unrecognized selector was sent.
Is there a better way to parse that string, i tried google but every post says the same approach.
You not getting a string but an array of strings, the JSON response holds mutiple videos. The code you used will retrieve all the id of all the videos.
You can loop thru the found videos like:
NSArray *videoIdArray = [jsondata valueForKeyPath:#"videos.video.id"];
for(NSString *videoId in videoIdArray) {
NSLog(#"video string is %#",videoId);
}

iphone sdk+How to retrive dictionary inside array of dictionary values

i want to retrive KpiName key(KpiData is array inside kpiName is dictionary key) and my data structure mentioned below
--Array of dictionaries inside array of dictinary
ex..listOfProjectsArray--(
{
alertSeverity = 1;
customerName = TPMG;
endDate = "05-05-2013";
isProjectHide = 1;
kpiData = (
{
KpiName = "Change Request ";
kpiActual = 14;
kpiPlanned = "";
kpiUnit = "";
},
{
KpiName = "Aged Debit";
kpiActual = 24000;
kpiPlanned = "";
kpiUnit = EUR;
},
);
lastInvoiceDate = "05-04-2013";
nextBillingDate = "05-04-2013";
nextMilestoneDate = "10-04-2013";
nextReviewDate = "08-04-2013";
plannedCompletionDate = "04-05-2013";
projectID = 3000;
projectName = "Rain Harvesting";
projectSortType = "";
projectStatus = 1;
startDate = "01-01-2013";
},
i tried this one but its returning null
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
Try this
NSString *kpiName =[[[[listOfProjectsArray objectAtIndex:indexPath.row] objectForKey:#"kpiData"]objectAtIndex:0]objectForKey:#"KpiName"];
I was mentioned wrong dictionary key
NSString *kpiName = [[[[listOfProjectsArray objectAtIndex:indexPath.row] valueForKey:#"kpiData"] objectAtIndex:0]valueForKey:#"kpiName"];
NSLog(#"kpiname:%#", kpiName);
correct one is KpiName key instead of kpiName.

Resources