且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在CoffeeScript中从JSON中获取数据

更新时间:2023-08-26 12:26:04

无论您是在浏览器中执行此操作,还是在节点中,您应该能够将JSON字符串传递到JSON.parse并选择所需的值。然后,您可以使用Node的fs模块附加到文件,如下所示: http://***.com/a/11267583/659910

Whether you're doing this in the browser or in Node, you should be able to pass the JSON string to JSON.parse and pick out the value you want. You can then append to a file using Node's fs module like this: http://***.com/a/11267583/659910.

fs = require 'fs'

# Sample JSON string.
json = '{ "statType": "TOTAL_SESSIONS_PLAYED", "count": { "value": 5 }, "dataVersion": 0 }'

data = JSON.parse(json)
fs.appendFile('/tmp/data.txt', data.count.value, (error) -> throw error if error)