首页 > 解决方案 > 如何将 Javascript 对象传递给 Bixby 的操作输出?

问题描述

我正在构建一个搜索动漫报价的应用程序。所以,我有以下数据,它有 331 个对象(我称之为“报价卡”),因此随着我添加越来越多的报价,它也会在将来更新。我遇到的问题是为 JS 的数组项(例如关键字和 imageTag)创建概念。以及作为属性列出的字符名称。只要我能保留类别和关键字数组项,我也愿意更改输出。这些是我的报价卡所必需的

[
  {
    $id: "Cold_Souls_1",
    animeTitle: "Fullmetal Alchemist Brotherhood",
    animePoster:{
      referenceImage: 'https://qph.fs.quoracdn.net/main-qimg-da58c837c7197acf364cb2ada34fc5fb.webp',
      imageTags: ["Grey","Yellow","Blue","Metal Body","Machine", "Robot","Yellow Hair Boy"],
    },
    animeCharacters:{
      "Edward Elric": [
        {
          quote: "A lesson without pain is meaningless. For you cannot gain something without sacrificing something else in return. But once you have recovered it and made it your own... You will gain an irreplaceable Fullmetal heart.",
          keywords: ["lesson", "pain", "return", "meaningless", "gain","sacrificing", "recover"],
          category: "Life Lesson"
        }
      ]
    }
  },....................
]

标签: bixbybixbystudio

解决方案


在 Bixby 中,您将对表示 JSON 响应的结构进行建模。

structure (Anime) {
  description (The output of your action)

  property (title) {
    type(viv.core.Text)
    visibility (Private)
  }

  property (poster){
    type(AnimePoster)
    visibility (Private)
  }

  property (characters) {
    type (AnimeCharacter)
    max (Many)
    visibility (Private)
  }

}

structure (AnimePoster) {

  property (referenceImage) {
    type (viv.core.Text)
    visibility (Private)
  }

  property (imageTags) {
    type (viv.core.Text)
    max (Many)
    visibility (Private)
  }

}


structure (AnimeCharacter) {

  property (name) {
    type (viv.core.Text)
    visibility (Private)
  }

  property (quote) {
    type (viv.core.Text)
    visibility (Private)
  }

  property (keywords) {
    type (viv.core.Text)
    max (Many)
    visibility (Private)
  }

  property (category) {
    type (viv.core.Text)
    visibility (Private)
  }

}

在您的 javascript 文件中,您处理动漫的 JSON 结构

// listOfAnimes is the JSON  object described in the question

var animes = [];

listOfAnimes.forEach((anime) => {

    var characterNames = Object.keys(anime.animeCharacters);
    var characters = [];

      Object.keys(anime.animeCharacters).forEach((key) => {
           characters.push({
             name: key,
             quote: anime.animeCharacters[key][0].quote, // * warning, can be many
             category: anime.animeCharacters[key][0].category// * warning, can be many
           });
    });

    animes.push( {
       $id: anime.$id,
       title: anime.title,
       characters: characters,
       poster: {
         referenceImage: anime.animePoster.referenceImage,
         imageTags: anime.animePoster.imageTags
       },

    });

});

在此处输入图像描述


推荐阅读