2016-11-21 13 views
-3

これは私のjsonレスポンスです。私は、これは私のPHPコードであるJSONレスポンスから値を取得する方法は?

"history":[["2","[email protected]","21.2299924","72.8247365","1479718230719"], 
["3","[email protected]","21.2299926","72.8247346","1479718265453"], 
["4","[email protected]","21.2299924","72.8247345","1479719800472"], 
["5","[email protected]","21.2299927","72.8247354","1479720302919"], 
["6","[email protected]","21.2299926","72.8247344","1479720880373"], 
["7","[email protected]","21.2299926","72.8247343","1479721139992"]]} 

...すべての配列からリストビューに3番目のインデックスの値を表示したい:

$email = $_POST['email']; 
     $con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or    die(mysqli_error($con)); 
     mysqli_select_db($con,DB_DATABASE) or die(mysqli_error($con)); 
     $result = mysqli_query($con,"SELECT * from tbl_userhistory WHERE email = '$email'"); 
     $no_of_rows = mysqli_num_rows($result); 
     if ($no_of_rows > 0) { 

     $rows = array(); 
     while ($row = mysqli_fetch_row($result)) { 
     $rows[] = $row; 
     } 
      $response["history"] =$rows; 
      echo json_encode($response); 

} 


    else { 

     return false; 
    } 

これは私が値を取得するコードです..: 私は応答を得ていますが、適切なフォーマットではありません。私はちょうど各列からlatitiude、経度lastupddateしたい。

private void getHistory(final String em){ 
    String tag_string_location= "group_history"; 
    pDialog.setMessage("Getting Group members history..."); 
    showDialog(); 
    StringRequest strReqLocation = new StringRequest(Request.Method.POST, 
      Config_URL.URL_REGISTER, new Response.Listener<String>(){ 

     @Override 
     public void onResponse(String response) { 
      Log.d("Activity_main", "getting group history " + response.toString()); 
      hideDialog(); 
      try { 
       JSONObject jObj = new JSONObject(response); 
       boolean error = jObj.getBoolean("error"); 
       if (!error) { 

        // String groupid = jObj.getString("groupid"); 
        JSONArray obj =jObj.getJSONArray("history"); 
        if (obj != null) { 
         for (int i=0;i<obj.length();i++){ 
          listdata.add(obj.get(i).toString()); 
         } 
        } 


        GroupAdapter gAdapter=new GroupAdapter(HistoryUser.this,listdata); 
        lvHistory.setAdapter(gAdapter); 

        Log.d("main", "New group " + obj); 

       } 
       else { 

        // Error occurred in registration. Get the error 
        // message 
        String errorMsg = jObj.getString("error_msg"); 
        Toast.makeText(getApplicationContext(), 
          errorMsg, Toast.LENGTH_LONG).show(); 
       } 
      }catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    },new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.e("MainAct", "group Error: " + error.getMessage()); 
      Toast.makeText(getApplicationContext(), 
        error.getMessage(), Toast.LENGTH_LONG).show(); 
      hideDialog(); 
     } 
    }){ 
     @Override 
     protected Map<String, String> getParams() { 
      // Posting params to register url 
      Map<String, String> params = new HashMap<String, String>(); 
      params.put("tag", "gethistory"); 
      // params.put("name", name); 
      params.put("email", em); 


      // params.put("password", password); 

      return params; 
     } 

    }; 

    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(strReqLocation, tag_string_location); 
} 
+1

と何を**これまで**自分を試してみましたか? –

+0

json_decodeが配列を返します – Ima

+0

JSONタグでほぼすべての回答を見るかもしれません – RiggsFolly

答えて

0
$x = '{"history":[["2","[email protected]","21.2299924","72.8247365","1479718230719"],["3","[email protected]","21.2299926","72.8247346","1479718265453"],["4","[email protected]","21.2299924","72.8247345","1479719800472"],["5","[email protected]","21.2299927","72.8247354","1479720302919"],["6","[email protected]","21.2299926","72.8247344","1479720880373"],["7","[email protected]","21.2299926","72.8247343","1479721139992"]]}'; 
echo "<pre>"; 
print_r(json_decode($x, true)); 
exit; 

Array 
(
    [history] => Array 
     (
      [0] => Array 
       (
        [0] => 2 
        [1] => [email protected] 
        [2] => 21.2299924 
        [3] => 72.8247365 
        [4] => 1479718230719 
       ) 

      [1] => Array 
       (
        [0] => 3 
        [1] => [email protected] 
        [2] => 21.2299926 
        [3] => 72.8247346 
        [4] => 1479718265453 
       ) 

      [2] => Array 
       (
        [0] => 4 
        [1] => [email protected] 
        [2] => 21.2299924 
        [3] => 72.8247345 
        [4] => 1479719800472 
       ) 

      [3] => Array 
       (
        [0] => 5 
        [1] => [email protected] 
        [2] => 21.2299927 
        [3] => 72.8247354 
        [4] => 1479720302919 
       ) 

      [4] => Array 
       (
        [0] => 6 
        [1] => [email protected] 
        [2] => 21.2299926 
        [3] => 72.8247344 
        [4] => 1479720880373 
       ) 

      [5] => Array 
       (
        [0] => 7 
        [1] => [email protected] 
        [2] => 21.2299926 
        [3] => 72.8247343 
        [4] => 1479721139992 
       ) 

     ) 

) 

今、あなたはjson_decodeを使用し、これを試してみてください、あなたが

+0

私の編集した質問を再度ご覧ください。 –

0

を必要とし、これまでどのようなデータを抽出することができるはずですが、次のようにこれは、あなたの配列を与えるだろう。

$data = json_decode($json, true); 
foreach($data as $val) 
{ 
    foreach($val as $dat_val) 
    {  
     print_r($dat_val); 
     echo "\n"; 
    } 
} 

DEMO

関連する問題