javascript - 如何从 url 中选择特定的 json 字符串以在 Web View 中显示?

标签 javascript android android-intent android-webview android-json

我从我的 javascript 文件中获取一个 json 字符串参数,如下所示:

http://local/action/?action=loadFile&params=%7B%22fileChart%22%3A%22url%22%2C%22item%22%3A%7B%22DocId%22%3A%2270078903%22%2C%22Headline%22%3A%22Alert%253A%2520AAPL%253A%2520QA%2520Test%22%2C%22PrimaryTickers%22%3A%22AAPL.O%22%2C%22ArrivalDate%22%3A%222015-04-29T08%3A04%3A40Z%22%2C%22fileType%22%3A%22url%22%2C%22secondaryFileType%22%3A%22pdf%22%2C%22secondaryFileExtension%22%3A%22pdf%22%2C%22curDate%22%3A1435589346483%2C%22Pages%22%3A8%2C%22url%22%3A%22https%253A%252F%252Fuat.citivelocity.com%252Frendition%252Feppublic%252FdocumentService%252FdXNlcl9pZD0mYWN0aW9uPXZpZXc%252FZmlsZV9uYW1lPVZTNkoucGRm%22%2C%22contributor%22%3A%22Citi%20-%20Linkback%20Test%22%7D%7D&requestType=GET&timestamp=1435589346484

我只想显示上述 url 的 url 部分,这样我就可以解码给定链接的 https 并仅在 webview loadurl 中显示所需的链接。在上述链接的情况下,它是:https://uat.citvelocity.com ..ETC , 目前我在网上搜索得到的想法是这样的:

webview.loadurl("javascript:(function() { "| "document.getElementByTagName('url')[0].style.display="+";"+"})()"); }
});

但这并没有按预期工作。我做错了吗?有什么想法吗?

最佳答案

由于您的字符串是 JSON,因此将该 JSON 解析为对象可能是个好主意,这样您就可以使用对象属性来获取您想要的任何内容。

var decoded = decodeURIComponent('http://local/action/?action=loadFile&params=%7B%22fileChart%22%3A%22url%22%2C%22item%22%3A%7B%22DocId%22%3A%2270078903%22%2C%22Headline%22%3A%22Alert%253A%2520AAPL%253A%2520QA%2520Test%22%2C%22PrimaryTickers%22%3A%22AAPL.O%22%2C%22ArrivalDate%22%3A%222015-04-29T08%3A04%3A40Z%22%2C%22fileType%22%3A%22url%22%2C%22secondaryFileType%22%3A%22pdf%22%2C%22secondaryFileExtension%22%3A%22pdf%22%2C%22curDate%22%3A1435589346483%2C%22Pages%22%3A8%2C%22url%22%3A%22https%253A%252F%252Fuat.citivelocity.com%252Frendition%252Feppublic%252FdocumentService%252FdXNlcl9pZD0mYWN0aW9uPXZpZXc%252FZmlsZV9uYW1lPVZTNkoucGRm%22%2C%22contributor%22%3A%22Citi%20-%20Linkback%20Test%22%7D%7D&requestType=GET&timestamp=1435589346484'),
    json = JSON.parse(decoded.slice(decoded.indexOf('&params=') + 8, decoded.indexOf('&requestType'))),
    url = decodeURIComponent(json.item.url);

关于javascript - 如何从 url 中选择特定的 json 字符串以在 Web View 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119677/

相关文章:

javascript - 在 Mongoose 的字段中定义一个函数作为默认值

javascript - ChatClient 中服务器关闭/重置后未触发错误事件

android - 如何监听不是 MenuItem 的 SearchView 的展开/折叠事件?

java - 在 Activity 之间发送数据 Intent 错误

javascript - 如何从其 Div id 获取 Ace Editor 对象

javascript - 如何使用 JavaScript 更改 <textarea> 的内容

android - 无法解析构造函数 'Intent(xxxx.xx.java.lang.thread.Class<com.practice.MainActivity>)'

android - 媒体查询以区分横向模式和桌面查看中的 Galaxy?

android - PendingIntent 完成当前的第 3 方 Activity 并启动其相应的 Activity

android - 动态更改 TextView 的样式和文本