Ios 在UIWebView中加载JSON中的URL?
我的JSON如下所示:Ios 在UIWebView中加载JSON中的URL?,ios,objective-c,json,url,uiwebview,Ios,Objective C,Json,Url,Uiwebview,我的JSON如下所示: { "sites": [ { "name": "lovely.com", "url": "http:\/\/www.trial.com\/lovely\/", "price": "1795", }, { "name": "great.com", "url": "http:\/\/www.trial.com\/great\/", "price": "1730", },
{
"sites": [
{
"name": "lovely.com",
"url": "http:\/\/www.trial.com\/lovely\/",
"price": "1795",
},
{
"name": "great.com",
"url": "http:\/\/www.trial.com\/great\/",
"price": "1730",
},
{
"name": "food.com",
"url": "http:\/\/www.trial.com\/food\/",
"price": "1195",
},
NSURL *nsurl=[NSURL URLWithString:Location.url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
当用户单击“购买”按钮时,我需要在单独的UIWebView中打开指定站点的url。以下是我当前代码的一个片段:
ViewController.m
-(void)buyBttnPressed:(id)sender{
UIWebView *buyView = [[UIWebView alloc] initWithFrame:CGRectMake(20,132,280,368)];
buyView.backgroundColor = [UIColor whiteColor];
buyView.scalesPageToFit = YES;
buyView.delegate = self;
[self.view addSubview:buyView];
// [buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
}
我已经解析了JSON文件。我只是想弄清楚如何使用url对象键在WebView中打开url 使用保存所有链接的Json元素,假设调用了JsonElement 位置 代码应该是这样的:
{
"sites": [
{
"name": "lovely.com",
"url": "http:\/\/www.trial.com\/lovely\/",
"price": "1795",
},
{
"name": "great.com",
"url": "http:\/\/www.trial.com\/great\/",
"price": "1730",
},
{
"name": "food.com",
"url": "http:\/\/www.trial.com\/food\/",
"price": "1195",
},
NSURL *nsurl=[NSURL URLWithString:Location.url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
您可以按如下方式修改代码
NSString *urlString=[NSString stringWithFormat:@"%@",url];
[buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstring]]];
////(OR) If are using NSObject say Location to store the details you can refer below code
UIButton *btn=(UIButton *)sender;
Location *locObj=[self.dataArray objectAtIndex:btn.tag];
NSString *urlString=[NSString stringWithFormat:@"%@",locObj.url];
[buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlstring]]];
int i=-1;
for(NSDictionary *dict in dataarray)
{
i++;
btn.tag=i;
}
希望它能帮助你 这样做
NSError *error;
NSJSONSerialization *jsonData = [NSJSONSerialization JSONObjectWithData:parseData options:NSJSONReadingMutableContainers error:&error];
NSString *strURL = [[[jsonData valueForKey:@"sites"]objectAtIndex:selectedIndex]valueForKey:@"url"];
[buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]]];
你可以试试这个
创建一个自定义方法以获取json数据作为数组
-(NSArray *)getDataDictionaryFromJsonFile:(NSString *)jsonFileName
{
NSData *fileContents = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:jsonFileName ofType:@"json"]];
NSError *error;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:fileContents options:kNilOptions error:&error];
NSArray * array = [dict objectForKey:@"sites"];
return array;
}
现在,在任何需要的地方调用此方法,
像
最简单的方法是使用AFNetworking库 将AFNetworking添加到您的项目中,导入AFNetworking.h并创建一个属性来存储来自JSON的url
//in .h file
@property (strong, nonatomic) NSString *urlFromJSON;
祝你好运;) 那么调用loadRequest时会发生什么呢?NSString*webUrl=[[arrParsedData valueForKey:@“sites”]valueForKey:@“url”],使用这个u可以从json数据中获取url@你知道如何打开网址http://www.trial.com/food/?如果是,URL只是JSON数据中的NSString。如果没有,这与JSON无关。您是否考虑过查看UIWebView的文档?谢谢@Vidhyanand900!这对我帮助很大!唯一的问题是,当我打开webview时,它每次都会打开相同的url。当我查看控制台时,我看到它一直打开的url位于索引0处,这一定意味着标记(在btw.tag中)是0。如何修复此问题?。而不是btn.tag..您需要传递数组的索引,如0、1或2。根据您的要求..在tableview或任何其他对象中显示已解析内容的位置..?已解析内容将显示在单独的UIView(如卡片)上。一次在一个数组中加载/解析的卡总数为75张。您正在显示所有已解析的数据或仅显示选定的一个解析数据(单个记录)?一次在一个数组中加载/解析75张卡。