JSON returned by Google Maps Query contains encode

2019-04-13 06:10发布

问题:

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:

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.