简体   繁体   English

仅从 java 中的 JsonNode 获取双精度类型

[英]Fetch only double types from JsonNode in java

I get the following JsonNode with JsonNode我用 JsonNode 得到以下 JsonNode

results = parentNode.get(GEOMETRY);

yields results: "marks (79.89 90.78)"产生结果: "marks (79.89 90.78)"

The problem is, I get the whole string "marks (79.89 90.78)" in results.问题是,我在结果中得到了整个字符串“marks (79.89 90.78)”。 But I need to fetch the doubles present inside () braces seperately.但我需要分别获取 () 大括号内的双打。

Any idea as to how I can get the double numbers from this string?关于如何从这个字符串中获取双数的任何想法?

I can use regular expression to get the numbers from the string(results string), but I want to know if there is any other workaround to do it.我可以使用正则表达式从字符串(结果字符串)中获取数字,但我想知道是否有其他解决方法可以做到这一点。

If the structure is always the same, you could do something like this to parse the doubles out:如果结构始终相同,您可以执行以下操作来解析双打:

String fromJson = "marks (79.89 90.78)";
String[] split = fromJson.split(" ");
double a = Double.parseDouble(split[1].substring(1));
double b = Double.parseDouble(split[2].substring(0,split[2].length()-1));
System.out.println(a); // 79.89
System.out.println(b); // 90.78

You can use regular expression.您可以使用正则表达式。 It gives all the values which matches given pattern.它给出了与给定模式匹配的所有值。 You can change the pattern according your needs.您可以根据需要更改模式。

String s = "marks (79.89 90.78)";
    Pattern p = Pattern.compile("\\d+\\.\\d+");
    Matcher m = p.matcher(s);
    while(m.find()) {
        double d = Double.parseDouble(m.group());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM