2016-09-26 16 views
1

私は出力としてJSON文字列を出力するAPI (here)を持っています。私はこのJSONを解析したい。それはもう少し前に働いていましたが、APIやJavaコードで何か変更した可能性があります。java.lang.string型のJava値はjsonobjectに変換できません

これは私が使用しているコードです:

をここで、msgは、私はPHPでCodeIgniterのフレームワークを使用している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のモデル内の関数である:

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"}'; 
    } 

} 

私はn webを見つけましたが、私が試したソリューションのどれも私のために働いていません。

UPDATE:

thisエラーに

thisを与える

しかし

thisは罰金を解析し、私にだけキーが見つからない例外

を与えるエラーが発生します

Githubサンプルjson apiと私のapiはcannot convert string to json obj例外を指定しますが、Googleマップapiは例外ではありません。私のAPIフォーマットに何か問題はありますか?

はFIXED:私はようやく私のAPIの出力は、これは私が(あまりにも大きなスクリーンショットを受信エラーのスクリーンショットです

解析エラーの原因となった主要な隠された文字を持っていたことを考え出したが、あなたが得ます私はこの問題を解決しようとした

enter image description here

+0

申し訳ありません...今すぐ追加しました。 – mrid

+0

org.json.JSONObjectクラスまたはorg.json.simple.JSONObjectを使用していますか? ? –

+0

なぜあなたのコンテンツタイプが 'application/json'ではなく' application/javascript'ですか? – RealSkeptic

答えて

0

アイデアが...)、何も私のために働いていないと私は理由を知りません!私には解決策がありますが、それが理にかなっているかどうかはわかりません!

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

あなたの努力の男のおかげで、助けなかった... – mrid

関連する問題