从json转换为十进制

时间:2011-10-10 13:40:16

标签: java regex json

我有一个JSON字符串。

{"bounds": {"south west":{ "lng":74.1475868, "lat": 31.366689}, "northeast": { "lng":74.85623 ,"lat": 32.5698746}}

我想在Java中使用正则表达式获取带小数值的整数。

2 个答案:

答案 0 :(得分:4)

JSON不是常规语言,因此无法通过普通正则表达式进行解析(并使用正则表达式的非常规扩展解析它非常复杂)。相反,请使用Java JSON library

答案 1 :(得分:0)

不要使用正则表达式来解析结构化文档。有更好的方法来做到这一点。现在万一有人强迫你使用正则表达式来完成这项工作,你可以用枪指着你的头:

try {
    Pattern regex = Pattern.compile("[-+]?\\b[0-9]*\\.?[0-9]+\\b");
    Matcher regexMatcher = regex.matcher(subjectString);
    while (regexMatcher.find()) {
        // matched text: regexMatcher.group()
        // match start: regexMatcher.start()
        // match end: regexMatcher.end()
    } 
} catch (PatternSyntaxException ex) {
    // Syntax error in the regular expression
}

这将使用可选的整数部分捕获所有数字整数或非负数或正数。

请注意,会使用科学记数法匹配数字!

相关问题