gpt4 book ai didi

javascript - JQuery 字符串到对象数组

转载 作者:行者123 更新时间:2023-11-27 22:33:27 25 4
gpt4 key购买 nike

我使用这些参数调用函数 tabulator()。

$("#tableObj").tabulator("addRow", {id:1, Name:"John", Age:"20"}, true);

我想动态传递数组元素名称,从 Json 读取 ( '{id:1, Name:"John", Age:"20"}' )

我的意思是列名将会改变。例如:{id:1,公司:“myComp”,地址:“myaddress”}

如何从字符串或 Json 文本创建这些对象?

最佳答案

您可以使用 JSON.parse 但请注意 id:1, Name:"John", Age:"20" 不是有效的 JSON。键必须用引号引起来,否则会产生错误。

var str = '{"id":1, "Name":"John", "Age":"20"}';
var obj = JSON.parse(str);

$("#tableObj").tabulator("addRow", obj, true);

关于javascript - JQuery 字符串到对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39355219/

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