gpt4 book ai didi

javascript - 将自定义字符串转换为 json

转载 作者:行者123 更新时间:2023-12-02 17:28:07 24 4
gpt4 key购买 nike

如何将字符串转换为 JSON,

此字符串为

x:100%,y:20%,radius:50%

Json

{"x":"100%","y":"20%","radius":"50%"}

最佳答案

您不需要 jQuery。

var s = "x:100%,y:20%,radius:50%";
var json = JSON.stringify(
s.split(',').reduce(function(m,v) {
var t = v.split(':');
m[t[0]] = t[1];
return m;
}, {})
);

Demonstration

如果要兼容IE8,请添加a shim for reduce .

关于javascript - 将自定义字符串转换为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18184913/

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