简体   繁体   English

java.lang.string类型的Java值无法转换为jsonobject

[英]Java value of type java.lang.string cannot be converted to jsonobject

I have an API (here) that outputs a JSON string as output. 我有一个API (此处)输出JSON字符串作为输出。 I want to parse this JSON. 我想解析这个JSON。 It was working a while ago, but I might have changed something in my API or java code. 它刚刚工作,但我可能在我的API或java代码中改变了一些东西。

This is the code I'm using: 这是我正在使用的代码:

Here, msg is a string which contains the api response 这里, 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();
}

I'm using codeigniter framework in php. 我在php中使用codeigniter框架。

This is the function in my php model : 这是我的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;
}

And this is my php controller : 这是我的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"}';
    }

}

I have searched a lot and tried a lot of different solutions I found n web, but none of the solutions I have tried has worked out for me. 我经常搜索并尝试了很多我在网上找到的不同解决方案,但我尝试过的解决方案都没有找到。

UPDATE : 更新:

this gives the error 给出了错误

this gives the error 给出了错误

But

this parses fine and gives me just key not found exception 解析很好,并给我只是键未找到异常

Github sample json api and my api give the cannot convert string to json obj exception, but google maps api doesn't. Github示例json api和我的api给出了cannot convert string to json obj异常,但google maps api没有。 Is there anything wrong with my api format ?? 我的api格式有什么问题吗?

FIXED : I finally figured out that my api output had a leading hidden character which caused the parsing error 修复:我终于发现我的api输出有一个隐藏的隐藏字符导致解析错误

This is a screenshot of the error I receive (Too big a screenshot, but you get the idea...) 这是我收到的错误的截图 (太大的截图,但你明白了......)

在此输入图像描述

I tried to solve the issue, nothing worked for me and I don't know why! 我试图解决这个问题,没有什么对我有用,我不知道为什么! I have a solution but I don't know if it does make sense! 我有一个解决方案,但我不知道它是否有意义!

    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());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM