如何从JSON响应中获取值?

时间:2016-11-21 09:47:43

标签: android json

这是我的json回复。我想在每个数组的listview中显示第三个索引的值...

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

这是我的PHP代码:

$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,经度最后一行从每一行开始。

 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);
}

2 个答案:

答案 0 :(得分:0)

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

这将为您提供如下数组

Array
(
    [history] => Array
        (
            [0] => Array
                (
                    [0] => 2
                    [1] => mega@gmail.com
                    [2] => 21.2299924
                    [3] => 72.8247365
                    [4] => 1479718230719
                )

            [1] => Array
                (
                    [0] => 3
                    [1] => mega@gmail.com
                    [2] => 21.2299926
                    [3] => 72.8247346
                    [4] => 1479718265453
                )

            [2] => Array
                (
                    [0] => 4
                    [1] => mega@gmail.com
                    [2] => 21.2299924
                    [3] => 72.8247345
                    [4] => 1479719800472
                )

            [3] => Array
                (
                    [0] => 5
                    [1] => mega@gmail.com
                    [2] => 21.2299927
                    [3] => 72.8247354
                    [4] => 1479720302919
                )

            [4] => Array
                (
                    [0] => 6
                    [1] => mega@gmail.com
                    [2] => 21.2299926
                    [3] => 72.8247344
                    [4] => 1479720880373
                )

            [5] => Array
                (
                    [0] => 7
                    [1] => mega@gmail.com
                    [2] => 21.2299926
                    [3] => 72.8247343
                    [4] => 1479721139992
                )

        )

)

现在,您应该能够提取所需的数据

答案 1 :(得分:0)

试试这个,使用json_decode。

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

<强> DEMO