javascript - 如何从 JSON 对象访问数据?
问题描述
在我的 react-native-webview 的 onMessage 道具中,我正在接收本机事件,如下所示
{"nativeEvent": {"canGoBack": true, "canGoForward": false, "data": "{\"url\":\"some_url"}", "loading": false, "target": 313, "title": "Doctors Platform", "url": "some_url"}}
我想访问数据对象中的 url 值,知道怎么做吗?
我的代码:
<WebView
source={{
uri: uri,
}}
renderLoading={renderLoadingView}
javaScriptEnabled={true}
domStorageEnabled={true}
startInLoadingState={true}
// onShouldStartLoadWithRequest={onShouldStartLoadWithRequest} //ios
onNavigationStateChange={onShouldStartLoadWithRequest} //android
ref={webviewRef}
injectedJavaScript={jsCode}
onMessage={(event) => {
let post = JSON.stringify(event.nativeEvent.data);
console.log(event)
console.log('post',post.message)
}}
/>
任何建议都会很棒,请让我知道为了更好地理解需要任何东西。
解决方案
如果您JSON.parse
在 data 属性上使用,那么使用.url
它将返回您想要的结果。
const myevent = {"nativeEvent": {"canGoBack": true, "canGoForward": false, "data": "{\"url\":\"some_url\"}", "loading": false, "target": 313, "title": "Doctors Platform", "url": "some_url"}};
console.log(JSON.parse(myevent.nativeEvent.data).url);
推荐阅读
- python - 是否可以使用 Vaex 进行 read_sql 查询?
- html - 如何使用 VBA 从没有 ID 的 htmldiv 元素中提取字符串
- r - 使用 DT::datatable,如何自动“单击”页面按钮
- c# - C# EPPlus:如何重新排序 Excel 文件中的工作表位置
- youtube-api - Webhooks/PubSubHubbub 用于其他事件,例如超级聊天消息?
- ios - 在 Gmail iOS 应用程序中单击链接时强制打开默认浏览器
- read-the-docs - 网格表不自动换行
- python - 如何使用 python requests.get “跳过”第一个响应
- webpack - 将 webpack 4 更新为 webpack 5 获取错误选项具有未知属性“内联”
- flutter - 如何在颤动的sqlite中保存颜色