2017-02-01 73 views
1

我已經創建了一個php腳本,並用java撿起它,但我很難將它轉換爲我可以使用的格式。android解析使用php生成的JSON

PHP

<?php 
//PDO is a extension which defines a lightweight, consistent interface for accessing databases in PHP. 
$db=new PDO('mysql:dbname=mydb;host=localhost;','root',''); 
//here prepare the query for analyzing, prepared statements use less resources and thus run faster 
$row=$db->prepare('select * from drinks'); 

$row->execute();//execute the query 
$json_data=array();//create the array 
foreach($row as $rec)//foreach loop 
{ 
$json_array['drinks_id']=$rec['drinks_id']; 
    $json_array['drink_name']=$rec['drink_name']; 
    $json_array['Description']=$rec['Description'];  
//here pushing the values in to an array 
    array_push($json_data,$json_array); 

} 

//built in PHP function to encode the data in to JSON format 
//print_r($json_array); 
echo json_encode($json_data,JSON_FORCE_OBJECT); 
?> 

JSON樣品(每行是accumative,而不是下一個certian標籤名稱爲)在Android中

{ 
    "0":{ 
     "drinks_id":"1", 
     "drink_name":"Uprising Treason West Coast IPA", 
     "Description":"Beer" 
    }, 
    "1":{ 
     "drinks_id":"2", 
     "drink_name":"Flying Dog Snake Dog IPA", 
     "Description":"Beer" 
    }, 
    "2":{ 
     "drinks_id":"3", 
     "drink_name":"Crafty Dan 13 Guns America IPA", 
     "Description":"Beer" 
    }, 
    "3":{ 
     "drinks_id":"4", 
     "drink_name":"Sixpoint Resin Double IPA", 
     "Description":"Beer" 
    }, 
    "4":{ 
     "drinks_id":"5", 
     "drink_name":"Sixpoint Bengali IPA", 
     "Description":"Beer" 
    }, 
    "5":{ 
     "drinks_id":"6", 
     "drink_name":"ShipYard ", 
     "Description":"Beer" 
    }, 
    "6":{ 
     "drinks_id":"7", 
     "drink_name":"Blue Moon Belgian White ", 
     "Description":"Beer" 
    }, 
    "7":{ 
     "drinks_id":"8", 
     "drink_name":"BrewDog Punk IPA ", 
     "Description":"Beer" 
    }, 
    "8":{ 
     "drinks_id":"9", 
     "drink_name":"Lagunitas IPA", 
     "Description":"Beer" 
    }, 
    "9":{ 
     "drinks_id":"10", 
     "drink_name":"Brooklyn Larger ", 
     "Description":"Larger" 
    }, 
    "10":{ 
     "drinks_id":"11", 
     "drink_name":"Hazy Hog Cloudy English Cider ", 
     "Description":"Cider" 
} 
} 

方法

private void parseJson() { 
     DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
     HttpPost httppost = new HttpPost(url); 
// Depends on your web service 
     httppost.setHeader("Content-type", "application/json"); 

    InputStream inputStream = null; 
    String result = null; 
    try { 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 

     inputStream = entity.getContent(); 
     // json is UTF-8 by default 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 
     Log.i(TAG, "readline : "+reader.readLine());   String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
      Log.i(TAG, "printLine: "+line); 
     } 
     result = sb.toString(); 
     JSONObject jObject = new JSONObject(result); 
     JSONArray jArray = jObject.getJSONArray("0"); //I BELIVE THIS IS THE ERROR BUT UNCLEAR HOW TO FIX IT 
     for (int i=0;i < jArray.length(); i++) 
     { 
      try { 
       JSONObject oneObject = jArray.getJSONObject(i); 
       // Pulling items from the array 
       int oneObjectsItem = oneObject.getInt("drinks_id"); 
       String oneObjectsItem2 = oneObject.getString("drink_name"); 
       String oneObjectsItem3 = oneObject.getString("Description"); 
       Log.i(TAG, "parseJson: "+oneObjectsItem2); 
      } catch (JSONException e) { 
       Log.e(TAG, "parseJson1: " +e.getMessage()); 
      } 
     } 
    } catch (Exception e) { 
     Log.e(TAG, "parseJson2: "+e.getMessage()); 
    } 
    finally { 
     try{ 
      if(inputStream != null) 
       inputStream.close();}catch(Exception squish){ 
      Log.e(TAG, "parseJson3: "+squish.getMessage()); 
     } 
    } 
} 

登錄貓的樣品(僅一行是否打印?)

E/Menu: parseJson2: Value {"drinks_id":"1","drink_name":"Uprising Treason West Coast IPA","Description":"Beer"} at 0 of type org.json.JSONObject cannot be converted to JSONArray 
+1

你沒有'JSONArray',你有一個'JSONObject' 。如果你改變你的JSON以''''''開始和結束,並且你移除了''0「,''1」'等等,我相信你的代碼應該可以工作 – 0xDEADC0DE

回答

1

這種方式嘗試PHP創建關聯數組,

<?php 
    $object=array(); 
    $i=0; 
    while($i<10){ 
    $employee = array("name"=>"hassan", "Designation"=>"Software Engineer"); 
    $i++; 
    $object[]=$employee; 
    } 
    $orginal["data"]=$object; 
    echo json_encode($orginal) 
?> 

輸出將被

{"data":[{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"},{"name":"hassan","Designation":"Software Engineer"}]} 
+0

當我在明天的時候 –

+0

聽起來不錯。快樂的編碼。 – Noorul