解析Json在Java中给出了错误

时间:2017-07-19 06:46:18

标签: java json parsing

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

这是我的代码: (我以这种格式接收json

//using this library
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

//this is how im receiving string - exactly same
String  jString =  "\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");

我收到此错误:

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;
    }

2 个答案:

答案 0 :(得分:-2)

这是解决方案:

if(jString .contains("{")){
jString = jString.substring(jString.indexOf("{")+0);
}

replace(“\ t”,“”);在{

之间添加空格

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

答案 1 :(得分:-3)

请查看并尝试此代码是否可以帮助您。您的字符串对象无法编译。必须逃避这样的引号:\"否则它将无法编译。

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