问题描述:

I'm trying to create a REST based web service with data encapsulated using JSON. My problem is when I pass the string version of JSON in the URI it turns to something like this: %7B%22coal%22:100,%22ele%22:1500.1%7D.

I'm wondering if there are any functions Java for converting between the escaped and unescaped versions so I can parse the string.

网友答案:

You should be sending any content in the Request body. Only send query parameters in the URI.

网友答案:

Yep, use URLEncoder and/or URLDecoder.

网友答案:

That would be URLDecoder. But unless you are doing something wrong, you shouldn't have to use it, since methods in the Servlet API gnerally return already-decoded strings.

网友答案:

You are not supposed to decode URL escaped text, the web server does that by its own, it should be completely transparent to you.

As some people already pointed out you use URLDecode to unescape a URL string

If on some point of your code you are manually escaping the JSON string that's probably the issue as its getting encoded 2 times.

网友答案:

Your parameters are flat (no lists/objects/etc.).

  • For HTTP GET requests, try to pass the data as query parameters instead of encoded JSON
  • For HTTP POST requests, you won't have an encoding problem with JSON
相关阅读:
Top