2017-07-19 81 views
-5

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

这是我的代码: (我这种格式接收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"); 

我“M收到此错误:

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

你在收到的错误信息中有什么不清楚的地方? –

+0

@MarcinOrlowski它在问题末尾给出 – Abdulin

+1

这不是一个字符串 – Mritunjay

回答

-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 
+0

好的,然后如何添加\之前?“请帮助我吧 – Abdulin

+0

显然,他编写的代码确实可以编译,否则他不会得到他得到的错误,他问这个问题的方式很混乱,但他 – ajb

+0

问题如果我定义字符串作为你的例子它会工作,但字符串我接收工作,直到用json库解析它 – Abdulin

-2

这是溶液:

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

替换( “\ t” 的, “”);增加了{

感谢您的支持!对此,我真的非常感激!

+0

在这种情况下,json字符串是不像你说的那样 –