我有一个输出 JSON 字符串作为输出的 API (here) 。我想解析这个 JSON。前一阵子它还在工作,但我可能已经更改了我的 API 或 Java 代码中的某些内容。
这是我正在使用的代码:
这里,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;
}
这是我的 php 控制器:
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"}';
}
}
我进行了很多搜索并尝试了很多我在网上找到的不同解决方案,但我尝试过的解决方案都没有为我解决。
更新 :
这给出了错误
这给出了错误
但
这 解析得很好,只给了我未找到异常的键
Github sample json api 和 my api 给出了cannot convert string to json obj
例外,但 google maps api 没有。我的api格式有什么问题吗??
修复:我终于发现我的 api 输出有一个导致解析错误的前导隐藏字符
这是我收到的错误的屏幕截图(屏幕截图太大,但你明白了......)