获取json格式的数据并显示在webview上

这段代码展示了如何从指定URL获取JSON数据,解析后提取特定字段,并将内容加载到WebView中显示。如果网络加载失败,则尝试从本地获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    NSURL *url = [NSURLURLWithString:@"http://cst.stu.126.net/u/json/cms/ykt_protocol.json"];

   NSData *responseData = [NSDatadataWithContentsOfURL:url];

   NSError *jsonError;

   id result = [NSJSONSerializationJSONObjectWithData:responseData options:kNilOptions error:&jsonError];

   NSString *protocolStr = [result valueForKey:@"protocol"];

    [self.protocolWebviewloadHTMLString:protocolStr baseURL:nil];




//    if (!self.loadingMaskView.hidden) {

//        [self.loadingMaskView startLoadingWithTip:@"努力载入中..."];

//    }

//    NSURL *url = [NSURL URLWithString:@"http://cst.stu.126.net/u/json/cms/ykt_protocol.json"];

//    NSData *responseData = [NSData dataWithContentsOfURL:url];

//    NSError *jsonError;

//    id result = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&jsonError];

//    NSString *protocolStr = [result valueForKey:@"protocol"];

//    

//    if(protocolStr){

//        [self.loadingMaskView stopLoadingSuccessfully];

//        //将协议显示到webView

//        //加载URL

//        [self.protocolWebview loadHTMLString:protocolStr baseURL:nil];

//    }

//    else{

//        [self.loadingMaskView stopLoadingFailedWithTip:@"点击屏幕,重新加载" reloadable:YES];

//    }

    

    

//    

//    if (!self.loadingMaskView.hidden) {

//        [self.loadingMaskView startLoadingWithTip:@"努力载入中..."];

//    }

//    NSString *protocolStr = [EduSystem sharedInstance].protocol;

//    if(protocolStr){

//        [self.loadingMaskView stopLoadingSuccessfully];

//        //将协议显示到webView

//        //加载URL

//        [self.protocolWebview loadHTMLString:protocolStr baseURL:nil];

//    }

//    else{

//        [self.loadingMaskView stopLoadingFailedWithTip:@"点击屏幕,重新加载" reloadable:YES];

//    }

    


    

//    //先从网络上拿数据,如果有网络,显示网络版,否则显示本地版本协议

//    NSURL *url = [NSURL URLWithString:@"http://cst.stu.126.net/u/json/cms/ykt_protocol.json"];

//    NSData *responseData = [NSData dataWithContentsOfURL:url];

//    NSError *jsonError;

//    id result = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&jsonError];

//    NSString *protocolStr = [result valueForKey:@"protocol"];

//

//    if(!protocolStr){

//        //网络未获取到

//        Protocol *protocol = [Protocol getProtocolWithType:1];

//        protocolStr = [protocol valueForKey:@"content"];

//    }

//        [self.protocolWebview loadHTMLString:protocolStr baseURL:nil];

    



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值