我想使用JSON2HTML从JSON解析HTML数据和(使用雨燕3.0)呈现在一个UIWebView的。请让我知道如何实现它。提前致谢!如何从JSON解析HTML数据,呈现在UIWebView中(使用雨燕3.0)
这是我已经试过:
let jsfile1 = try!String(contentsOfFile: Bundle.main.path(forResource: "json2html", ofType: "js")!)
func loadJS()
{
var getData={}
var context = JSContext()
var valSwiftyJson:JSON = [:]
var test = context?.evaluateScript(jsfile1)
let testFunction = test?.objectForKeyedSubscript("json2html")
let urlString = //Have removed the URL string due to restrictions
Alamofire.request(urlString,encoding:JSONEncoding.default).responseJSON
{ response in
if let alamoJson = response.result.value
{
let swiftyJson = JSON(data:response.data!)
valSwiftyJson = swiftyJson["FormInfo"]["Form"]
print(valSwiftyJson)
}
}
let result = testFunction?.call(withArguments: [getData,valSwiftyJson])
webView.loadHTMLString((result?.toString())!, baseURL: nil)
}
最后,我设法通过创建一个index.html文件(本地存储)来解决这个问题,我提到里面的JSON2HTML库。每当我需要将JSON转换为HTML时,我都会将JSON(HTML内部)内容动态地添加到它。最后,我加载了UIWebView中的最终index.html(它像魅力一样工作)。
你能告诉我们你的代码到目前为止,你已经尽力了,更好地共享它在jsfiddle或 – user3775217
@ user3775217我已经添加了编码,因为您已经问 – user2842987