这是原始的JSON对象:
{"num":11,"content":"puss\n","percentage":0}
我要替换11
与12
,即改变"民"的值.
{"num":12,"content":"puss\n","percentage":0}
请用Ruby语言描述它.
使用原始json字符串转换为哈希对象JSON#parse
.根据需要更改哈希对象.然后使用以下命令将其转换回json字符串JSON#dump
:
require 'json' raw_json = '{"num":11,"content":"puss\n","percentage":0}' h = JSON.parse(raw_json) h['num'] += 1 JSON.dump(h) # => '{"num":12,"content":"puss\n","percentage":0}'