I have a JSON data in unicode (like: \u0661\u0660) that I am displaying in my cellForRowAtIndexPath. I need to make the cell of different color when the value key from json is "عشرة ١٠".
Here's the code:
NSString *filePath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"json"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
_params = [[json valueForKey:#"data"] valueForKey:#"list"];
CellForRowAtIndexPath:
NSString *match = #"عشرة ١٠";
item = [[_params objectAtIndex:indexPath.row] valueForKey:#"value"];
Is there a way to compare match
with item? The containsString, localizedStandardContainsString do not work.
You can convert unicode to NSString and than compare it,
refer this link and this link
After converting it to string you can simply check it by,
[your_string isEqualToString:#"your second string"];
Hope this will help you.
Related
I am new to json.
I want to know that how can I get code and name of country from
http://country.io/names.json
and display it in label.
I only want to know objectForKEy and valueForKey for this.
I want output like,
I want Output like,
IN India
In my tableView
I have two label in my cell, one for Code and another for name.
do like
NSMutableArray *Name = [[NSMutableArray alloc] init];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://country.io/names.json"]];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
for (NSString *findkey in [json allKeys]) {
//[Name addObject:[json objectForKey:findkey]];
[Name addObject:[NSString stringWithFormat:#"%# %#",findkey,[json objectForKey:findkey]]];
}
NSLog(#"Name==%#",Name);
you get output like
Good afternoon,
I'm trying to store a "text" using NSString from my JSON output but it's not working because it's always NULL. It's working fine with the other info (because they are integers) but when I have to store the info (the picture) in a NSString it's always NULL.
I have tried to search a lot of places in order to see how I have to declare that "NSString" and make it work but it's not working and I need some help.
Can you help me with that? How can I fill the NSString with the information in "picture" from my JSON output?
ProfileViewController:
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSString *picture = [jsonData objectForKey:#"picture"];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:picture]];
self.profileimage.image = [UIImage imageWithData:imageData];
JSON output:
{"success":1,"stars":0,"photos":0,"followers":0,"picture":http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg}
Thanks in advance.
The code looks correct, however the JSON output is not valid according to http://jsonlint.com/
You need to wrap the link with quotes ("http://m.c.lnkd.licdn.com/mpr/mpr/shrink_200_200/p/3/000/25b/1fe/200e9f3.jpg") and then try again.
I am trying to request the following URL and i am getting an error saying "Data parameter is nil". i had found that the 'seller_name' has got space and 'amount' has got dot(.). I think this is the problem that is being caused by the URL. So is there any way to send URL having space and dot without loosing the information?
NSURL *url1 = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];
Take a look at the -stringByAddingPercentEscapesUsingEncoding: method in NSString, e.g.:
NSString *myUnencodedString = [NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]
NSString *encodedString = [myUnencodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]
...
Cf.: Apple documentation.
It is happening because URL should not contain white space. It should be encode to %20 if there is any space. We can encode space and special character in NSString using NSUTF8StringEncoding as below
NSString *string = [[NSString stringWithFormat:#"192.168.1.85/localex_postsale.html?contactid=%#&exchangeid=%#&token=%#&buyerid=%#&seller_name=%#&desc=%#&amount=%#&sellerid=%#",contactid,exchangeid,token,buyervalue,sellers,_Description,_Amount,contactid]stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url1 = [[NSURL alloc]initWithString:string];
NSError *errors1;
NSData *data1 = [NSData dataWithContentsOfURL:url1];
NSDictionary *json1 = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data1 options:kNilOptions error:&errors1];
I did it easily in Android but Im little lost how to do it in iOs.
My json is like:
[{"name":"qwe","whatever1":"asd","whatever2":"zxc"},
{"name":"fgh","whatever1":"asd","whatever2":"zxc"}]
If I do:
NSData *jsonData = [data dataUsingEncoding:NSUTF32BigEndianStringEncoding];
rows = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
in rows can I access with?
NSString *name = [[rows objectAtIndex:0] [objectForKey:#s"name"]];
Or how I do that? thanks.
FINALLY NSString *name = [[rows objectAtIndex:0] objectForKey:#"name"]]; WORKS! :D
I think that will work, however you want:
NSString *name = [[rows objectAtIndex:0] objectForKey:#"name"];
(dropped extraneous square brackets and use #"name" as string literal).
However, is the input JSON really in UTF-32 format?
NSMutableArray *row= [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &error];
for (int i=0; i<[row count]; i++) {
NSMutableDictionary *dict=[row objectAtIndex:i];;
NSString * name=[dict valueForKey:#"name"];
NSLog(#"%#",name);
}
}
Assuming the NSJSONSerialization operation was successful, simply:
NSString *name = rows[0][#"name"];
NSUTF32BigEndianStringEncoding is suspicious, json data is more usually NSUTF8StringEncoding.
In my iOS 5.1 application, I'm trying to load a JSON text file into a NSDictionary.
NSString *filepath = [[NSBundle mainBundle] pathForResource:#"stuff" ofType:#"json"];
NSDictionary *stuff = [NSDictionary dictionaryWithContentsOfFile:filepath];
The filepath is valid, the file exists, but stuff is nil, which means there was a problem decoding the file (according to the documentation).
(How) Can I get more debugging information on why [NSDictionary dictionaryWithContentsOfFile:] - or any kind of API call - returns nil?
Read the contents as NSData and then use +[NSPropertyListSerialization propertyListWithData:options:format:error:].
As you said: Your file contains a JSON string. First, you have to load the string and then you have to parse it using some JSON library.
E.g.:
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString: content];