0

我正在将 Ruby 数组转换为 JSON,保存到 MySQL,然后加载到 KnockoutJS。问题是该数组仍然是一个 JSON 字符串,我无法对其进行迭代。

tags = `/usr/bin/svn ls #{svn_repo_url}`.split("/\n")

puts tags.inspect
["1.0.0", "1.0.1", "1.0.10", "1.0.11", "1.0.12", "1.0.13", "1.0.14", "1.0.15", "1.0.16", "1.0.2", "1.0.3", "1.0.4", "1.0.5", "1.0.6", "1.0.7", "1.0.8", "1.0.9"]

puts tags.to_json
["1.0.0","1.0.1","1.0.10","1.0.11","1.0.12","1.0.13","1.0.14","1.0.15","1.0.16","1.0.2","1.0.3","1.0.4","1.0.5","1.0.6","1.0.7","1.0.8","1.0.9"]

这被保存到 MySQL,然后被加载到 KnockoutJS,但它仍然是一个字符串,所以我无法在 foreach 循环中迭代它。

我试图这样做ko.mapping.toJS(myString)ko.toJSON(myString)但到目前为止没有运气并且无法转换为我可以迭代的实际数组或对象

我在这里做错了什么?

谢谢

更新:解决了eval(myString)

4

1 回答 1

1

在遍历数组之前,您可以使用 JSON.parse(array) 将其转换为 JavaScript 数组。

于 2013-11-07T04:37:45.417 回答