如何在Apple Automator中使用javascript正确地将字符串解析为JSON?

问题描述 投票:0回答:1

我正在尝试在 OSX 上使用 Automator 制作一个简单的家庭自动化工作流程。

首先使用 shell 脚本从 HTTP 请求获取 JSON。然后使用 JavaScript 修改 JSON,并通过 shell 脚本通过另一个 HTTP 请求发布。

我面临的问题是,特别是在自动机上,JSON.parse() 似乎无法正确解析接收到的字符串。只看 JSON.parse() 函数,这是输入:

"{\"accessories\":[{\"aid\":1,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"homebridge.io\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"homebridge\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"Homebridge 7168 8A14\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"0E:1E:A2:02:71:68\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.6.1\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"A2\",\"iid\":2000000008,\"characteristics\":[{\"type\":\"37\",\"iid\":9,\"value\":\"1.1.0\",\"perms\":[\"pr\"],\"description\":\"Version\",\"format\":\"string\",\"maxLen\":64}]}]},{\"aid\":6,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"ZigBee Permit Join\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"dstltuakzw\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.0.0\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"49\",\"iid\":8,\"characteristics\":[{\"type\":\"25\",\"iid\":9,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"}]}]},{\"aid\":7,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"None\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"ZigBee Touchlink\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"kaxqkaj8hj\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"1.0.0\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"}]},{\"type\":\"49\",\"iid\":8,\"characteristics\":[{\"type\":\"25\",\"iid\":9,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"}]}]},{\"aid\":8,\"services\":[{\"type\":\"3E\",\"iid\":1,\"characteristics\":[{\"type\":\"14\",\"iid\":2,\"perms\":[\"pw\"],\"description\":\"Identify\",\"format\":\"bool\"},{\"type\":\"20\",\"iid\":3,\"value\":\"Philips\",\"perms\":[\"pr\"],\"description\":\"Manufacturer\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"21\",\"iid\":4,\"value\":\"LLC020\",\"perms\":[\"pr\"],\"description\":\"Model\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"23\",\"iid\":5,\"value\":\"Hue Go\",\"perms\":[\"pr\"],\"description\":\"Name\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"30\",\"iid\":6,\"value\":\"0x00178801047a69e6\",\"perms\":[\"pr\"],\"description\":\"Serial Number\",\"format\":\"string\",\"maxLen\":64},{\"type\":\"52\",\"iid\":7,\"value\":\"2.5.10\",\"perms\":[\"pr\"],\"description\":\"Firmware Revision\",\"format\":\"string\"},{\"type\":\"54\",\"iid\":8,\"value\":\"5.127.1.26420\",\"perms\":[\"pr\"],\"description\":\"Software Revision\",\"format\":\"string\"},{\"type\":\"53\",\"iid\":9,\"value\":\"1\",\"perms\":[\"pr\"],\"description\":\"Hardware Revision\",\"format\":\"string\"}]},{\"type\":\"43\",\"iid\":10,\"characteristics\":[{\"type\":\"CE\",\"iid\":15,\"value\":445,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Color Temperature\",\"format\":\"int\",\"minValue\":140,\"maxValue\":500,\"minStep\":1},{\"type\":\"25\",\"iid\":11,\"value\":0,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"On\",\"format\":\"bool\"},{\"type\":\"13\",\"iid\":12,\"value\":31,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Hue\",\"format\":\"float\",\"unit\":\"arcdegrees\",\"minValue\":0,\"maxValue\":360,\"minStep\":1},{\"type\":\"2F\",\"iid\":13,\"value\":82,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Saturation\",\"format\":\"float\",\"unit\":\"percentage\",\"minValue\":0,\"maxValue\":100,\"minStep\":1},{\"type\":\"8\",\"iid\":14,\"value\":100,\"perms\":[\"ev\",\"pr\",\"pw\"],\"description\":\"Brightness\",\"format\":\"int\",\"unit\":\"percentage\",\"minValue\":0,\"maxValue\":100,\"minStep\":1}]}]}]}"

这是用

https://jsonbeautifier.org
美化的输入:

{
  "accessories": [
    {
      "aid": 1,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "homebridge.io",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "homebridge",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "Homebridge XXXX XXXX",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "xx:xx:xx:xx",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.6.1",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "A2",
          "iid": 2000000008,
          "characteristics": [
            {
              "type": "37",
              "iid": 9,
              "value": "1.1.0",
              "perms": [
                "pr"
              ],
              "description": "Version",
              "format": "string",
              "maxLen": 64
            }
          ]
        }
      ]
    },
    {
      "aid": 6,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "ZigBee Permit Join",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "dstltuakzw",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.0.0",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "49",
          "iid": 8,
          "characteristics": [
            {
              "type": "25",
              "iid": 9,
              "value": 0,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            }
          ]
        }
      ]
    },
    {
      "aid": 7,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "None",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "ZigBee Touchlink",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "kaxqkaj8hj",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "1.0.0",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "49",
          "iid": 8,
          "characteristics": [
            {
              "type": "25",
              "iid": 9,
              "value": 0,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            }
          ]
        }
      ]
    },
    {
      "aid": 8,
      "services": [
        {
          "type": "3E",
          "iid": 1,
          "characteristics": [
            {
              "type": "14",
              "iid": 2,
              "perms": [
                "pw"
              ],
              "description": "Identify",
              "format": "bool"
            },
            {
              "type": "20",
              "iid": 3,
              "value": "Philips",
              "perms": [
                "pr"
              ],
              "description": "Manufacturer",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "21",
              "iid": 4,
              "value": "LLC020",
              "perms": [
                "pr"
              ],
              "description": "Model",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "23",
              "iid": 5,
              "value": "Hue Go",
              "perms": [
                "pr"
              ],
              "description": "Name",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "30",
              "iid": 6,
              "value": "0x00178801047a69e6",
              "perms": [
                "pr"
              ],
              "description": "Serial Number",
              "format": "string",
              "maxLen": 64
            },
            {
              "type": "52",
              "iid": 7,
              "value": "2.5.10",
              "perms": [
                "pr"
              ],
              "description": "Firmware Revision",
              "format": "string"
            },
            {
              "type": "54",
              "iid": 8,
              "value": "5.127.1.26420",
              "perms": [
                "pr"
              ],
              "description": "Software Revision",
              "format": "string"
            },
            {
              "type": "53",
              "iid": 9,
              "value": "1",
              "perms": [
                "pr"
              ],
              "description": "Hardware Revision",
              "format": "string"
            }
          ]
        },
        {
          "type": "43",
          "iid": 10,
          "characteristics": [
            {
              "type": "CE",
              "iid": 15,
              "value": 500,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Color Temperature",
              "format": "int",
              "minValue": 140,
              "maxValue": 500,
              "minStep": 1
            },
            {
              "type": "25",
              "iid": 11,
              "value": 1,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "On",
              "format": "bool"
            },
            {
              "type": "13",
              "iid": 12,
              "value": 270,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Hue",
              "format": "float",
              "unit": "arcdegrees",
              "minValue": 0,
              "maxValue": 360,
              "minStep": 1
            },
            {
              "type": "2F",
              "iid": 13,
              "value": 100,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Saturation",
              "format": "float",
              "unit": "percentage",
              "minValue": 0,
              "maxValue": 100,
              "minStep": 1
            },
            {
              "type": "8",
              "iid": 14,
              "value": 10,
              "perms": [
                "ev",
                "pr",
                "pw"
              ],
              "description": "Brightness",
              "format": "int",
              "unit": "percentage",
              "minValue": 0,
              "maxValue": 100,
              "minStep": 1
            }
          ]
        }
      ]
    }
  ]
}

这是输出:

{{accessories:{{aid:1, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"homebridge.io", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"homebridge", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"Homebridge XXX XXXX", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"xx:xx:xx:xx", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.6.1", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"A2", iid:2.000000008E+9, characteristics:{{type:"37", iid:9, |value|:"1.1.0", perms:{"pr"}, description:"Version", format:"string", maxLen:64}}}}}, {aid:6, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"None", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"None", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"ZigBee Permit Join", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"dstltuakzw", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.0.0", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"49", iid:8, characteristics:{{type:"25", iid:9, |value|:0, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}}}}}, {aid:7, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"None", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"None", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"ZigBee Touchlink", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"kaxqkaj8hj", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"1.0.0", perms:{"pr"}, description:"Firmware Revision", format:"string"}}}, {type:"49", iid:8, characteristics:{{type:"25", iid:9, |value|:0, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}}}}}, {aid:8, services:{{type:"3E", iid:1, characteristics:{{type:"14", iid:2, perms:{"pw"}, description:"Identify", format:"bool"}, {type:"20", iid:3, |value|:"Philips", perms:{"pr"}, description:"Manufacturer", format:"string", maxLen:64}, {type:"21", iid:4, |value|:"LLC020", perms:{"pr"}, description:"Model", format:"string", maxLen:64}, {type:"23", iid:5, |value|:"Hue Go", perms:{"pr"}, description:"Name", format:"string", maxLen:64}, {type:"30", iid:6, |value|:"0x00178801047a69e6", perms:{"pr"}, description:"Serial Number", format:"string", maxLen:64}, {type:"52", iid:7, |value|:"2.5.10", perms:{"pr"}, description:"Firmware Revision", format:"string"}, {type:"54", iid:8, |value|:"5.127.1.26420", perms:{"pr"}, description:"Software Revision", format:"string"}, {type:"53", iid:9, |value|:"1", perms:{"pr"}, description:"Hardware Revision", format:"string"}}}, {type:"43", iid:10, characteristics:{{type:"CE", iid:15, |value|:500, perms:{"ev", "pr", "pw"}, description:"Color Temperature", format:"int", minValue:140, maxValue:500, minStep:1}, {type:"25", iid:11, |value|:1, perms:{"ev", "pr", "pw"}, description:"On", format:"bool"}, {type:"13", iid:12, |value|:270, perms:{"ev", "pr", "pw"}, description:"Hue", format:"float", unit:"arcdegrees", minValue:0, maxValue:360, minStep:1}, {type:"2F", iid:13, |value|:100, perms:{"ev", "pr", "pw"}, description:"Saturation", format:"float", unit:"percentage", minValue:0, maxValue:100, minStep:1}, {type:"8", iid:14, |value|:10, perms:{"ev", "pr", "pw"}, description:"Brightness", format:"int", unit:"percentage", minValue:0, maxValue:100, minStep:1}}}}}}}}

抱歉,由于语法错误,我无法美化它。您将看到 value 属性被解析为 |value|而不是“价值”

输入是字符串,而不是 JSON 对象。输出应该是 JSON 对象,但字符串未正确解析并导致“未定义”对象。整个代码在 JavaScript 中完美运行,但在 Automator 中 JSON.parse() 方法失败。

有人遇到过并希望解决这个问题吗?

javascript json macos automator
1个回答
0
投票

@red_menace 的评论就是答案。 JSON 已正确解析,但 Automator 将其强制打印为 AppleScript 寄存器语法,这给人的印象是未正确解析。

© www.soinside.com 2019 - 2024. All rights reserved.