【问题标题】:Parse JSON to webview Android将 JSON 解析为 webview Android
【发布时间】:2015-05-17 00:26:03
【问题描述】:

我通过php从mysql中检索数据并保存为String

 read= new BufferedReader(new InputStreamReader(s,"iso-8859-1"),8);
 String line=null;
 while ((line=read.readLine()) !=null)
 {
     text+=line+"\n";
 }

输出是

[{"DATE_OF_TEXT":"2015-05-06","total_rate_FORuser":"9.90000"},
 {"DATE_OF_TEXT":"2015-05-30","total_rate_FORuser":"5.10000"}]

如何在 webview 中将 JSON 解析为我的 Javascript 文件?

【问题讨论】:

    标签: android webview


    【解决方案1】:

    要在页面加载后传入字符串,可以使用

    String jsText = "javascript:addData('" + text + "');");
    webView.loadUrl(jsText);
    

    编辑:在您的情况下,使用字符串修改 url:

    String urlWithData = yourUrl + "?data=" + text;
    webView.loadUrl(urlWithData);
    

    然后在javascript中,首先使用window.location.search获取文本

    var jsontext = window.location.search.substring(1).split('=')[1];
    

    然后,将 JSON 文本转换为 JavaScript 对象,使用 JSON.parse

    var obj = JSON.parse(jsontext);
    

    【讨论】:

    • 我如何将其解析为我的 javascript 文件
    • 根据您的用例,有不同的方法可以实现这一点。我已经用一种方法更新了我的答案。
    • 我把这个网址放在哪里“file:///android_asset/example.html”
    • 哦。我明白你在做什么。给我一点时间来更新答案。
    • 感谢您的回复,但我还有另一个问题我的数据 = [{"DATE_OF_TEXT":"2015-05-06","total_rate_FORuser":"9.90000"}] 但以这种方式显示 [ {%22DATE_OF_TEXT%22:%222015-05-06%22,%22total_rate_FORuser%22:%229.90000%22}}你有什么解决办法吗?
    猜你喜欢
    • 2021-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多