Tencent/APIJSON · error · UnsupportedEncodingException
JSON格式不合法!{}! {}
Error message
JSON格式不合法!{}! {} What it means
parseRequest(String) wraps JSON.parseObject plus a null check; any parse failure or null result is rethrown as UnsupportedEncodingException with message 'JSON格式不合法!<cause>! <original request>'. It is the entry gate: the raw request body must be a valid JSON object string before any APIJSON parsing begins.
Source
Thrown at APIJSONORM/src/main/java/apijson/orm/AbstractParser.java:452
if (verifier == null) {
verifier = createVerifier().setVisitor(getVisitor());
}
verifier.setParser(this);
return verifier;
}
/**解析请求JSONObject
* @param request => URLDecoder.decode(request, UTF_8);
* @return
* @throws Exception
*/
public static <M extends Map<String, Object>> M parseRequest(String request) throws Exception {
try {
M req = JSON.parseObject(request);
Objects.requireNonNull(req);
return req;
} catch (Throwable e) {
throw new UnsupportedEncodingException("JSON格式不合法!" + e.getMessage() + "! " + request);
}
}
/**解析请求json并获取对应结果
* @param request
* @return
*/
@Override
public String parse(String request) {
return JSON.toJSONString(parseResponse(request));
}
/**解析请求json并获取对应结果
* @param request
* @return
*/
@NotNull
@Override
public String parse(M request) {View on GitHub (pinned to 5284052872)
Solutions
- Print the request string included in the message and run it through any JSON validator to find the syntax error
- Use a real JSON serializer client-side instead of string concatenation/template literals
- Ensure the endpoint receives a raw JSON object body ({} outermost), not an array or query-string
- Check for encoding damage: URL-decode twice only if your transport requires it
Example fix
// before
String body = "{'name': 'a',}");
// after
String body = "{\"name\": \"a\"}"; Defensive patterns
Strategy: validation
Validate before calling
Object parsed = new JSONParser().parse(body);
if (!(parsed instanceof JSONObject)) throw new IOException("body is not a JSON object"); Type guard
boolean isValidJsonObjectString(String s) {
try { return s != null && s.trim().startsWith("{") && new JSONParser().parse(s) instanceof JSONObject; }
catch (Exception e) { return false; }
} Try / catch
catch (UnsupportedEncodingException e) { if (e.getMessage().contains("JSON格式不合法")) { logPayload(body); return clientError("invalid json body"); } throw e; } Prevention
- Always use a JSON serializer; never template-concatenate bodies
- Validate the body with a JSON schema/linter in tests
- Log the offending payload (message includes it) once to find the exact syntax break
When it happens
Trigger: Sending a malformed body: truncated JSON, single quotes, trailing commas, a JSON array instead of object, form-encoded or XML content, or an empty body. Also double-URL-encoding that corrupts the payload before decode.
Common situations: HTTP client sends the JSON with wrong Content-Type handling or manual string concatenation; proxy/gateway mangles the body; client library serializes undefined to invalid tokens; version change switched from form params to raw body without updating the client.
Related errors
- JSON格式不合法!
- Value for key '" + key + "' is not a Map: " + value.getClass
- Value for key '" + key + "' is not a List: " + value.getClas
- Cannot convert String value '" + value + "' to int: " + e.ge
- Cannot convert value of type " + value.getClass().getName()
AI-assisted analysis of Tencent/APIJSON@5284052872 (2026-08-14).
Data as JSON: /api/errors/b281a0d197bc5e08.
Report an issue: GitHub.