How to parse JSON response to show Image and Webview. - ios

I have the JSON Response in this format:
{“ContentPage”: {“ContentImage”: “avdsfg.png”, “Content”: “have html data ” } }
How do I parse the response so as to get the Image and Content for the Imageview and webView respectively in iOS. Guidance needed. A Beginner in iOS.
Thanks.

Try doing something like this. This is not the exact solution and you'll have to work around your way.
NSString *link = [NSString stringWithFormat:#"yourServiceURL"];
NSString *encdLink = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:encdLink];
NSData *response = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:response options: NSJSONReadingMutableContainers error: &error];
Now you have your data in the array and extract it out using objectForKey or valueForKey.
This is what I am doing to fetch images from JSON and showing in my app. Firstly get the array from the above code. Then extract out the image content like this :
_ImageArray = [jsonArray valueForKey:#"ContentImage"];
Now you have the image name in your ImageArray.Since you have only imageName,you'll have to prepare the image URL in your code. Now to load the image in the ImageView, you need to do something like this.
NSURL *imageurl = [NSURL URLWithString:[_ImageArray objectAtIndex:indexPath.item]];
ImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageurl]];
This is for image. You can do it for the webview also. Some changes will be required as its a Webview. But it'll be done.

Related

Store JSON text output in NSString

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.

Json crash when sending space and dot

I am trying to request a URL and I am getting error saying
"Data parameter is nil".
I had found that the variables used has got space 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 crashing?
NSURL *url = [[NSURL alloc]initWithString:[NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]];
NSError *errors;
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *json = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&errors];
Try using NSUTF8StringEncoding
NSString *myUnencodedString = [NSString stringWithFormat:#"192.168.1.5/mobileapp?/signin=%#&%#",username,password]
NSString *encodedString = [myUnencodedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL = [[NSURL alloc] initWithString:encodedString]

How to Cache NSDATA json in iOS

I am using this code to fetch application screenshots of an application from appStore using only appID,
NSString *numericIDStr = appID;
NSString *urlStr = [NSString stringWithFormat:#"http://itunes.apple.com/lookup?id=%#", numericIDStr];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *json = [NSData dataWithContentsOfURL:url];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:json options:0 error:NULL];
NSArray *results = [dict objectForKey:#"results"];
NSDictionary *result = [results objectAtIndex:0];
myArray = [result objectForKey:#"screenshotUrls"];
I managed to cache screenshots by using SDWebImage and it works perfect, but I noticed that every time that I call this method it delays about 3-4 seconds, and on 3G its even more, about 10-15 seconds.
and other issue that my app crash when not connected to the internet, is there any way to kinda cache this method or something similar?
You can save your response(cache) using the following libraries on disk or in memory.
TMCache
or EGOCache
Use AFNetworking. It will take one line of code and do the caching for you.
[yourImage setImageWithURL:yourNSUrl];
That's it! It does all the caching for you.

How to request url which has space and dot in ios?

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];

Getting an image fails to create NSURL

Im trying to get an image from a url link.
NSURL *thmbnailUrl = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]];
NSLog(#"thmbnail:%#",[NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]]);
NSData *data = [NSData dataWithContentsOfURL:thmbnailUrl];
NSLog(#"movimgdata:%#",data);
UIImage *img = [[UIImage alloc]initWithData:data];
NSLog(#"movimg:%#",data);
NSData *imgData = UIImagePNGRepresentation(img);
when i log thmbnail i get this "ipofserverinnetwork/fishtune/movie/Our Logo 480p back.png"
Note:i didn't put the original ip.
I get null when i log data and of course img will also be null. I really dont know why I'm getting null. because when I tried entering the url in a browser it displays the image. I used the same code in another part of my app and it works but when i used it in this it fails. Is it because of the spaces between "Our_Logo_480p"?
you might need to use
NSString* escapedUrl = [yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
yes it's because of spaces, use
NSString *myUrl = [NSString stringWithFormat:#"http://%#:800/fishtune/movie/%#.png",[[DBHelper sharedInstance] currentHostIP],[arr objectAtIndex:0]];
NSString *url = [myUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Resources