gpt4 book ai didi

javascript - 从 iOS 将字典数组传递给 javascript 函数

转载 作者:行者123 更新时间:2023-11-29 02:15:51 25 4
gpt4 key购买 nike

我的 javascript 函数需要这种类型的数组:

var data = [{
"sale": "202",
"year": "2000"
}, {
"sale": "215",
"year": "2002"
}, {
"sale": "179",
"year": "2004"
}, {
"sale": "199",
"year": "2006"
}, {
"sale": "134",
"year": "2008"
}, {
"sale": "176",
"year": "2010"
}];

我想将此数组作为参数从 [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"js_func(%@, %@)", y1Arr, y2Arr]];

我正在传递这种格式的字典数组:

({
x = "663.78";
y1 = "-0.000";
},
{
x = "663.88";
y1 = "0.005";
},
{
x = "663.98";
y1 = "0.019";
},
{
x = "664.08";
y1 = "-0.001";
}
)

但是它不起作用...

最佳答案

您不应该直接在 stringWithFormat 中传递数组,您应该首先将它们转换为 json 对象,然后将这些 json 对象(作为字符串表示形式)传递给 stringWithFormat

NSData *data1 = [NSJSONSerialization dataWithJSONObject:y1Arr options:0 error:NULL];
NSString *y1ArrStr = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding];

NSData *data2 = [NSJSONSerialization dataWithJSONObject:y2Arr options:0 error:NULL];
NSString *y2ArrStr = [[NSString alloc] initWithData:data2 encoding:NSUTF8StringEncoding];

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"js_func(%@, %@)", y1ArrStr, y2ArrStr]];

关于javascript - 从 iOS 将字典数组传递给 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28760419/

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