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.
Related
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.
I have the following code :
NSData * jsonData = [NSData dataWithContentsOfURL:location];
NSString* newStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSData *data = [newStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *jsonError;
if (jsonError) {
NSLog(#"JSON Error %#", [jsonError localizedDescription]);
}
NSArray * jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError];
Which is parsing the following JSON String :
{"author":"Oli Riman","content":"The museum shows us a view of pre-WWI society that includes doubts, fears, political protests etc. through newspaper cartoons of the time. Really interesting for adults who enjoy history. I wouldn't suggest this for kids who haven't studied WWI history or who don't read easily.","rating":"5","placeId":"40","date":"29-June-2015","reviewId":"9905A52D-76B2-4D42-8CA8-9158225C0D07"}
However I am getting a strange error code of :
domain: (null) - code: 0
Can anyone advise on what is causing this ?
Just tested the code on my simulator. Its working. You need to check if you are getting data from server or not.
If you want to test the parsing thing, you can do one thing-
Just store the data in json file and save it in the app bundle lets say file is "data.json"
and call below method, you will get data for sure.
- (void)readJsonData {
NSString *path = [[NSBundle mainBundle] pathForResource:#"data" ofType:#"json"];
NSURL *url = [NSURL fileURLWithPath:path];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData: data
options: NSJSONReadingAllowFragments
error: &error];
NSLog(#"Parsed json data- %#", dict);
}
This question already has an answer here:
Parsing JSON response .
(1 answer)
Closed 8 years ago.
Hai I need to get the id & status from the service for login my code is below. please guide me to get the values.. Thanks in advance..
NSString *Username= txtUsername.text;
NSString *Password=txtPassword.text;
NSString *link = [NSString stringWithFormat:#"http://www.xxx/login.php?user=%#&pass=%#&format=json",Username,Password];
NSURL *url=[NSURL URLWithString:link];
NSData *data=[NSData dataWithContentsOfURL:url];
1st Do the jSon parsing and then get the particular value from the
key .
Before getting any value , we have to understand the tree of jSon.
Here "posts" is an NSArray ,within that one DIctionary "post" is
there ,which again contains another dictionary.
Below is the complete code.
(void)viewDidLoad
{
[super viewDidLoad];
NSString *Username= txtUsername.text;
NSString *Password=txtPassword.text;
NSString *link =
[NSString stringWithFormat:#"http://www.some.com/webservice/login.php?user=%#&pass=%#&format=json",Username,Password];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:#selector(fetchedData:)
withObject:data waitUntilDone:YES];
}); }
Then call that selector fetchedData
(void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
if(!error){
NSArray* postArray = [json objectForKey:#“posts”]; //This is an array
if (postArray.count>0) {
NSDictionary *dict = [[postArray objectAtIndex:0] objectForKey:#"post" ];
NSString *id_ = [dict objectForKey:#"id"];
NSString *status_ = [dict objectForKey:#"status"];
}
}
}
Can you post your json string. You can use NSJSONSERIALISATION to convert data (json string ) into NSDictionary. Then use the keys to extract the values. I'm replying through mobile so I can't write the actual code.
Use Below code to parse Json in IOS
NSString *Username= txtUsername.text;
NSString *Password=txtPassword.text;
NSString *link = [NSString stringWithFormat:#"http://www.some.com/_webservice/login.php?user=%#&pass=%#&format=json",Username,Password];
NSURL *url=[NSURL URLWithString:link];
NSMutableURLRequest *req1 = [NSMutableURLRequest requestWithURL:url];
NSURLResponse *response;
NSError *error;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:req1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSLog(#"basavaraj \n\n\n %# \n\n\n",responseString);
NSData* data = [responseString dataUsingEncoding:NSUTF8StringEncoding];
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&myError];
NSString *id=[res objectForKey:#"ID"];
NSString *status=[res objectForKey:#"Status"];
and if u need extra info please go through below link it may help you
Click here for more details
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];
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];