2017-03-25 22 views
0

我收到此錯誤:JSONObject cannot be converted to JSONArray的JSONObject不能轉換到JSONArray但成功後測試

造成這部分代碼:

private int parse() { 
     try { 
      Log.d("Jou", "result"); 
      JSONArray ja = new JSONArray(data); 
      JSONObject jo = null; 
      titles.clear(); 
      skills.clear(); 
      for (int i = 0; i < ja.length(); i++) { 
       jo = ja.getJSONObject(i); 
       String id = jo.getString("ID"); 
       String title = jo.getString("post_title"); 
       //String content = jo.getString("post_content"); 
       String date = jo.getString("post_date"); 
       Skill skill = new Skill(); 
       skill.setId(id); 
       skill.setTitle(title); 
       //skill.setContent(content); 
       skill.setDate(date); 
       skills.add(skill); 
       titles.add(title); 
      } 
      return 1; 
     } catch (JSONException e) { 
      Log.d("Jou", e.getMessage()); 
      return 0; 
     } 

雖然我以前嘗試過了,這是完全一樣的,那麼我添加了另一個字符串,這是日期,然後我得到了錯誤。代碼有什麼問題?

這是一個需要解析的服務器結果:

s = {"result":[{"post_id":"390","post_title":"Cart","post_date":‌​"2017-02-07 12:17:29"},{"post_id":"421","post_title":"Front End Developer - Digital Arts","post_date":"2017-02-07 12:18:04"},{"post_id":"431","post_title":"Art Director","post_date":"2017-02-07 12:18:19"}]} 

這裏是PHP腳本:

<?php 
$dbhost = 'localhost'; 
$dbuser = ''; 
$dbpass = ''; 
$conn = mysqli_connect($dbhost, $dbuser, $dbpass) or die ("Unable to connect") ; 
if(! $conn) 
{ 
echo 'Could not connect: ' . mysqli_error(); 
} 
error_reporting(-1); 
ini_set('display_errors', 'On'); 
mysqli_set_charset($conn, 'utf8'); 
$search =""; 
if(isset($_REQUEST['query'])){ 
    $search = $_REQUEST['query']; 
} 
if($search != ""){ 
    $sql = "SELECT ID,post_title,post_date FROM `wp_posts` WHERE post_title LIKE '%".$search."%'"; 

    mysqli_select_db($conn,''); 
$query = mysqli_query($conn, $sql) or die ("Error: ".mysqli_error($conn));; 
$result = array(); 
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
array_push($result, 
    array('post_id'=>$row['ID'], 
     'post_title'=>$row['post_title'], 
     'post_date'=>$row['post_date'] 
    ));} 
    echo json_encode(array("result"=>$result)); 
}else{ 
    echo 'No search field has been sent'; 
} 
?> 
+0

後,你必須解析JSON。 –

+0

s = {「result」:[{「post_id」:「390」,「post_title」:「Cart」,「post_date」:「2017-02-07 12:17:29」},{「post_id」:「 421「,」post_title「:」前端開發者 - 數字藝術「,」post_date「:」2017-02-07 12:18:04「},{」post_id「:」431「,」post_title「:」美術指導「,」post_date「:」2017-02-07 12:18:19「}]} – Jou

回答

0

請與下面的代碼。我希望它會工作

private int parse(String data) { 
    try { 
     JSONObject jsonObject = new JSONObject(data); //get a response as Json Object and then get it as array by corresponding key 
     JSONArray ja = jsonObject.getJSONArray("result"); 
     JSONObject jo = null; 
     for (int i = 0; i < ja.length(); i++) { 
      jo = ja.getJSONObject(i); 
      String id = jo.getString("ID"); 
      String title = jo.getString("post_title"); 
      //String content = jo.getString("post_content"); 
      String date = jo.getString("post_date"); 
      Skill skill = new Skill(); 
      skill.setId(id); 
      skill.setTitle(title); 
      //skill.setContent(content); 
      skill.setDate(date); 
      skills.add(skill); 
      titles.add(title); 
     } 
     return 1; 
    } catch (JSONException e) { 
     Log.d("TAG", e.getMessage()); 
     return 0; 
    } 
} 
+0

不幸的是它不起作用 - >無法解析 – Jou

+0

你會請分享你的代碼。因爲我測試了我發佈的上述代碼。我需要你使用的網址 –

+0

我編輯了帖子並添加了php腳本。請檢查 – Jou

0

嘗試下面的代碼:

try { 
     Log.d("Jou", "result"); 
     JSONObject object = new JSONObject(data) 
     JSONArray ja = object.getJSONArray("result"); 
     JSONObject jo = null; 
     titles.clear(); 
     skills.clear(); 
     for (int i = 0; i < ja.length(); i++) { 
      jo = ja.getJSONObject(i); 
      String id = jo.getString("post_id"); 
      String title = jo.getString("post_title"); 
      //String content = jo.getString("post_content"); 
      String date = jo.getString("post_date"); 
      Skill skill = new Skill(); 
      skill.setId(id); 
      skill.setTitle(title); 
      //skill.setContent(content); 
      skill.setDate(date); 
      skills.add(skill); 
      titles.add(title); 
     } 
     return 1; 
    } catch (JSONException e) { 
     Log.d("Jou", e.getMessage()); 
     return 0; 
    } 
+0

我試過了,它不起作用 – Jou

+0

它顯示的是什麼錯誤? –

+0

我不知道什麼是錯的。 「無法解析」吐司節目,在日誌中:s = {「result」:[{「post_id」:「390」,「post_title」:「購物車」,「post_date」:「2017-02-07 12:17 :29「},{」post_id「:」421「,」post_title「:」前端開發人員 - 數字藝術「,」post_date「:」2017-02-07 12:18:04「},{」post_id「: 「431」,「post_title」:「美術指導」,「post_date」:「2017-02-07 12:18:19」}]} 03-25 08:39:33.291 16232-17338/com.example.joudialfattal .skillsexchange D/Joudi:結果 – Jou

0

有一些問題,你的JSON響應它走在POST_DATE多餘的字符嘗試驗證它在下面的鏈接。

http://www.jsoneditoronline.org/

+0

如何使用這個編輯器? – Jou

+0

只要打開鏈接並粘貼你的JSON響應,它會告訴你問題在哪裏。 – Jordon

+0

它顯示了一秒鐘然後它消失,然後我不知道什麼是錯的。 – Jou

相關問題