I have Array of dictionary contents but i am not able to get the dictionary values.
Below is the dictionary format and in dictionary "resdata" and timestamp is dictionary key values. So i need to know how to get the timestamp values.
<__NSArrayM 0x89426d0>(
{
resData = (
{
timestamp = "2012-04-09 13:54:08 +0000";
}
);
seqCounter = 101;
}
here is the source code
for (int i = 0; i < [self.gluClkDetailArray count]; i++)
{
NSMutableDictionary *mDict = [self.gluClkDetailArray objectAtIndex:i];
NSDate *mDate = [[mDict objectForKey:#"resData"] objectForKey:#"timestamp"];
NSLog(#"NSDATE-----%#", mDate);
}
In above code the dictionary value is 0.
Thanks in advance
resData is an array of dictionaries.
Index into the array:
for (NSDictionary *mDict in self.gluClkDetailArray) {
NSArray *resData = [mDict objectForKey:#"resData"];
NSString *timestamp = [[resData objectAtIndex:0] objectForKey:#"timestamp"];
NSLog(#"timestamp: %#", timestamp);
}
Example (with llvm 4.0):
NSArray *a = #[ #{ #"resData" : #[ #{ #"timestamp" : #"2012-04-09 13:54:08 +0000" } ],
#"seqCounter" : #101
}
];
NSLog(#"a: %#", a);
for (NSDictionary *mDict in a) {
NSArray *resData = [mDict objectForKey:#"resData"];
NSString *timestamp = [[resData objectAtIndex:0] objectForKey:#"timestamp"];
NSLog(#"timestamp: %#", timestamp);
}
NSLog output:
a: (
{
resData = (
{
timestamp = "2012-04-09 13:54:08 +0000";
}
);
seqCounter = 101;
}
)
timestamp: 2012-04-09 13:54:08 +0000
Related
Right now i have a dictionary like this, it's just a example, i got A to Z:
(
{
id = 13;
name = "Roll";
firstLetter = R;
},
{
id = 14;
name = "Scroll";
firstLetter = S;
},
{
id = 16;
name = "Rock";
firstLetter = R;
},
{
id = 17;
name = "Start";
firstLetter = S;
}
)
I want to extract the dict has the same firstLetter and combine these into a NSArray object. The expected results like this:
R array:
(
{
id = 13;
name = "Roll";
firstLetter = R;
},
{
id = 16;
name = "Rock";
firstLetter = R;
}
)
and S array:
(
{
id = 14;
name = "Scroll";
firstLetter = S;
},
{
id = 17;
name = "Start";
firstLetter = S;
}
)
How to do that?
I believe the better method would be the one suggested by Saohooou
But it can be optimised as
NSArray *array = #[#{#"id": #13,#"name":#"Roll",#"firstLetter":#"R"},
#{#"id": #14,#"name":#"Scroll",#"firstLetter":#"S"},
#{#"id": #15,#"name":#"Rock",#"firstLetter":#"R"},
#{#"id": #16,#"name":#"Start",#"firstLetter":#"S"}];
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
NSString *key = dict[#"firstLetter"];
NSMutableArray *tempArray = dictionary[key];
if (!tempArray) {
tempArray = [NSMutableArray array];
}
[tempArray addObject:dict];
dictionary[key] = tempArray;
}];
NSLog(#"%#",dictionary);
NSMutableDictionay *dic = [NSMutableDictionay dictionay];
for ( YourObject *obj in yourDic.allValues )
{
NSMutableArray *dateArray = dic[obj.firstLetter];
if ( !dateArray )
{
dateArray = [NSMutableArray array];
[dic setObject:dateArray forKey:obj.firstLetter];
}
[dateArray addObject:obj];
}
so dic is what you want
I assume you organized the dict as an NSArray.
NSMutableDictionary* result = [NSMutableDictionary dictionary]; // NSDictionary of NSArray
for (id entry in dict) {
NSString* firstLetter = [entry firstLetter];
// Find the group of firstLetter
NSMutableArray* group = result[firstLetter];
if (group == nil) {
// No such group --> create new a new one and add it to the result
group = [NSMutableArray array];
result[firstLetter] = group;
}
// Either group has existed, or has been just created
// Add the entry to it
[group addObject: entry];
}
result holds what you want.
try this
NSString *currentStr;
//this int is to detect currentStr
NSInteger i;
NSMutableArray* R_Array = [[NSMutableArray alloc] init];
NSMutableArray* S_Array = [[NSMutableArray alloc] init];
for (NSDictionary *myDict in MyDictArray){
NSString *tempStr = [myDict objectForKey:#"firstLetter"];
if(currentStr = nil && [currentStr isEqualToString:""]){
currentStr = tempStr;
if([currentStr isEqualToString:"R"] ){
[R_Array addObject:myDict];
i = 0;
}else{
[S_Array addObject:myDict];
i = 1;
}
}else{
if([currentStr isEqualToString:tempStr]){
(i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
}else{
(i=0)?[R_Array addObject:myDict]:[S_Array addObject:myDict];
}
}
}
Base on your dictionaries. There are only two type, so i just created two array and use if-else for solving the problem. if there are multy values, you can try switch-case to do it.
Lets do this
NSMutaleDictionary * speDict = [[NSMutableDictionary alloc] init];
for(i=0;i<26;i++){
switch (i){
case 0:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"A"];
break;
case 1:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"B"];
break;
Case 2:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"C"];
break;
...........
Case 25:
[speDict setObject:[NSMutableArray alloc] init] forKey:#"Z"];
break;
}
}
for (NSDictionary *myDict in MyDictArray){
NSString *tempStr = [myDict objectForKey:#"firstLetter"];
switch (tempStr)
case A:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
case B:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
Case C:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
...........
Case Z:
[self addToMySpeDictArrayWithObject:myDict andStr:temStr];
break;
}
-(void)addToMySpeDictArrayWithObject:(NSDictionary*)_dict andStr:(NString*)_str
{
NSMutableArray *tempArray = [speDict objectForKey:_str];
[tempArray addObject:_dict];
}
then the speDict is like
A:
//all firstletter is A
myDict
myDict
myDict
B:
//all firstletter is B
myDict
myDict
.......
First of all the sample you've provided is an array of dicts (not a dict as the question notes). Now, the easiest way to query this array is by using an NSPredicate. Something like this perhaps:
NSArray *objects = ...; // The array with dicts
NSString *letter = #"S"; // The letter we want to pull out
NSPredicate *p = [NSPredicate predicateWithFormat:#"firstLetter == %#", letter];
NSArray *s = [objects filteredArrayUsingPredicate:p]; // All the 'S' dicts
If for some reason you need to group all of your objects without having to ask for a specific letter each time, you could try something like this:
// Grab all available firstLetters
NSSet *letters = [NSSet setWithArray:[objects valueForKey:#"firstLetter"]];
for (NSString *letter in letters)
{
NSPredicate *p = [NSPredicate predicateWithFormat:#"firstLetter == %#", letter];
NSArray *x = [objects filteredArrayUsingPredicate:p];
// Do something with 'x'
// For example append it on a mutable array, or set it as the object
// for the key 'letter' on a mutable dict
}
And of course you could further optimize this approach by implementing a method for filtering the array based on a letter. I hope that this makes sense.
I want to merge two NSDictionary in one NSDictionary.
my first DIctionary
BreakFast Dates Dic {
"2012-07-24" = "27";
"2012-08-03" = "98";
"2012-08-06" = "4203";
"2012-08-23" = "0";
"2012-08-24" = "0";
"2012-09-11" = "36";
"2012-09-19" = "450";
"2012-09-24" = "36";
Goals = 3;
}
my second Dictonary
Dinner Dates Dic {
"2012-08-03" = "100";
"2012-08-27" = "0";
"2012-09-19" = "270";
Goals = 4;
}
i want to merge both dictionary into one. like if in breakfast i have "2012-09-19" and same date i have in dinner. i want to separate both values by comma (,) .
my resultant dictionary should be like this.
combine {
"2012-07-24" = "27,0";
"2012-08-03" = "98,100";
"2012-08-06" = "4203,0";
"2012-08-23" = "0,0";
"2012-08-24" = "0,0";
"2012-09-11" = "36,0";
"2012-09-19" = "450,270";
"2012-09-24" = "36,0";
Goals = 3,4;
}
NSMutableDictionary *combined = [objectDelegate.dataBreakFastSummary mutableCopy];
for(NSString *key in [combined allKeys])
{
NSString *breakfastDate = [objectDelegate.dataBreakFastSummary valueForKey:key];
NSString *dinnerDate = [objectDelegate.dataDinnerSummary objectForKey:key];
if(dinnerDate){
[combined setObject:[NSString stringWithFormat:#"%#,%#",breakfastDate, dinnerDate] forKey:key];
} else {
[combined setObject:[NSString stringWithFormat:#"%#,0",breakfastDate] forKey:key];
}
}
NSLog(#"Combine %#",combined);
I have array with nsdictionary objects, for ex.:
({"someKey" = "title";
"name" = "someName";
},
{"someKey" = "title";
"name" = "anotherName";
}
{"someKey" = "newTitle";
"name" = "someName";
}
)
Please, help me to sort it in this format:
({"someKey" = "title";
"names": (
{"name" = "someName"},
{"name" = "anotherName"}
)
},
{"someKey" = "newTitle";
"names": (
{"name" = "someName"}
)
}
)
Thanks..
As far as i understand from your question you want to pick individual objects into one single dictionary object..
NSArray *yourArray=.... /* array which contains this data: ({"someKey" = "title";
"name" = "someName";
},
{"someKey" = "title";
"name" = "anotherName";
}
)*/
NSMutableDictionary *newDictionary=[[NSMutableDictionary alloc]init];
for(int i=0;i<[yourArray count];i++){
[newDictionary setObject:[yourArray objectAtIndex:i] forKey:#"names"];
}
({"someKey" = "title";
"name" = "someName";
},
{"someKey" = "title";
"name" = "anotherName";
}
)
Assuming above is equivalent to
NSArray *array=[NSArray arrayWithObjects:dictionary1,dictionary2,nil];
then to achieve below result
(
{
"someKey" = "title";
"names": (
{"name" = "someName"},
{"name" = "anotherName"}
)
}
)
We have this :
//Get name array
NSMutableArray *names=[NSMutableArray array];
for(int i=0;i<[array count];i++)
{
NSDictionary *dictionary=[array objectAtIndex:i];
[names addObject:[dictionary valueForKey:#"name"];
}
NSDictionary *newDictionary=[NSDictionary dictionaryWithOjbectsAndKeys:#"someKey",#"title",names,#"names",nil];
//Your final result is an array with one object ( A Dictionary )
NSArray *finalArray=[NSArray arrayWithObjects: newDictionary,nil];
You just need to traverse the current structure and build a new dictionary with the title as the unique key:
NSEnumerator* arrayEnumerator = [myArray objectEnumerator];
NSDictionary* = dictFromArray;
NSMutableArray* titleArray = [[NSMutableArray alloc] init];
NSMutableDictionary* titleDict = [[NSMutableDictionary alloc] init];
while(dictFromArray = [arrayEnumerator nextObject])
{
currentTitle = [dictFromArray objectForKey:#"someKey"];
currentName = [dictFromArray objectForKey:#"name"];
if([titles containsObject:currentTitle)
{
NSMutableDictionary namesArray = [titleDict objectForKey:currentTitle];
[namesArray addObject:currentName];
}
else
{
[titles addObject:currentTitle];
[titleDict addObject:[NSMutableArray arrayWithObject:currentName] forKey:currentTitle];
}
}
This should give you a dictionary that looks like:
{
title =
(
someName,
anotherName
);
newTitle =
(
someName
)
}
To get the exact structure you have above, I think this should work:
NSArray* titleKeys = [titleDict allKeys];
NSEnumerator* keyEnumerator = [titleKeys objectEnumerator];
NSMutableArray* finalArray = [[NSMutableArray alloc] init];
NSString* key;
while (key = [keyEnumerator nextObject])
{
[finalArray addObject: [NSDictionary
dictionaryWithObjects:(key, [dictArray objectForKey:key])
forKeys:(#"someKey", #"names")]];
}
I have one more big problem while parse the values from Webservice response. I dont know the key for the values in the webservice response. For example,
class = (
{
"ObjectiveC" =
(
{
"brief_desc" = "ObjectiveC";
date = "2008-02-27";
"event_status" = Attended;
},
{
"brief_desc" = "ObjectiveC";
date = "2008-03-05";
"event_status" = Attended;
},
{
"brief_desc" = "ObjectiveC";
date = "2008-03-12";
"event_status" = Missed;
},
);
},
{
"Java" = (
{
"brief_desc" = "Java";
date = "2008-02-27";
"event_status" = Attended;
},
{
"brief_desc" = "Java";
date = "2008-03-05";
"event_status" = Attended;
},
{
"brief_desc" = "Java";
date = "2008-03-12";
"event_status" = Missed;
},
);
}
);
In this response even we dont know the keys "ObjectiveC" and "Java". The keys("ObjectiveC and Java") should be change in every response retured. How to get values of key (Unknown key)? How can i parse this response and get the values?
I would enumerate through the keys to get the value. Here is an example of enumeration in objective-c.
NSEnumerator *enumerator = [myDictionary keyEnumerator];
id key;
while ((key = [enumerator nextObject])) {
//assuming value will be string
NSString *valueForKey = [myDictionary valueForKey:key];
//assuming value is another dictionary
NSDictionary *subDictionary = [myDictionary objectForKey:key];
}
And if you don't know the keys of the sub dictionaries, you can enumerate through those as well.
NSDictionary has - (NSArray *)allKeys and - (NSArray *)allValues. One of those might help
NSDictionary * lessonDict = [[NSDictionary alloc] initWithObjectsAndKeys:#"Key", #"Value", #"Key 2", #"Value 2", nil];
NSArray *values = [lessonDict allValues];
NSArray *keys = [lessonDict allKeys];
NSLog(#"Keys: %#",keys);
NSLog(#"Values: %#",values);
I have the following JSON value:
-(
{ Key = IsEmail;
Value = 1; },
{ Key = TrackingInterval;
Value = 20; },
{ Key = IsBackup;
Value = 1; },
{ Key = WipeOnRestore;
Value = 1; }
)
How might I go about parsing this object into an array or string? - i.e. eack key values to be stored in an array and each Value to be stored in another array.
Please help me out with this.
Thanks :)
This approach uses the json-framework.
I've shortened your example:
NSString *jsonString = #"[{\"Key\":\"IsEmail\",\"Value\":\"1\"},{\"Key\":\"TrackingInterval\",\"Value\":\"20\"},{\"Key\":\"IsBackup\",\"Value\":\"1\"}]";
NSMutableArray *keys = [NSMutableArray array];
NSMutableArray *values = [NSMutableArray array];
NSArray *json = [jsonString JSONValue];
for (NSDictionary *pair in json) {
[keys addObject:[pair objectForKey:#"Key"]];
[values addObject:[pair objectForKey:#"Value"]];
}
NSLog(#"%#", keys);
NSLog(#"%#", values);
Output:
2011-05-18 14:23:55.698 [36736:207] (
IsEmail,
TrackingInterval,
IsBackup
)
2011-05-18 14:23:55.700 [36736:207] (
1,
20,
1
)
Refere
http://www.xprogress.com/post-44-how-to-parse-json-files-on-iphone-in-objective-c-into-nsarray-and-nsdictionary/
http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/
http://mobile.tutsplus.com/tutorials/iphone/iphone-json-twitter-api/
http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c
Your data is not vald json, You may want to structure it more like this:
var theObj = { IsEmail: 1, TrackingInterval: 20, IsBackup: 1, WipeOnRestore: 1 };
Then you could populate your key and value arrays something like this:
var keys = new Array();
var values = new Array();
for (prop in theObj) {
keys.push(prop);
values.push(theObj[prop]);
}
if the JSON is in below format,
responseString=[ {
Key = IsEmail;
Value = 1;
},
{
Key = TrackingInterval;
Value = 20;
},
{
Key = IsBackup;
Value = 1;
},
{
Key = WipeOnRestore;
Value = 1;
}]
then,
NSArray *resultArray=[responseSrting JSONValue];
NSMuatbleArray *keyArray=[[NSMutableArray alloc] init];
NSMutableArray *valueArray=[[NSMutableArray alloc] init];
for(NSDictionary *dict in resultsArray){
[keyArray addObject:[dict objectForKey:#"Key"]];
[valueArray addObject:[dict objectForKey:#"Value"]];
}
then, all your keys are stored in keyArray and all your values are stored in valueArray