bixby - 在 Bixby javascript 函数中如何在从服务器接收后操作 JSON
问题描述
关于 Javascript 函数的工作原理,有一些非常基本的事情我不明白,这阻碍了我。在此脚本中,我从 api 服务器检索 JSON。我需要稍微修改一下 JSON,使其符合 Bixby 中的数据类型模型。
var http = require('http');
var console = require('console')
const baseURL = 'https://www.altbrains.com/api/news/impeachmentsage';
exports.function = function getNews () {
let options = {
format: 'json',
headers: {
'accept': 'application/json'
},
cacheTime: 0
};
var response = http.getUrl(baseURL, options);
news = response
image_url = news.image
console.log("image url", image_url)
var modify;
modify = {
url: image_url
}
console.log('modify', modify)
news.image = modify
console.log ('news image', news.image)
return response;
}
response 的值正确报告为:
[{"image":"https://www.altbrains.com/images/48936529373_71ff9e0e13_o.jpg","tags":"news","text":"Trump's ambassador to the EU testified on Nov. 20 that 'Yes, there was a quid pro quo.'","title":"QPQ = YES"},{"image":"https://www.altbrains.com/images/48936529373_71ff9e0e13_o.jpg","tags":["news","schedule"],"text":"The next impeachment hearings by the House Permanent Select Committee on Intelligence will be held on Dec. 3. Enjoy a peaceful Thanksgiving!","title":"What's Next"}]
日志报告 news.image 未定义,这使我无法将其修改为 Bixby 所需的格式,该格式具有“{url:”插槽。为什么?
解决方案
响应是一个数组,里面有一个对象。尝试news = response[0]
推荐阅读
- angular - 如何使用 apollo 客户端库创建带有角度参数的 graphql 查询
- android - 如何在Android中使用滚动视图而不滞后
- laravel - 如何使用 Facebook SDK 在我自己的 Facebook 页面上发帖?
- java - 在 Spring Boot 查询注释中使用 Date 时出现 JSONParseException
- centos7 - 有没有其他方法可以在 Centos 中安装 libxml2-devel 库?
- angular - 如何绕过 Angular 6 中的弹出窗口拦截器?
- android - notifyDataSetChanged() 导致其他 recyclerview 滚动重置
- mysql - 如何找到具有更多相似标签的项目?
- google-play - Google Play 显示与应用页面和常见趋势列表不同的趋势位置
- javascript - 从 React 项目中提取架构