Not Able to Parse JSON Data in WebView

1.3k views Asked by At

i am Newbie in iOS Development.i want to load only "description" Key Data in to UIWebView From my Url Here my Url like as

http://www.janvajevu.com/webservice/specific_post.php?post_id=2885

and i parsed Data For it like as

-(void)viewWillAppear:(BOOL)animated
{
[self.webSpinner startAnimating];
NSURL * url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/specific_post.php?post_id=2885"]];
dispatch_async(kBgQueue, ^{
    data = [NSData dataWithContentsOfURL: url];
    [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
});
}
-(void)fetchedData:(NSData *)responsedata
{
if (responsedata.length > 0)
{
    NSError* error;
    self.webDictionary= [NSJSONSerialization JSONObjectWithData:responsedata options:kNilOptions error:&error];
    self.webArray=[_webDictionary objectForKey:@"data"];
}
self.webViewString=[self.webArray valueForKey:@"description"];
NSString *descriptionString=[NSString stringWithFormat:@"%@",self.webViewString];
[self.webView loadHTMLString:descriptionString baseURL:nil];

then it print Data in WebView Like as

"<img style='width: 100% !important; height: auto; margin: 30px 20px 30px 20px;' class=\"alignnone size-full wp-image-2967\" src=\"http://www.janvajevu.com/wp-content/uploads/2014/12/Amazing-talented-people-in-the-world.jpg\" alt=\"Amazing talented people in the world\" width=\"0\" height=\"0\" />\U0a9c\U0ac1\U0a93 \U0aa6\U0ac1\U0aa8\U0abf\U0aaf\U0abe \U0aa8\U0abe\U0a85\U0a9c\U0aac \U0a97\U0a9c\U0aac \U0a9f\U0ac7\U0ab2\U0ac7\U0a82\U0a9f\U0ac7\U0aa1 \U0ab2\U0acb\U0a95\U0acb<br />
\n<br />
\nhttps://www.youtube.com/watch?v=Vo0Cazxj_yc"

And When i Encoded Them like as

NSData *webViewDatastring = [descriptionString dataUsingEncoding:[NSString defaultCStringEncoding]];
NSString *webUnicodeString = [[NSString alloc] initWithData:webViewDatastring encoding:NSNonLossyASCIIStringEncoding];
NSString *webViewString=[webUnicodeString stringByTrimmingCharactersInSet:charsToTrim];

and Load them in to WebView like as

[self.webView loadHTMLString:webViewString baseURL:nil];

then it Return Null. So I confuse about that how to encode This HTML Data then it is Shown in to UIWebView please Give me Solution For that else Give me Resource for this type of tutorial.

1

There are 1 answers

3
iphonic On BEST ANSWER

I am simply doing the following process its working for me directly.

NSData *jsonData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.janvajevu.com/webservice/specific_post.php?post_id=2885"]];
NSJSONSerialization *jsonParser=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];

NSDictionary *data=[[(NSDictionary *)jsonParser objectForKey:@"data"] lastObject];

[self.webView loadHTMLString:[data objectForKey:@"description"] baseURL:nil];

See output

enter image description here

Hope it helps.