-3

I have Strings like this:

fields {
  key: "ristorante"
  value {
    string_value: ""
  }
}
fields {
  key: "type"
  value {
    list_value {
      values {
        string_value: "pizza"
      }
    }
  }
}

And I need to extract just the string_value of type, in this case the word pizza.

How can I do it in Java?

I don't want the answer for this specific case, I just want to know how to extract some text from a String or if is better to parse it to a JSON, and how to do it.

Orion
  • 1
  • 4
  • 2
    Did you leave some colons out? That sure looks a lot like JSON data. – VGR Nov 03 '20 at 18:09
  • Building off of what is said above, if you can convert your string into a JSON data, you would then be able to traverse it like a dictionary. If not, try looking into regex if you know that the value is always after "string_value". – David Yi Nov 03 '20 at 18:13
  • That is what DialogFlow returns. I copied the full String, which i converted with toString() method. I think it is a "protobuf", but i don't know how to manage that kind of values, so i thought to convert it in String to make it simple. – Orion Nov 03 '20 at 18:13
  • I tried to find a way to convert that "protobuf" to a JSON but had not success. – Orion Nov 03 '20 at 18:15
  • 1
    If this is actually JSON, use a JSON parser. If not, maybe read it line by line and check for lines that start with **string_value:**. – Dawood ibn Kareem Nov 03 '20 at 18:16
  • Do you have access to the protobuf object? That might be more useful than its string representation. – f1sh Nov 03 '20 at 18:16
  • Please turn to the [help] to learn how/what to ask here. Just dropping requirements "this is what I want" isn't appreciated. When you try something yourself, and you get stuck with a specific problem, we will gladly help. But please understand that this place is not intended to give guidance with the possibly many steps required to get you from your vision to a working program. – GhostCat Nov 03 '20 at 18:20
  • @GhostCat So how can I ask something that i don't understand and I can't find on web? I didn't want to ask "do it for me for this specific case", I just wanted to ask how to do it in general...You could answer "try with Lists and this method..." with some documentations attached. I explained my specific problem to be understandable with my bad english. – Orion Nov 03 '20 at 18:43
  • As said, such requests are typically not appreciated here. And the problem is: we simply have no idea about your current skills. See https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question for more details. – GhostCat Nov 03 '20 at 19:33
  • @GhostCat I'm sorry but I still really don't get it. Can you look at my other question on my profile? Is it bad too? I really want to understand how to ask a good question, I've read the post you have linked before and the one suggested at registration time. – Orion Nov 04 '20 at 01:53
  • Comparing with higher rated questions (pick some with code and reference data and stuff) and going through the links given above should be ok to get the questions right. takes a bit to get used to customs here ;) – Curiosa Globunznik Nov 04 '20 at 19:40

1 Answers1

-1
Pattern pattern = Pattern.compile("values[^s]+?string_value\\s?:\\s?\"(.*?)(?<!\\\\)\"");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
    String value = matcher.group(1);
    System.out.println(value);
}

Use this regex if you really only want to get the 'pizza' value

If you want to get all 'string_value' entries use

"string_value\\s?:\\s?\"(.*?)(?<!\\\\)\""

as the regex pattern. You will then find multiple occurrences, so use a while loop instead of if (matcher.find)

(Regex will probably be a little bit faster than parsing it with JsonParser)

IntoVoid
  • 914
  • 4
  • 7
  • The matcher allows you to find all occurrences of that regex pattern. You can also get parts of it (groups) like I did. It would be best practice to compile the pattern somewhere else, so you only compile it once and not every time. (You can reuse the pattern or reset the matcher) – IntoVoid Nov 03 '20 at 19:12
  • He should certainly use a JSON parser rather than any workaround. – user207421 Nov 04 '20 at 23:38
  • @MarquisofLorne can u tell me how to do it? But before please take a look to the answer i got [here](https://stackoverflow.com/questions/64651027/how-to-get-parameters-from-dialogflow-in-java-android-studio/64688141#64688141) – Orion Nov 05 '20 at 17:52