1

我正在使用 org.json 库来解析我的 JSON 字符串。我的 json 字符串来自输入流。我正在从输入流中读取 json 字符串并将其传递给 JSONObject() 构造函数。但我得到以下异常:

[2011-08-28 23:42:52,235] main INFO - Task(): input = "{\"keyword\":\"xxxx"}" [2011-08-28 23:42:52,238] main ERROR - Task(): Exception: A JSONObject text must begin with '{' at 1 [character 2 line 1]

我想问题出"在我的input. 当我使用new JSONObject("{\"keyword\":\"xxxx"}");时,它工作正常。

++++更新++++

这是我的json字符串读取代码:

    try {
        in = new InputStreamReader(new BufferedInputStream(is));

        int c;
        while (true) {
            c = in.read();
            if (c == '\r' || c == '\n')
                break;
            requestLine.append((char) c);
        }

    } catch (Exception e) {
        logger.error("Task(): Exception: "+e.getMessage());
    }
    input = requestLine.toString();
    //input = "{\"keyword\":\"xxxx\"}"; //working fine
    logger.info("Task(): input = "+input);
    try{
        org.json.JSONObject json = new org.json.JSONObject(input);      
        keyword = json.getString("keyword");
    }catch(Exception e) {
        logger.error("Task(): Exception: "+e.getMessage());
    }


    logger.info("Task(): keyword = "+keyword);
4

1 回答 1

1

我已经通过消除前导引号和尾随引号解决了这个问题 input = input.replaceAll("^\"|\"$", "");

于 2011-08-29T18:01:13.250 回答