gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:49:39 28 4
gpt4 key购买 nike

我正在尝试使用 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((内容))”=>

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"}])

在网络端,我可以成功接收此消息,这是我的 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";
}
)

我不知道为什么它将方括号变成圆括号。这真的让我很困惑。正如我之前提到的,我只想要一个正确的数据结构。它来自QRCode,我认为我在QRCode中制作了正确的数据结构,但我不知道为什么在我将其传递到网络后它发生了变化。

谁能帮我吗?

最佳答案

我已经通过再次进行 stringify 修复了它:

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

然后方括号又回来了。

关于javascript - 将 JSON 字符串从 iOS WKWebKit 传递到 JS ,总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55815590/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com