我有一个JSON字符串,可以从网站上抓取。我只需要以下数据(原始字符串要长得多)以下是检索到的JSON,我计划将其转换为Ruby Hash:
{"day": 15, "month": 03, "year": 2012, "hour": 10, "min": 00, "sec": 00}
我使用regex:检索到了上面的json
targetDate:s+(.*?)})/m
由于整数中有多余的零,我无法解析上面的json。(00和03)我试着用3代替03,用0代替00手动更改数字,结果成功了!
所以,我想json解析器可能无法查看这种数字。
问题是,我如何清理上面检索到的JSON,以便删除不必要的零。也就是说,
{"day": 15, "month": 3, "year": 2012, "hour": 10, "min": 0, "sec": 0}
谢谢你的帮助!
与其引入regex,不如评估它:
hash = eval '{"day": 15, "month": 03, "year": 2012, "hour": 10, "min": 00, "sec": 00}'.gsub(': ', ' => ')
尝试此regexp
json = '{"day": 15, "month": 03, "year": 2012, "hour": 10, "min": 00, "sec": 00}'
json.gsub(/b0*(d+)/, '1')
#=> {"day": 15, "month": 3, "year": 2012, "hour": 10, "min": 0, "sec": 0}
编辑:
虽然b
字边界不是严格必要的(请参阅注释),但它可以确保只有数字开头的零可以匹配。
json.gsub(/(?<=[: ])0+(d+,)/, "\1")
请记住,您可能有类似{ "someKey": "james bond: 007" }
的json,它将被替换为{ "someKey": "james bond: 7" }
。
json.gsub(/("w")s+:s+0+(d+)s+,/, "\1: \2,")
看起来更好,但也有可能"智胜"此regexp。Regexp不太适合这样的问题。
好的,这里是非正则表达式解决方案:
var inString = false; # check, whether current char is in string. Think of it as whether current symbol would be highlighted as string constant in editor
var out = []; # array/stack for output
var prevChar = null; # previous char. One may init to space symbol or even replace it with `out[-1]` everywhere
for (chr in jsonStr) { # iterate over symbols (chars) of a string
if (char == '"' && prevChar != "\") inString = !inString;
if (!isDigit(out[-2])
&& prevChar == '0'
&& isDigit(chr)) { # i.e. last 3 chars match /(D)0(d)/
out[-1] = prevChar = chr; # make it 12
} else {
out.push(prevChar = chr); # just continue building string
}
}
out.join("");
将其视为类似javascript的伪代码,而不进行测试。