首页 > 解决方案 > 在 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:”插槽。为什么?

标签: bixby

解决方案


响应是一个数组,里面有一个对象。尝试news = response[0]


推荐阅读