『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1358|回复: 3

[已解决] node red 中javascript遍历json 从该数组中提取对象用于 tts 小爱...

[复制链接]

6

主题

88

帖子

467

积分

中级会员

Rank: 3Rank: 3

积分
467
金钱
379
HASS币
0
发表于 2023-9-6 23:51:20 | 显示全部楼层 |阅读模式
本帖最后由 敬轩 于 2023-9-7 22:59 编辑

在Node-RED中,您可以使用Function节点来编写自定义JavaScript函数以处理传入的数据。一个JSON对象和一个数组,想要从该数组中提取诗句。
111.png 222.png
上图两种都没成功提取出来,求教大佬指教
回复

使用道具 举报

13

主题

619

帖子

3207

积分

论坛元老

HomeAssistant

Rank: 8Rank: 8

积分
3207
金钱
2583
HASS币
20
发表于 2023-9-7 08:36:38 | 显示全部楼层
把Json转换成字符串,再去读取调用payload.data.origin.content,做TTS的话,每句话的双引号应该不影响
微信图片_20230907083246.png

回复

使用道具 举报

17

主题

255

帖子

1877

积分

金牌会员

Rank: 6Rank: 6

积分
1877
金钱
1622
HASS币
0
发表于 2023-9-7 15:40:03 | 显示全部楼层
学到了
回复

使用道具 举报

6

主题

88

帖子

467

积分

中级会员

Rank: 3Rank: 3

积分
467
金钱
379
HASS币
0
 楼主| 发表于 2023-9-7 22:59:19 | 显示全部楼层
本帖最后由 敬轩 于 2023-9-7 23:02 编辑
DDDear 发表于 2023-9-7 08:36
把Json转换成字符串,再去读取调用payload.data.origin.content,做TTS的话,每句话的双引号应该不影响
[{"id":"96b65df3837820d9","type":"function","z":"51be17f36b61d565","name":"获取日期和时间","func":"      let date = new Date();\n      let year = date.getFullYear();\n      let month = date.getMonth() + 1;\n      let day = date.getDate();\n      let hour = date.getHours();\n      let minute = date.getMinutes();\n      let second = date.getSeconds();\nmonth = month >= 10 ? month : "0" + month;\nday = day >= 10 ? day : "0" + day;\nhour = hour >= 10 ? hour : "0" + hour;\nminute = minute >= 10 ? minute : "0" + minute;\nsecond = second >= 10 ? second : "0" + second;\nmsg.payloadtime =year + '年' + month + '月' + day + '日' + hour+'点'+minute+'分';\n      \n\nreturn msg;\n// + "\\r"","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":1840,"wires":[["3d5a19f88a4602ef"]]},{"id":"114a951a94be258f","type":"change","z":"51be17f36b61d565","name":"提取参数","rules":[{"t":"set","p":"payload1","pt":"msg","to":"payload.data.content","tot":"jsonata"},{"t":"set","p":"payload2","pt":"msg","to":"payload.data.origin.title","tot":"jsonata"},{"t":"set","p":"payload3","pt":"msg","to":"payload.data.origin.dynasty","tot":"jsonata"},{"t":"set","p":"payload4","pt":"msg","to":"payload.data.origin.author","tot":"jsonata"},{"t":"set","p":"payload5","pt":"msg","to":"payload.data.origin.content","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":1840,"wires":[["96b65df3837820d9"]]},{"id":"9b926b570c587655","type":"json","z":"51be17f36b61d565","name":"","property":"payload.data.origin.content","action":"","pretty":false,"x":330,"y":1840,"wires":[["114a951a94be258f"]]},{"id":"ea0c55272a72b2f9","type":"http request","z":"51be17f36b61d565","name":"古诗请求","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://v2.jinrishici.com/sentence","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Cookie","valueType":"other","valueValue":"X-User-Token=修改为上面网址在浏览器中运行查看"}],"x":200,"y":1840,"wires":[["9b926b570c587655"]]},{"id":"f98114d148b3cfce","type":"inject","z":"51be17f36b61d565","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":70,"y":1840,"wires":[["ea0c55272a72b2f9"]]},{"id":"36c40415fd9fa9ec","type":"debug","z":"51be17f36b61d565","name":"debug 254","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1010,"y":1820,"wires":[]},{"id":"3ddba62598bf57ea","type":"xiaoai-tts","z":"51be17f36b61d565","name":"","xiaoai":"","tts":"","device":"","x":990,"y":1880,"wires":[[],[]]},{"id":"3d5a19f88a4602ef","type":"function","z":"51be17f36b61d565","name":"播报内容","func":"var str1 = msg.payload1\nvar str2 = msg.payload2\nvar str3 = msg.payload3\nvar str4 = msg.payload4\nvar str5 = msg.payload5\nvar str6 = msg.payload6\nvar str7 = msg.payload7\nvar time = msg.payloadtime;\n\n\nmsg.payload = msg.payload = "早上好,现在是" + time + "   古诗经典句是   " + str1 + "   句子题目是  " + str2 + "    作者朝代是    " + str3 + "     古诗作者   " + str4 + "     完整诗句朗诵     " + str5 \nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":800,"y":1840,"wires":[["36c40415fd9fa9ec","3ddba62598bf57ea"]]}]

谢谢 444.png 333.png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-27 12:14 , Processed in 0.231320 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表