-2

I need to grab JSON from a WKWebView. Without creating extra classes and structs for the json payload, I am not parsing it, how can I do this?

I looked at a lot of posts online about wkwebview / javascript, and while informative, often they're more complex than I need.

Basically all I am doing is grabbing the json payload. And saving that payload as a string value I can use wherever. Thats all.

But I just can't seem to get my head around it. Lets say we don't know what the payload will be and for our part we don't care. We just store it in a string like "data" .

mikalooch
  • 91
  • 1
  • 8
  • If you do not show any of the code you have tried people will be reluctant to help. Please edit your question and add some relevant code pieces that display what you are trying to do. – Patru Apr 10 '19 at 01:22
  • Related and answer: https://stackoverflow.com/questions/56655204/get-json-text-from-wkwebview – twodayslate Jun 19 '19 at 00:39

1 Answers1

-1

If your whole data from the WebView is the JSON, you're better off doing a NSURLRequest to get that JSON, but I'm assuming you want to get the JSON that is inside some element in a webpage, in that case you can do this:

webView.evaluateJavaScript("document.getElementById('jsonContainer').innerHTML") { (value, error) in
    print(value)
}

In the example above you're getting the contents of an element on that page with the jsonContainer id.

RomuloVitoi
  • 117
  • 5