JSON returned by Google Maps Query contains encode

2019-04-13 06:07发布

In a Java app, i get JSON (from Google Maps) with characters like \x26 which i want to convert to its original character &. As far as i can see, that's a UTF-8 notation, but i am not completely sure. In the source JSON all kinds of encoded characters can appear (e.g. \x3c/div\x3e). How can i decode them?

I tried:

String json = "\\x3c/div\\x3e";
byte [] b = json.getBytes("UTF-8");
json = new String(b,"UTF-8");

No luck. Any ideas?

1条回答
可以哭但决不认输i
2楼-- · 2019-04-13 06:50

Got it to work:

String json = "\\x3c/div\\x3e";        
json = URLDecoder.decode(json.replace("\\x", "%"), "UTF-8")
        .replace("\\=", "=")
        .replace("\\&", "=")
        .replace("\\?", "=");

The three other replaces are needed for other problems that occur with the result of a Google Maps Query JSON Result.

查看更多
登录 后发表回答