简体   繁体   English

Java - 使用多正则表达式从字符串中提取 JSON 值

[英]Java - extract JSON values from string using multi regex

I am trying to use this multi regex java library to extract JSON field value from a string.我正在尝试使用这个多正则表达式 java 库从字符串中提取 JSON 字段值。

My JSON look like this:我的 JSON 看起来像这样:

{
  "field1": "something",
  "field2": 13
  "field3": "some"
}

I have created a regex pattern to fit each field, and it is working with Java Regex Pattern by simply doing something like this for each pattern:我创建了一个正则表达式模式来适应每个领域,它通过简单地为每个模式做这样的事情来使用 Java 正则表达式模式:

Matcher matcher = patternToSearch.matcher(receiveData);
        if (matcher.find()) {
            return matcher.group(1);
        }

I decided to try and improve the code and use multi regex so instead of scanning the string 3 times, it will scan it only one time and extract all needed values.我决定尝试改进代码并使用多正则表达式,而不是扫描字符串 3 次,它只会扫描一次并提取所有需要的值。

So I came up with something like this:所以我想出了这样的事情:

String[] patterns = new String[]{
                    "\"field1\":\\s*\"(.*?)\"",
                    "\"field2\":\\s*(\\d+)(\\.\\d)?",
                    "\"field3\":\\s*\"(.*?)\"",
            };
            this.matcher = MultiPattern.of(patterns).matcher();

the matcher has only one method - match - used like this: matcher只有一种方法 - match - 像这样使用:

int[] match = this.matcher.match(jsonStringToScan);

so I ended up with a list of integers, but I have no idea how to get the json values from these strings and how those integers are helping me.所以我最终得到了一个整数列表,但我不知道如何从这些字符串中获取 json 值以及这些整数如何帮助我。 The multi regex matcher does not support the group method I used before to get the value.多正则表达式matcher不支持我之前使用的group方法来获取值。

Any idea of how I can extract multiple json values from string using multi regex?知道如何使用多正则表达式从字符串中提取多个 json 值吗? (Scanning string only once) (只扫描一次字符串)

As mentioned on github page from your link match returnes indexes of patterns matched.正如在您的链接match github 页面上提到的,返回match模式的索引。 Another point from this page:此页面的另一点:

The library does not handle groups.图书馆不处理组。

Consider matching key as group too.也考虑将匹配键作为组。 Look at this simple example:看这个简单的例子:

    final Pattern p = Pattern.compile("\"(field.)\":((?:\".*?\")|(?:\\d+(?:\\.\\d+)?))");
    final Matcher m = p.matcher("{\"field3\":\"hi\",\"field2\":100.0,\"field1\":\"hi\"}");
    while (m.find()) {
        for (int i = 1; i <= m.groupCount(); i++) {
            System.out.print(m.group(i) + " ");
        }
        System.out.println();
    }

It prints:它打印:

field3 "hi" 
field2 100.0 
field1 "hi" 

If you want to avoid quotes in value group, you need more complicated logic.如果要避免值组中的引号,则需要更复杂的逻辑。 I've stopped at:我停在:

        final Pattern p = Pattern.compile("\"(field.)\":(?:(?:\"(.*?(?=\"))\")|(\\d+(?:\\.\\d+)?))");

resulting in导致

field3 hi null 
field2 null 100.0 
field1 hi null 

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

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