将 JSON 字符串从 iOS WKWebKit 传递给 JS,总是失败

passing JSON string from iOS WKWebKit to JS , always failure

我正在尝试使用来自 iOS 应用程序 (swift) 的字符串参数在 JS 中调用一个函数。我希望数据结构像:

[{"name":"Mat. Category","value":"PIPE"},
{"name":"Manufacturer","value":"Manufacturer 1"},
{"name":"Heat#","value":"78652457"},
{"name":"Manufacture Date","value":"2019-04-15"},
{"name":"Item Description","value":"8\" PIPE, 18\" LG"},
{"name":"Wall Thickness","value":"0.375IN"},
{"name":"Diameter","value":"24IN UPS"},
{"name":"Yield Strength","value":"35,000 psi"},
{"name":"Document Repository Link","value":"https://drive.google.com/drive/folders/1SPB7-R1p_UWEWnG9H6rx0SX3LTpDSG_L"}
];

所以,它是一个包含一些对象的数组。我从 iOS 传递它:

   webPage.evaluateJavaScript("cameraClosed(\(content))") { (res, error) in
            if error == nil{

                print("invoke successed")
                print("res:\(res)")
            }else{
                print("invoke failure")
                print(error)
            }
        }

内容是一个字符串。我打印传递给网络的 javascriptstring :

打印"cameraClosed((content))"=>

cameraClosed([{"name":"Mat. Category","value":"Aaron"},{"name":"Manufacturer","value":"Manufacturer 1"},{"name":"Heat#","value":"18616949864"},{"name":"Manufacture Date","value":"2019-04-14"},{"name":"Item Description","value":"8\" Steel Gas main - 50 Ft"},{"name":"Wall Thickness","value":"0.375IN"},{"name":"Diameter","value":"24IN UPS"},{"name":"Yield Strength","value":"35,000 psi"},{"name":"Document Repository Link","value":"https://drive.google.com/drive/folders/1SPB7-R1p_UWEWnG9H6rx0SX3LTpDSG_L"}])

在web端,我可以成功接收到这个,这是我的React代码:

 window.cameraClosed = (content) => {

      if (typeof (content) == "string") {
        window.webkit.messageHandlers.messageRecieved.postMessage("is String");
      }else{
        window.webkit.messageHandlers.messageRecieved.postMessage("is Object");
      }
}

然后我打印在 React 中收到的 content(使用 messageHandlers )因为 content 来自 QRcode,所以我无法在 PC 中调试它,我只能使用iOS 设备和嵌入的 WKWebView :

(
        {
        name = "Mat. Category";
        value = Aaron;
    },
        {
        name = Manufacturer;
        value = "Manufacturer 1";
    },

        {
        name = "Heat#";
        value = 18616949864;
    },
        {
        name = "Manufacture Date";
        value = "2019-04-14";
    },
        {
        name = "Item Description";
        value = "8\" Steel Gas main - 50 Ft";
    },
        {
        name = "Wall Thickness";
        value = "0.375IN";
    },
        {
        name = Diameter;
        value = "24IN UPS";
    },
        {
        name = "Yield Strength";
        value = "35,000 psi";
    },
        {
        name = "Document Repository Link";
        value = "https://drive.google.com/drive/folders/1SPB7-R1p_UWEWnG9H6rx0SX3LTpDSG_L";
    }
)

不知道为什么把方括号变成了parentheses.and,真是一头雾水。正如我之前提到的,我只想要一个正确的数据结构。它来自一个QRCode,我认为我在QRCode中制作了正确的数据结构,但我不知道为什么在我将它传递到网络后它改变了。

谁能帮帮我?

我通过再次进行字符串化来修复它:

 let string = JSON.stringify(content)
 let jsonArry = JSON.parse(string)

然后方括号又回来了。