我試圖解析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;
}
你在收到的錯誤信息中有什麼不清楚的地方? –
@MarcinOrlowski它在問題末尾給出 – Abdulin
這不是一個字符串 – Mritunjay