解析 JSON 到网络视图 Android

Parse JSON to webview Android

我通过 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 文件?

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

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.parse

将 JSON 文本转换为 JavaScript 对象
var obj = JSON.parse(jsontext);