Php JSONException:无法将java.lang.String类型的值转换为JSONObject
我面临一个问题,一个有效的JSON字符串不能成为JSON对象 我已经测试了来自服务器的响应,它是一个有效的JSON 我在网上查过,是关于UTF-8和DOM的问题。但即使我将Notepad++中的字符集更改为不带DOM的UTF-8,仍然会出现相同的错误 我的代码:Php JSONException:无法将java.lang.String类型的值转换为JSONObject,php,android,web-services,Php,Android,Web Services,我面临一个问题,一个有效的JSON字符串不能成为JSON对象 我已经测试了来自服务器的响应,它是一个有效的JSON 我在网上查过,是关于UTF-8和DOM的问题。但即使我将Notepad++中的字符集更改为不带DOM的UTF-8,仍然会出现相同的错误 我的代码: <?php require_once("Connection/conn.php"); //parse JSON and get input $json_string = $_POST['json'];
<?php
require_once("Connection/conn.php");
//parse JSON and get input
$json_string = $_POST['json'];
$json_associative_array = json_decode($json_string,true);
$userId = $json_associative_array["userId"];
$password = $json_associative_array["password"];
$userType = $json_associative_array["userType"];
//get the resources
$json_output_array = array();
$sql = "SELECT * FROM account WHERE userId = '$userId' AND password = '$password' AND userType = '$userType'";
$result = mysql_query($sql);
//access success?
if (!$result) {
die('Invalid query: ' . mysql_error());
$json_output_array["status"] = "query failed";
}
else{
$json_output_array["status"] = "query success";
}
//find the particular user?
if (mysql_num_rows($result) > 0){
$json_output_array["valid"] = "yes";
}
else{
$json_output_array["valid"] = "no";
}
//output JSON
echo json_encode($json_output_array);
?>
未格式化JSON:
{“状态”:“查询成功”,“有效”:“是”}
当我将其复制到记事本++中时,它将变成?{“status”:“query success”,“valid”:“yes”}
似乎有一个不可见字符。我用MuhammedPasha提供的修复了它,它对JSON字符串进行子串以删除不可见字符。我将JSON字符串从1子串起来,以解决我的问题
有一种方法可以检测那些不可见的字符,将日志结果复制到记事本++(复制!不要键入!)如果有?(问号),它们表示存在一些不可见的字符。我也有同样的问题。也许您需要在不使用unicode签名(BOM)的情况下保存。您应该发布JSON结果。看起来您正在尝试将JSON数组转换为JSON对象—这不起作用。@dispake我已更新了答案:)您是否尝试为UrlEncodedFormEntity调用添加编码类型?UrlEncodedFormEntity(成对,HTTP.UTF_8);首先,您应该公布错误的实际情况,以及它发生在这两个错误中的哪一部分?目前没有人知道是PHP还是Android出错。同时提及您遇到的任何错误文本。此外,如果能看到生成的JSON字符串未格式化,那就太好了。抱歉,我犯了一个惯常的错误,忽略了标题,因为标题离发帖文本太远了。事实上,我已经找到了这个问题的原因。我用UTF-8保存了这个PHP,没有BOM,但是包含的PHP仍然用DOM保存为UTF-8
public boolean login() {
// instantiates httpclient to make request
DefaultHttpClient httpClient = new DefaultHttpClient();
// url with the post data
String url = SERVER_IP + "/gc/login.php";
JSONObject holder = new JSONObject();
try {
holder.put("userId", "S1");
holder.put("password", "s12345");
holder.put("userType", "supervisor");
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Log.d("JSON", holder.toString());
// HttpPost
HttpPost httpPost = new HttpPost(url);
//FormEntity
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("json", holder.toString()));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// execution and response
boolean valid = false;
try {
HttpResponse response = httpClient.execute(httpPost);
Log.d("post request", "finished execueted");
String responseString = getHttpResponseContent(response);
Log.d("post result", responseString);
//parse JSON
JSONObject jsonComeBack = new JSONObject(responseString);
String validString = jsonComeBack.getString("valid");
valid = (validString.equals("yes"))?true:false;
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return valid;
}
private String getHttpResponseContent(HttpResponse response) {
String responseString = "";
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
responseString += line ;
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return responseString;
}
{
"status": "query success",
"valid": "yes"
}