2016-11-21 2 views
-3

이것은 내 json 응답입니다. 나는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"]]} 

이 내 PHP 코드입니다 ... 모든 배열에서리스트 뷰에서 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

관련 문제