在UIWebView中加载JSON中的URL?

时间:2014-09-02 10:43:55

标签: 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",
    },

    {
      "name": "food.com",
      "url": "http:\/\/www.trial.com\/food\/",

      "price": "1195",
    },

当用户点击“购买”按钮时,我需要在单独的UIWebView中打开指定网站的网址。这是我当前代码的片段:

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。 。

5 个答案:

答案 0 :(得分:0)

使用保存所有链接的Json元素,让我们说你的JsonElement被调用

  

位置

代码应该是这样的:

NSURL *nsurl=[NSURL URLWithString:Location.url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];

答案 1 :(得分:0)

您可以修改以下代码

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

希望它可以帮助你......!

答案 2 :(得分:0)

这样做

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

答案 3 :(得分:-1)

你可以试试这个

创建一个自定义方法以将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;
}

现在可以随意调用此方法, 喜欢

 NSArray *  sitesArray = [self getDataDictionaryFromJsonFile:@"Sites"];
 NSURL * url = [NSURL urlFromString:[[sitesArray objectAtIndex:0] objectForKey:@"url"]];
 NSLog(@"lovely URL:%@",[[sitesArray objectAtIndex:0] objectForKey:@"url"]);

答案 4 :(得分:-1)

最简单的方法是使用AFNetworking库here 将AFNetworking添加到您的项目并导入AFNetworking.h并创建一个属性来存储来自JSON的URL

 //in .h file 
 @property (strong, nonatomic) NSString *urlFromJSON;

  //in .m file
  NSURL *url = [[NSURL alloc] initWithString:@"YOUR_JSON_URL_HERE"];
  NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

  AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

           //You can reach the the url using:
           self.urlFromJSON = JSON["url"]; //the key of the url in the JSON


     } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id      JSON) {
    NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
 }];

 [operation start];

祝你好运;)