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) `

最佳答案

您的json无效。

paramsArrayparamsObjonethreeparamsStr应该用双引号引起来(有效的JSON包含键和值都被包装)。

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


尝试使用JSONLint之类的JSON验证程序。

08-24 23:57