1

I'm using org.json lib to parse my JSON string. My json string is from input stream. I'm reading the json string from input stream and passing it to JSONObject() constructor. But I'm getting the following exception:

[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]

I guess the problem is with the extra double quotes," in my input. When I use new JSONObject("{\"keyword\":\"xxxx"}");, it is working fine.

++++ UPDATE ++++

Here is my json string reading code:

    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);
3
  • 1
    How are you reading and logging the string? Show the code that reads the input and the code that caused that INFO output. Commented Aug 28, 2011 at 18:20
  • Hi @delnan, I have updated my question, please check once. Commented Aug 29, 2011 at 6:32
  • Well, I see what's causing the exception, but you already guessed that yourself. Your string contains quotes that shouldn't be there, and unless requestLine is of some type that magically adds quotes around the strings it collects, those quotes are the fault of whoever gives your program its input. If the quotes really are in the input, the input is invalid JSON and your code can't do anything about (except trying to hack around it). Commented Aug 29, 2011 at 8:05

1 Answer 1

1

I have solved this by eliminating the leading and trailing quotes by input = input.replaceAll("^\"|\"$", "");

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.