无法将Java.lang.string类型的Java值转换为jsonobject
我有一个输出JSON字符串作为输出的API。我想解析这个JSON。它在一段时间前就开始工作了,但我可能已经更改了API或java代码中的某些内容 这是我正在使用的代码:无法将Java.lang.string类型的Java值转换为jsonobject,java,php,json,codeigniter,jsonexception,Java,Php,Json,Codeigniter,Jsonexception,我有一个输出JSON字符串作为输出的API。我想解析这个JSON。它在一段时间前就开始工作了,但我可能已经更改了API或java代码中的某些内容 这是我正在使用的代码: public function get_cleaner($mobile, $pass){ $this->db->select("cleaner.cleaner_id, cleaner.cleaner_name, cleaner.date_of_joining, cleaner.current_address,
public function get_cleaner($mobile, $pass){
$this->db->select("cleaner.cleaner_id, cleaner.cleaner_name, cleaner.date_of_joining, cleaner.current_address, cleaner.mobile1, cleaner.mobile2, cleaner.date_of_birth, skill.skill_name as cleaner_designation");
$this->db->from("cleaner");
$this->db->join('skill', 'cleaner.designation = skill.skill_id', 'left');
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->result_array();
return $data;
}
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Content-Type: application/javascript');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data[0]);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
这里,msg
是一个包含api响应的字符串
try {
/* this prints */
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
JSONObject jObject = new JSONObject(msg);
String cleaner_id = jObject.getString("cleaner_id");
/* but this does not */
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
catch(Exception e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
我正在php中使用codeigniter框架
这是我的php模型中的函数:
public function get_cleaner($mobile, $pass){
$this->db->select("cleaner.cleaner_id, cleaner.cleaner_name, cleaner.date_of_joining, cleaner.current_address, cleaner.mobile1, cleaner.mobile2, cleaner.date_of_birth, skill.skill_name as cleaner_designation");
$this->db->from("cleaner");
$this->db->join('skill', 'cleaner.designation = skill.skill_id', 'left');
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->result_array();
return $data;
}
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Content-Type: application/javascript');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data[0]);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
这是我的php控制器:
public function get_cleaner($mobile, $pass){
$this->db->select("cleaner.cleaner_id, cleaner.cleaner_name, cleaner.date_of_joining, cleaner.current_address, cleaner.mobile1, cleaner.mobile2, cleaner.date_of_birth, skill.skill_name as cleaner_designation");
$this->db->from("cleaner");
$this->db->join('skill', 'cleaner.designation = skill.skill_id', 'left');
$this->db->where("mobile1", $mobile);
$this->db->where("user_pass", $pass);
$data = $this->db->get()->result_array();
return $data;
}
public function getCleaner(){
$mobile_no = $this->input->get('mobile');
$pass = $this->input->get('pass');
if(!is_null($mobile_no) && !is_null($pass))
{
header('Content-Type: application/javascript');
$data = $this->cleaner_model->get_cleaner($mobile_no, $pass);
if(!empty($data))
echo json_encode($data[0]);
else
echo '{"cleaner_id":"-10","cleaner_name":"cleaner_not_found"}';
}
}
我在网上搜索了很多,尝试了很多不同的解决方案,但是没有一个解决方案适合我
更新:
给出了错误
给出了错误
但是
解析精细并给我just key not found异常
Github示例json api和my api给出了无法将字符串转换为json obj
异常,但google maps api没有。我的api格式有什么问题吗
修复:我终于发现我的api输出有一个导致解析错误的前导隐藏字符
这是我收到的错误截图(截图太大,但你明白了…)
我试图解决这个问题,但什么都不管用,我也不知道为什么!我有一个解决方案,但我不知道它是否有意义
try{
HttpURLConnection conn = (HttpURLConnection) new URL("http://mridulahuja.com/api/index.php/cleaner/getCleaner?mobile=1111111111&pass=njnjhbh").openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
if(line != null){
line = line.substring(line.indexOf("{"));
JSONObject obj = new JSONObject(line);
System.out.println(obj.getString("cleaner_name"));
}
reader.close();
}catch(Exception e){
System.err.println(e.getMessage());
}
我试图解决这个问题,但什么都不管用,我也不知道为什么!我有一个解决方案,但我不知道它是否有意义
try{
HttpURLConnection conn = (HttpURLConnection) new URL("http://mridulahuja.com/api/index.php/cleaner/getCleaner?mobile=1111111111&pass=njnjhbh").openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream(), "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String line = reader.readLine();
if(line != null){
line = line.substring(line.indexOf("{"));
JSONObject obj = new JSONObject(line);
System.out.println(obj.getString("cleaner_name"));
}
reader.close();
}catch(Exception e){
System.err.println(e.getMessage());
}
抱歉…我现在添加了它。您使用org.json.JSONObject类还是org.json.simple.JSONObject?为什么您的内容类型是
application/javascript
而不是application/json
?我尝试使用application/json,但没有任何改变,我使用的是org.json.JSONObject对不起……我现在添加了它。您使用org.json.JSONObject类还是org.json.simple.JSONObject?为什么您的内容类型是application/javascript
而不是application/json
?我尝试使用application/json,但没有任何改变,我正在使用org.json.JSONObject