在 Bixby javascript 函数中如何在从服务器接收后操作 JSON

in Bixby javascript function how to manipulate JSON after receving from server

关于 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;

}

响应值正确报告为:

[{"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]

我成功了:

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
  };

  response = http.getUrl(baseURL, options);

  news = response
 // news = response[0]
  console.log('news', news)
  news.forEach(function(item, index, array) {

  image_url = news[index].image
  console.log('news story', news[index])
  console.log('news image_url', news[index].image)

   modified_url = {
     url: image_url 
      }

   news[index].image = modified_url

});

  return news;
}