You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JAX-RS使用org.json.JSONObject接收JSON数据时抛出异常求助

Troubleshooting org.json.JSONObject Parsing Error vs javax.json Success

Hey there! Let's figure out why org.json.JSONObject is throwing an exception while javax.json.JsonObject handles your JSON data just fine. I'll walk through the most common culprits and fixes based on your sample JSON.

First, Let's Validate Your Sample JSON

Your JSON is valid (I checked it with a JSON linter), so the issue isn't with the JSON structure itself. Here's your sample for reference:

{ "error": "false", "error_msg": "", "version": "1.13.10", "result": { "malware": { "finding1": { "file": "/path/to/filep", "malware": "{HEX}r2h.malware.blue.44" } } }, "newest_version": "1.13.10" }

Common Causes & Fixes

1. Outdated org.json Library Version

Older versions of org.json had bugs parsing strings containing curly braces (like your "{HEX}r2h.malware.blue.44" value). This is likely the biggest culprit.

Fix: Upgrade to the latest stable version of org.json. For Maven, update your dependency:

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20240303</version> <!-- Use the latest version available -->
</dependency>

For Gradle:

implementation 'org.json:json:20240303'

2. Incorrectly Reading the Request Body

If you're building a REST API, you might be reading the application/json request body incorrectly with org.json, while javax.json handles it properly. For example:

  • Using request.getParameter("json") won't work for application/json requests (this is for form data).
  • Not fully reading the input stream before passing it to JSONObject.

Fix: Read the request body properly as a full string:

import org.json.JSONObject;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

public class JsonParser {
    public JSONObject parseRequest(HttpServletRequest request) throws IOException {
        // Read the entire request body into a string
        BufferedReader reader = request.getReader();
        StringBuilder jsonBuilder = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonBuilder.append(line);
        }
        String jsonStr = jsonBuilder.toString();
        
        // Now parse with org.json
        return new JSONObject(jsonStr);
    }
}

3. Unintended Character Escaping or Data Truncation

Sometimes, intermediary layers (like proxies or your API framework) might modify the JSON string—for example, truncating it or escaping characters incorrectly. javax.json might be more resilient to minor formatting issues, while org.json is stricter.

Fix: Log the raw JSON string before parsing it with org.json to verify it's exactly the same as what javax.json receives. Add a log statement like:

System.out.println("Raw JSON string: " + jsonStr); // Or use a proper logging framework

If the string is truncated or modified, trace back to where the data is being passed/processed.

Test the Direct Parsing

To rule out API-specific issues, test parsing your sample JSON directly with org.json:

import org.json.JSONObject;

public class TestJsonParsing {
    public static void main(String[] args) {
        String sampleJson = "{ \"error\": \"false\", \"error_msg\": \"\", \"version\": \"1.13.10\", \"result\": { \"malware\": { \"finding1\": { \"file\": \"/path/to/filep\", \"malware\": \"{HEX}r2h.malware.blue.44\" } } }, \"newest_version\": \"1.13.10\" }";
        
        try {
            JSONObject jsonObj = new JSONObject(sampleJson);
            // Retrieve the malware value to confirm parsing works
            String malware = jsonObj.getJSONObject("result")
                                    .getJSONObject("malware")
                                    .getJSONObject("finding1")
                                    .getString("malware");
            System.out.println("Successfully parsed malware value: " + malware);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

If this works with the latest org.json version, the issue is in how you're reading the request body in your API.


内容的提问来源于stack exchange,提问作者Ilir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:47:56