NSDictionnary to NSString - ios

I have a NSDictionary that comes like that : (
1,
2,
3
)
And I would like to assign a NSString like this: 123, how? Thank you
My code :
NSDictionary *keys = [self.json valueForKeyPath:#"survey.questions.id"][0][0];
NSString *keysString = [NSString stringWithFormat:#"my dictionary is %#", keys];

NSArray *keys = [dict allKeys];
NSString *result = [[keys valueForKey:#"description"] componentsJoinedByString:#""];

Related

Objective-c: Not getting the right text on NSArray

i am trying to catch some data on my plist , it has a lot of records
for that case i used this code
NSString *path = [[NSBundle mainBundle] pathForResource:#"kurdiebg" ofType:#"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"english = %#", self.searchQwery.text];
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(#"found matches: %# : %#", filtered,[filtered valueForKey:#"kurdi"]);
NSString*nss = [NSString stringWithFormat:#"%#",[filtered valueForKey:#"english"]];
self.lblWord.text = nss;
its all fine on the Log, but on the uilabel it returns this
on the NSLog
Thanks
your filter is array od NSdictionary
NSString *path = [[NSBundle mainBundle] pathForResource:#"kurdiebg" ofType:#"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:#"english = %#", self.searchQwery.text];
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(#"found matches: %# : %#", filtered,[filtered valueForKey:#"kurdi"]);
NSString*nss = [NSString stringWithFormat:#"%#",[filtered valueForKey:#"english"]];
if (filtered.count>0) {
NSDictionary *dic = filtered[0];
self.lblWord.text = dic[#"english"];
}

How to get an array type [1,2] instead of (1, 2) in Objective-c?

I'm trying to send an array in post JSONModel call. I need convett my array to NSString and send the array in format:
[1, 2, 3]
but when I convert this to NSString and print my array, this has the format:
(1, 2, 3)
NSMutableArray *array= [NSMutableArray arrayWithObjects:#"1", #"2",#"3",#"4", nil];
NSString *arraString = [NSString stringWithFormat:#"%#", arr];
NSLog(#"%#",arraString);
How can I create this with [] format?
NSMutableArray *array= [NSMutableArray arrayWithObjects:#"1", #"2",#"3",#"4", nil];
NSData *jsond = [NSJSONSerialization dataWithJSONObject: array options:NSJSONWritingPrettyPrinted error:NULL];
NSString *json = [[NSString alloc] initWithData:jsond encoding:NSUTF8StringEncoding];
NSLog(#"%#", json);
What you can do is
NSString *joinedString = [array componentsJoinedByString:#","];
NSString *arraString = [NSString stringWithFormat:#"(%#)", joinedString];
Hope this will fix your problem

How to fetch NSString from a JSON array of dictionaries?

This is my JSON:
-elements: [
{
HomeworkElementSession: {
id: "608743",
name: "Interval for x",
description: "",
}
}
]
...
I was able to get to the point where I have an actual NSArray representing the "elements" node and therefore containing only one object in the array.
But I have no idea how to reach this string "name".
What i did was:
NSMutableArray *elements = [singleHomework objectForKey:#"elements"];
for(int i=0; i<elements.count; i++){
NSDictionary* homeworkSession = [elements objectAtIndex:i];
NSString* name = [homeworkSession objectForKey:#"name"];
NSLog(#"%#",name);
}
But i get nil in Log.
What am I doing wrong ?
NSMutableArray *elements = [singleHomework objectForKey:#"elements"];
for(int i=0; i<elements.count; i++){
NSDictionary* homeworkSession = [elements objectAtIndex:i];
NSDictionary* dataDict = [homeworkSession objectForKey:#"HomeworkElementSession"];
NSString* name = [dataDict objectForKey:#"name"];
NSLog(#"%#",name);
}
You need to get the Dictionary for key HomeworkElementSession first
NSMutableArray *elements = [singleHomework objectForKey:#"elements"];
for(int i=0; i<elements.count; i++){
NSDictionary* mainhomeworkSession = [elements objectAtIndex:i];
NSDictionary* homeworkSession = [mainhomeworkSession objectForKey:#"HomeworkElementSession"];
NSString* name = [homeworkSession objectForKey:#"name"];
NSLog(#"%#",name);
}
Hope it helps you..!
NSMutableArray *elements = [singleHomework objectForKey:#"elements"];
for(NSDictionary *dict in elements){
NSDictionary* dataDict = [dict objectForKey:#"HomeworkElementSession"];
NSString* name = [dataDict objectForKey:#"name"];
NSLog(#"%#",name);
}
Try this may be help full ,
Note: you are getting output in NSString so use this
NSString *singleHomework = #"your data";
NSMutableDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:[singleHomework dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:Nil];
NSMutableArray *dataArray = [dataDic valueForKey:#"elements"];
NSLog(#"Name Print %#",[dataArray[0] valueForKey:#"name"]);
First, I'm not sure why you have a hyphen in the dictionary key "-elements". That could be a problem. However, your main problem is that your JSON is an array containing a single dictionary which then contains a dictionary (HomeworkElementSession) which has attributes.
NSString * json = #"{\"elements\": [{\"HomeworkElementSession\": {\"id\": \"608743\", \"name\":\"Interval for x\", \"description\": \"\"}}]}";
NSData * jsonData = [json dataUsingEncoding:NSUTF8StringEncoding];
NSError *e;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&e];
NSLog(#"dict=%#", dict);

get value from NSDictionary not work

1.I get JSON data from web services and add It's to NSDictionary
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
2.Then I Show "dic" in NSLog It's present
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
3.Then I get data value from "dic" in to new NSDictionary "dt"
NSDictionary *dt = [dic objectForKey: #"query"];
4.Then I Show "dt" in NSLog It's present
({ID = 01;IndexNo = 1;Picture = "img/food-48.png";})
5.I want to get "ID" from "dt". I use this code
NSString *ID = [dt objectForKey: #"ID"];
but it's error
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a40a60
Can you try this
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *arr = [dic objectForKey: #"query"];
NSDictionary *dt = arr[0];
NSString *ID = [dt objectForKey: #"ID"];
[dic objectForKey: #"query"] returns an NSArray actually. Note these square brackets in the output from the first NSLog statement:
{"query": [...]}
Try this:
NSArray *dtArray = [dic objectForKey: #"query"];
NSDictionary *dt = dtArray[0];
NSString *ID = [dt objectForKey:#"ID"];
Or to use the key-value coding behavior of NSDictionary and NSArray:
NSString *ID = [[dic valueForKeyPath:#"query.ID"] firstObject];
NSDictionary *dt = [[dic objectForKey: #"query"] objectAtIndex:0];
NSString *ID = [dt objectForKey: #"ID"];
You are trying to get value for key from array. change these lines.
From your jsonStructure
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
object for key "query" is an array as {} is a dictionary and [] is an array. so do the following
NSArray *tempArray = [dt objectForKey:#"query"];
NSDictionary *tempDictionary = [tempArray objectAtIndex:0];
NSString *ID = [tempDictionary objectForKey:#"ID"];
Hope this helps.

Can't Read plist File

I am having trouble reading the data in a plist file. I'm not getting the titleString printed in the console like I'm expecting. What am I doing wrong?
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSDictionary *dictPri = [[NSMutableDictionary alloc]initWithContentsOfFile:path];
NSMutableArray *arrEvents = [[NSMutableArray alloc] initWithArray:[dictPri valueForKey:#"Root"]];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = nil;
NSString *date = nil;
titleString = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Title"]];
date = [NSString stringWithFormat:#"%#",[dict valueForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}
Your main element (Root) is Dictionary - not Array - change it in plist by clicking on type next to it.
Also there is a problem in your code - you never access "Root" element by name - it's by default top-level object. Consider taking out additional array initialization which is not required.
Fixed code:
NSString *path = [[NSBundle mainBundle] pathForResource:#"Events" ofType:#"plist"];
NSArray* arrEvents = [NSArray arrayWithContentsOfFile:path];
for (NSDictionary *dict in arrEvents)
{
NSString *titleString = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Title"]];
NSString *date = [NSString stringWithFormat:#"%#",[dict objectForKey:#"Date"]];
NSLog(#"Title String: %#", titleString);
}

Resources