0

I received a json file with ascii code in middle, for example:

âmbito da Comemoração do Dia de São Valentim, 14 de fevereiro.

How decode this to special chars in java.

Thanks you.

  • http://stackoverflow.com/questions/7693994/how-to-convert-ascii-code-0-255-to-a-string-of-the-associated-character – Aditya Vyas-Lakhan Jan 28 '16 at 12:29
  • 1
    "I received a json file with ascii code in middle" -- actually, you received a JSON file with HTML/XML entities in the middle. "How decode this to special chars in java" -- I usually pass the string through `Html.fromHtml()`, which decodes most of those. – CommonsWare Jan 28 '16 at 12:31
  • That is no ASCII code. Those are XML &-Escapes. – Hermann Klecker Jan 28 '16 at 12:38

1 Answers1

1

That is "âmbito da Comemoração do Dia de São Valentim, 14 de fevereiro."

    String s = "âmbito da Comemoração do Dia de São Valentim, 14 de fevereiro.";
    Pattern p = Pattern.compile("&#(\\d+);|.");
    StringBuilder sb = new StringBuilder();
    Matcher m  = p.matcher(s);
    while (m.find())
        if (m.group(1) != null)
            sb.append((char)Integer.parseInt(m.group(1)));
        else
            sb.append(m.group());
    System.out.println(sb.toString());
    // -> âmbito da Comemoração do Dia de São Valentim, 14 de fevereiro.