Dictionary giving improper value - ios

This is my code
for(id tempLangItem in jp.staffData)
{
NSMutableDictionary *temp=[[NSMutableDictionary alloc] initWithDictionary:tempLangItem];
NSString *name = [temp objectForKey:#"lang_name"];
NSLog(#"item_name =%#",name);
NSLog(#"value in dictionary=%#",temp);
}
These are log details
item_name =marinieres
value_in_dictionary={
"lang_id" = 2;
"lang_name" = "\U7a46\U840a\U65afmarinieres";
time = "2013-06-05 05:14:50";
}
why it is giving lang_name=\U7a46\U840a\U65afmarinieres in value_in_dictionary logs while it is displaying correct in item_name log.

Tried
NSMutableDictionary *temp=[[NSMutableDictionary alloc]init];
[temp setObject:#"marinieres" forKey:#"lang_name"];
NSString *name = [temp objectForKey:#"lang_name"];
NSLog(#"item_name =%#",name);
NSLog(#"value in dictionary=%#",temp);
and what my log shows
2013-06-06 12:38:02.337 Cool[96423:11303] item_name =marinieres
2013-06-06 12:38:04.022 Cool[96423:11303] value in dictionary={
"lang_name" = marinieres;
}
1 Quick question: if it is an NSDictionary why you creating a new instance?
NSMutableDictionary *temp=[[NSMutableDictionary alloc] initWithDictionary:tempLangItem];
try with tempLangItem

Related

How to retrieve specific value of key in json?

this is my json content.
[
{
"sha":"30eae8a47d0203ac81699d8fc2ab2632de2d0bba",
"commit":{
"author":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"committer":{
"name":"Madhura Bhave",
"email":"mbhave#pivotal.io",
"date":"2017-03-23T23:14:32Z"
},
"message":"Merge branch '1.5.x'",
}
}
]
and this is my main.i just want to retrieve key value from message and name,email,date from committer dictionary.i got stuck how to do that.
NSMutableArray *CommitArray = [[NSMutableArray alloc] init];
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
commitDictObj.message = [CommitDictionary objectForKey:#"message"];
for (NSDictionary *CommitterDictionary in [CommitDictionary objectForKey:#"committer"]) {
Committer *author = [[Committer alloc] init];
author.name = [CommitterDictionary objectForKey:#"name"];
author.email = [CommitterDictionary objectForKey:#"email"];
author.date = [CommitterDictionary objectForKey:#"date"];
}
[CommitArray addObject:commitDictObj];
}
for (int i =0 ; i < [CommitArray count] ; i++){
CommitDict *commitDictObj = [CommitArray objectAtIndex:i];
NSLog(#"Commit Message: %#", commitDictObj.message);
}
return 0;
}
}
i try fetch the json and display it value of message,name,email and date.how can i log the value of message, name, email and date?
Your array contains a dictionary, and that dictionary contains the commit dictionary, not the commit dictionary directly. Replace that part of your code:
for (NSDictionary *CommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
With that:
for (NSDictionary *shaCommitDictionary in CommitJson) {
CommitDict *commitDictObj = [[CommitDict alloc] init];
NSDictionary *CommitDictionary = [shaCommitDictionary objectForKey:#"commit"];
(1) Convert JSON to NSDictionary
NSData *jsonData= ... // Assume you got the data already loaded
NSError *error = nil;
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
(2) Access the dictionary values (fast enumeration available by now!!
NSString *message = dictionary[#"message"];
NSDictionary *author = dictionary[#"author"];
NSString *name = author[#"author"];
NSString *email = author[#"author"];
NSString *date = author[#"author"];
// OR:
// NSString *name = dictionary[#"author"][#"author"];
// NSString *email = dictionary[#"author"][#"author"];
// NSString *date = dictionary[#"author"][#"author"];
And thats it. I think the tricky thing is to get the JSON Data to the NSDictionary?
See here: https://stackoverflow.com/a/30561781/464016

Unable to retrieve the data from Dictionary

In my project I am getting response from the server in the form
response:
<JKArray 0x7fa2e09036b0>(
{
id = 23;
name = "Name1";
},
{
id = 24;
name = "Name2";
}
)
From this response array i am retrieving the objects at different indexes and then adding them in a mutableArray and then into a contactsDictionary.
self.contactsDictionary = [[NSMutableDictionary alloc] init];
for(int i=0 ; i < [response count] ; i++)
{
NSMutableArray *mutableArray=[[NSMutableArray alloc] init];
[mutableArray addObject:[response objectAtIndex:i]];
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
}
I want to retrieve data for Key #"name" from the contactsDictionary at some other location in the project. So how to do it.
Thanks in advance....
this is the wrong way like you are setting your contactsDictionary.
replace below line
[self.contactsDictionary setObject:mutableArray forKey:[NSString stringWithFormat:#"%i",i]];
with
[self.contactsDictionary setObject:[mutableArray objectAtIndex :i] forKey:[NSString stringWithFormat:#"%i",i]];
becuase everytime your array have new objects so your contacts dictionary's first value have one object then second value have two object. so you shouldn't do that.
now, if you want to retrieve name then call like
NSString *name = [[self.contactsDictionary objectForKey : #"1"]valueForKey : #"name"];
avoid syntax mistake if any because have typed ans here.
Update as per comment:
just take one mutablearray for exa,
NSMutableArray *arr = [[NSMutableArray alloc]init];
[arr addObject : name]; //add name string like this
hope this will help :)
Aloha from your respond I can give you answer Belo like that according to you response.
for(int i=0;i<[arrRes count];i++);
{
NSString *strId = [NSString stringWithFormat:#"%#",[[arrRes obectAtIndex:i]objectForKey:#"id"]];
NSString *StrName = [NSString stringWithFormat:#"%#",[[arrRes objectAtIndex:i]objectForKey:#"name"]];
NSLog(#"The ID is -%#",strId);
NSLog(#"The NAME is - %#",strName);
}

Separate a String By Semicolon

I am using following code to separate a string into multiple strings and getting an error
NSArray *arr = [randomStr componentsSeparatedByString:#";"];
Error:
-[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x1758f230
-[__NSDictionaryM componentsSeparatedByString:]: unrecognized selector sent to instance 0x1758f230
This is my Sample Data
NSArray *data = {
{
name = "name1";
address = "RWP";
ID = 0;
},
{
name = "name2";
address = "RWP";
ID = 1;
},
{
name = "name3";
address = "RWP";
ID = 2;
},}
NSString *randomStr = data[0];
What's wrong in my code
You have an array of dictionaries, not strings. There is nothing to split.
You want something like this:
NSDictionary *dict = data[0];
NSString *name = dict[#"name"];
NSString *address = dict[#"address"];
You have to be sure randomStr as a NSString.
You can check like
if ([randomStr isKindOfClass:[NSString Class]]) {
NSArray *arr = [randomStr componentsSeparatedByString:#";"];
}
It sounds like the randomStr variable is an MutableDictionary. Thats why its not working.
here is my Test:
NSString *foo = #"BAR;FOO;VAR";
NSArray *arr = [foo componentsSeparatedByString:#";"];
NSLog(#"%#", arr);
Thise Logs:
BAR,
FOO,
VAR
EDIT:
Dictionary to array:
NSMutableDictionary *dict = #{#"aKey1" :#"BAR",
#"aKey2" :#"FOO",
#"aKey3" :#"VAR"}.mutableCopy;
NSMutableArray *array = [[NSMutableArray alloc] init];
for (NSString *key in dict) {
[array addObject:dict[key]];
}
NSLog(#"%#",array);
This Logs:
BAR,
FOO,
VAR

Put multiple arrays in Dictionary

I am parsing a CSV file multiple times with for loop, here I need to store these arrays one by one dictionary. There are very less questions in stack about adding NSArray to NSDictionary. I am parsing CSV with below code but I strucked at storing in NSDictionary, The program is terminating and showing warning at assigning string to dictionary
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSLog(#"Serail No %d %#",i,keysArray);
NSString *string = [NSString stringWithFormat:#"%d", i];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: keysArray forKeys: string];
}
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSString *key = [NSString stringWithFormat:#"serial%d",i];
[dict setObject:keysArray forKey:key];
}
To get back data from dictionary,
NSArray *array = [dict valueForKey:#"serial24"];//to get array 24.
If I understand you correctly, you want to add the arrays to a dictionary, with the key being the string value of integer i ? What you need to do is allocate the dictionary outside your loop -
NSMutableDictionary *dict=[NSMutableDictionary new];
for (i=0; i<=57; i++) {
NSString *keysString = [csvArray objectAtIndex:i];
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
NSLog(#"Serial No %d %#",i,keysArray);
NSString *string = [NSString stringWithFormat:#"%d", i];
dict[string]=keysArray;
}
I am not sure why you would want to do this, because this is basically an array. You could simply do -
NSMutableArray *outputArray=[NSMutableArray new];
for (NSString *keysString in csvArray) {
NSArray *keysArray = [keysString componentsSeparatedByString:#","];
[outputArray addObject:keysArray];
}

I've got strange output from 'componentsSeparatedByString' method of NSString

I want to store the array of NSDictionary to a file. So I write a function to convert from NSArray to NSString. But I got a very strange problem. Here is my code.
+ (NSArray *)arrayForString:(NSString*)dataString
{
NSArray* stringArray = [dataString componentsSeparatedByString:ROW_SEPARATOR];
NSLog(#"%#", stringArray);
NSMutableArray* dictionaryArray = [[NSMutableArray alloc] initWithCapacity:0];
for (int i = 0; i < [stringArray count]; i++)
{
NSString* string = [stringArray objectAtIndex:i];
NSLog(#"%#", string);
NSArray* subStrings = [string componentsSeparatedByString:COLUMN_SEPARATOR];
NSDictionary* dic = [[NSDictionary alloc] initWithObjectsAndKeys:[subStrings objectAtIndex:0], PHOTO_NAME, [NSNumber numberWithUnsignedInt:[[subStrings objectAtIndex:1] unsignedIntValue]], PHOTO_SEQ_NO, nil];
[dictionaryArray addObject:dic];
}
return dictionaryArray;
}
Here is the log:
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)
2012-05-05 23:57:35.118 SoundRecognizer[147:707] new Photo/0
2012-05-05 23:57:35.123 SoundRecognizer[147:707] -[__NSCFString unsignedIntValue]: unrecognized selector sent to instance 0x1d18c0
How do I get a #"-" from this following array?!
2012-05-05 23:57:35.113 SoundRecognizer[147:707] (
"new Photo/0",
"new Photo/1"
)
NSString doesn't have an unsignedIntValue method. Use intValue instead. But I'm not sure of the point of all this - you can write an array of dictionaries straight to a file anyway (as long as they only contain property list types) using writeToFile: atomically:.

Resources