简体   繁体   中英

Getting specific part of a string

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.

看来这是个有效的JSON对象,一种选择是使用Jackson库或其他东西将字符串转换为JSON对象,之后您可以得到想要的任何东西。

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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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