0
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException, ParseException {


        String s = "{paramsArray: [\"first\", 100],"
                + "paramsObj: {one: \"two\", three: \"four\"},"
                + "paramsStr: \"some string\"}";

        JSONParser parser = new JSONParser();
        Object ob = parser.parse(s);
        JSONObject obj = (JSONObject) ob;
        System.out.println(obj.get("paramsStr"));


    }

}

Error: Exception in thread "main" Unexpected character (p) at position 1.
at org.json.simple.parser.Yylex.yylex(Yylex.java:610)
at org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:118)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:81)
at org.json.simple.parser.JSONParser.parse(JSONParser.java:75)
at Main.main(Main.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120) `
1
  • The key values in the objects need to be in quotes. Commented Feb 28, 2014 at 16:17

2 Answers 2

2

Your json is not valid.

The paramsArray,paramsObj,one,three and paramsStr should be wrapped beetween double quotes (a valid JSON has keys and values both wrapped).

String s = "{\"paramsArray\": [\"first\", 100],"
            + "\"paramsObj\": {\"one\": \"two\", \"three\": \"four\"},"
            + "\"paramsStr\": \"some string\"}";

Try a JSON validator like JSONLint.

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

Comments

1
"{ 'paramsArray': ['first', '100']," + 
  "'paramsObj': { 'one':   'two'," +
                   "'three': 'four'}," +
  "'paramsStr': 'some string'}"

Use single quotes inside double ones to increase readability like so...

1 Comment

Although some parsers may accept single quotes, the JSON specification specifically says that names, which are string values, need to be enclosed in double quotes ".

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.