你好,我有这个json条目存储在对象字符串在R:
string <- '"rooms":"{"name": "01","type": "Quiet Room","used": "90","empty": "10"},{"name": "02","type": "Meeting Room","used": "90","empty": "10"}"}'
想要插入左括号和右括号,使其成为一个json数组使用gsub这样的结果是:
"rooms": "[{"name": "01","type": "Quiet Room","used": "90","empty": "10"},{"name": "02","type": "Meeting Room","used": "90","empty": "10"}]"
我当前的方法:
gsub("rooms":"\{\"name(.*\"\})"", "rooms":"\[\{\"name\1\]"", string)
但是它不起作用:(朋友们,有什么主意吗?
用这个模式试试sub
:
sub('(.*):"(.*)}','\1:"[\2}]',string)
这将在第一个:
之后插入一个左括号,在最后一个}
之后插入一个右括号。