Home > Mobile >  Parse Correct encoding with Kotlin rest template
Parse Correct encoding with Kotlin rest template

Time:02-21

I want to get the labels from the wiki api. And I am using rest template in Kotlin with Spring. I also tried OKHttp in Java but get the same results. It worked in Postman.

 val uri = "https://www.wikidata.org/w/api.php?action=wbgetentities&ids=Q1726&format=json&props=labels&languages=de|zh"

        val restTemplate = RestTemplate()
        val result = restTemplate.getForObject(uri, ByteArray::class.java)
        if (result != null)
            print(String(result, charset("UTF-8")))

Output is

{"entities":{"Q1726":{"type":"item","id":"Q1726","labels":{"zh":{"language":"zh","value":"\u6155\u5c3c\u9ed1"},"de":{"language":"de","value":"M\u00fcnchen"}}}},"success":1}[B@6a71619f

I expect it to be "München" and "慕尼黑"

How to I parse the encoded strings correctly?

CodePudding user response:

This answer did the trick.

Using StringEscapeUtils from org.apache.commons.text because the other is deprecated.

  • Related