从EvaluationJavacript访问数据

时间:2018-08-18 02:59:56

标签: json swift wkwebview

我已经从链接中解析了html,并访问了某个脚本标签。我已经能够使用WKWebView成功地在valuateJavascript函数上获得结果,但是很难访问每个值。看起来它返回JSON,但无法使用SwiftyJSON访问项目。关于如何访问每个值的任何想法,例如“ display_url”。到目前为止,这是我的代码:

let webView = WKWebView(frame: CGRect.zero)
                self.view.addSubview(webView)

                webView.loadHTMLString(item.html, baseURL: nil)

                while webView.isLoading {
                    CFRunLoopRunInMode(CFRunLoopMode.defaultMode, 0.1, false)
                }

                webView.evaluateJavaScript("[window._sharedData]") { (result, error) in
                    if (error != nil) {
                        print("Error: \(String(describing: error))")
                    } else {
                        print(result.debugDescription)

                    }
                }

结果显示如下:

{
"activity_counts" = "<null>";
"bundle_variant" = base;
config =     {
    "csrf_token" = INiUp4ZeYy1clJhH2Mjud6Sh5ZAUtVzl;
    viewer = "<null>";
};
"country_code" = US;
"entry_data" =     {
    PostPage =         (
                    {
            graphql =                 {
                "shortcode_media" =                     {
                    "__typename" = GraphSidecar;
                    "caption_is_edited" = 0;
                    "comments_disabled" = 0;
                    dimensions =                         {
                        height = 1080;
                        width = 1080;
                    };
                    "display_resources" =                         (
                                                    {
                            "config_height" = 640;
                            "config_width" = 640;
                            src = "https://scontent-mia3-2.cdninstagram.com/vp/8501e2ba82c589ed924dfc8013087924/5BF22BE0/t51.2885-15/sh0.08/e35/s640x640/38298826_204373327100142_7852885326031749120_n.jpg";
                        },
                                                    {
                            "config_height" = 750;
                            "config_width" = 750;
                            src = "https://scontent-mia3-2.cdninstagram.com/vp/9c28806d6762f497a62b40b3f37b4d0c/5C1488E0/t51.2885-15/sh0.08/e35/s750x750/38298826_204373327100142_7852885326031749120_n.jpg";
                        },
                                                    {
                            "config_height" = 1080;
                            "config_width" = 1080;
                            src = "https://scontent-mia3-2.cdninstagram.com/vp/24a1bba482b5abf8fa62756d0dcc6c75/5C0B4D05/t51.2885-15/e35/38298826_204373327100142_7852885326031749120_n.jpg";
                        }
                    );
                    "display_url" = "https://scontent-mia3-2.cdninstagram.com/vp/24a1bba482b5abf8fa62756d0dcc6c75/5C0B4D05/t51.2885-15/e35/38298826_204373327100142_7852885326031749120_n.jpg";
                    "edge_media_preview_like" =                         {
                        count = 3323;
                        edges =                             (
                        );
                    };

0 个答案:

没有答案