0

I am trying to parse through a string which i create after getting a response from an API.

this is the string i am trying to parse:

// API callback
translateText({
 "data":{
  "translations":[
  {
   "translatedText": "Ola Mundo",
   "detectedSourceLanguage": "en"
  }
 ]
}
}
);

i want to be able to get the Translated text "Ola Mundo", but i don't want to just search through it and get that specific text, as this Text maybe different the next time.

Jdavis649
  • 129
  • 1
  • 1
  • 8

3 Answers3

0

It seems this is a valid JSON object , one option is to convert the string to a JSON object using Jackson library or something else , after that you can get anything you want .

0

Try to do that with the following Regex:

translatedText": (.*?),

In Java, you approach to the result with:

Pattern p = Pattern.compile("");
Matcher m = p.matcher(str);
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
0

If you you remove the translateText(); part and just have:

{ "data":{ "translations":[ { "translatedText": "Ola Mundo", "detectedSourceLanguage": "en" } ] } }

its a valid JSON object and you can easily use a library to parse it. See this post. This way is recommend if you want to access the data securely.

Community
  • 1
  • 1
Joakim Ericsson
  • 4,616
  • 3
  • 21
  • 29