解析Json在Java中出现错误

解析Json在Java中出现错误,java,json,parsing,Java,Json,Parsing,我试图解析json字符串并获取参数。但它给出了一个错误: 请帮我解决这个问题。我知道解析json有很多问题和答案,但这次它不起作用了 这是我的代码: (我正在接收这种格式的json) 我得到了这个错误: org.primefaces.json.JSONException: A JSONObject text must begin with '{' at character 1 这是我的完整代码: try { String jString = doPost(toUrl, params)

我试图解析
json
字符串并获取参数。但它给出了一个错误: 请帮我解决这个问题。我知道解析
json
有很多问题和答案,但这次它不起作用了

这是我的代码: (我正在接收这种格式的json)

我得到了这个错误:

org.primefaces.json.JSONException: A JSONObject text must begin with '{' at character 1
这是我的完整代码:

try {
     String jString = doPost(toUrl, params);
//Getting this string: 
// \t\t\t\t\t\t\t\t\t\t\t{"msg":"directPaidout is true, but paidout password is wrong","sts":"2"}
     //replacing \t with ""
     jString = jString .replace("\t", "");

    JSONObject jsonObject = new JSONObject(jString);
    String mString = jsonObject.getString("msg");

    } catch (Exception e) {
        System.out.println("Error: SEND: Exception in sending request!);
        e.printStackTrace();
    }

//This is post and get String method
    public static String doPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);

            URLConnection conn = realUrl.openConnection();

            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.setDoOutput(true);
            conn.setDoInput(true);

            out = new PrintWriter(conn.getOutputStream());

            out.print(param);

            out.flush();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            //System.out.println("Res:" + result);
        } catch (Exception e) {
            System.out.println("POST EXc!" + e);
            e.printStackTrace();
        }

        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
if(jString .contains("{")){
jString = jString.substring(jString.indexOf("{")+0);
}

如果此代码可以帮助您,请查看并尝试。字符串对象未编译。必须像这样避开引号:
\“
,否则它将无法编译

import org.primefaces.json.JSONObject;

class Main {
    public static void main(String[] args) {
        String jString = "\t\t\t\t\t\t\t\t\t\t\t{\"msg\":\"directPaidout is true, but paidout password is wrong\",\"sts\":\"2\"}";
        jString = jString.replace("\t", "");
        JSONObject jsonObject = new JSONObject(jString);
        String mString = jsonObject.getString("msg");
        System.out.println(mString);
    }
}
输出

/usr/lib/jvm/java-1.8.0-openjdk-amd64/bin/java -javaagent:/opt/idea-IU-171.3780.107/lib/idea_rt.jar=40176:/opt/idea-IU-171.3780.107/bin -Dfile.encoding=UTF-8 -classpath /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/charsets.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/cldrdata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/dnsns.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/icedtea-sound.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/jaccess.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/localedata.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/nashorn.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunec.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunjce_provider.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/sunpkcs11.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/ext/zipfs.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jce.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/jsse.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/management-agent.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/resources.jar:/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/lib/rt.jar:/home/developer/IdeaProjects/sortarray/out/production/sortarray:/home/developer/Downloads/primefaces-6.1.jar Main
directPaidout is true, but paidout password is wrong

Process finished with exit code 0

这就是解决方案:

try {
     String jString = doPost(toUrl, params);
//Getting this string: 
// \t\t\t\t\t\t\t\t\t\t\t{"msg":"directPaidout is true, but paidout password is wrong","sts":"2"}
     //replacing \t with ""
     jString = jString .replace("\t", "");

    JSONObject jsonObject = new JSONObject(jString);
    String mString = jsonObject.getString("msg");

    } catch (Exception e) {
        System.out.println("Error: SEND: Exception in sending request!);
        e.printStackTrace();
    }

//This is post and get String method
    public static String doPost(String url, String param) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);

            URLConnection conn = realUrl.openConnection();

            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.setDoOutput(true);
            conn.setDoInput(true);

            out = new PrintWriter(conn.getOutputStream());

            out.print(param);

            out.flush();

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            //System.out.println("Res:" + result);
        } catch (Exception e) {
            System.out.println("POST EXc!" + e);
            e.printStackTrace();
        }

        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }
if(jString .contains("{")){
jString = jString.substring(jString.indexOf("{")+0);
}
replace(“\t”,”);正在{


谢谢您的支持!我真的很感谢!

您收到的错误消息中有什么不清楚的地方?@MarcinOrlowski在问题的结尾给出了它,这不是一个字符串。您的代码甚至都没有编译。请发布正确的代码。@MarcinOrlowski我的编译器缺乏对插件的理解。很明显,他编写的代码没有compile,否则他就不会得到他得到的错误。他问问题的方式令人困惑,但他没有犯下您试图纠正的错误。问题是,如果我将字符串定义为您的示例,它会起作用,但我收到的字符串在用json解析它之前一直起作用library@Abdulin首先,代码必须编译ode编译并给出错误,然后我们可以更好地帮助您。@Abdulin当您收到字符串时,代码是什么样子的?在这种情况下,json字符串并不像您所说的那样