我不确定如何使用coffee脚本修改JSON文件格式 我有类似的东西
{"counts":{"USA":100,"France":90,"Italy":80,"Canada":70,"Germany":60}}
我想将json转换为此
[{text: "USA", weight: 100},{text: "France", weight: 90},{text: "Italy",weight: 80},{text: "Canada", weight: 70}, {text: "Germany", weight: 60}]
这在Java中很容易,但对咖啡脚本不太确定
答案 0 :(得分:1)
我将使用的CoffeeScript功能:
for key, value of object
循环{ foo }
与{ foo: foo }
示例:
data = {"counts":{"USA":100,"France":90,"Italy":80,"Canada":70,"Germany":60}}
result =
for text, weight of data.counts
{ text, weight }
console.log result
# [{"text":"USA","weight":100},{"text":"France","weight":90},{"text":"Italy","weight":80},{"text":"Canada","weight":70},{"text":"Germany","weight":60}]