Android receiving a sub array from database using JSON

时间:2016-04-07 10:30:33

标签: android json

I can successfully use MySQL statement to select all the records i want. The format is as follow:

{"result[
          {"totalDis":"764.108207","totalTime":"36","totalCalories":"49.9464197344328","Weight":"65","Sex":"M","Age":"18","MaxSpeed":"44.16","LongestT":"00:00:10","FarthestDis":"144.7902",
           "result2":[{"speed":"6.00","distance":"12"},
                      {"speed":"9.00","distance":"27"},
                      {"speed":"14.48","distance":"144.7902"},
                      {"speed":"1.45","distance":"10.135314"},
                      {"speed":"1.95","distance":"140.6154"},
                      {"speed":"44.16","distance":"88.322022"}
            ]}
]}

I have received many data from database already using the code below, but the data i have received can be defined as they all have specific names, so i can get them based on their name. But in result2, the same field has more than one data, and i want to get them all and store it into different variables.

private void getData(){
    String url = Config.DATA_URL+user;

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            showJSON(response);
        }
    },
    new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error){
            Toast.makeText(MyProfile.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
        }
    });

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

private void showJSON(String response){
    String totalDis="";
    String totalTime="";
    String totalCalories="";
    String weight;
    String sex;

    String age="";
    String maxSpeed="";
    String longestT="";
    String farthestDis="";
    try {
        JSONObject jsonObject = new JSONObject(response);
        JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
        JSONObject collegeData = result.getJSONObject(0);
        totalDis = collegeData.getString(Config.KEY_TOTALDIS);
        totalTime = collegeData.getString(Config.KEY_TOTALTIME);
        totalCalories = collegeData.getString(Config.KEY_TOTALCALORIES);
        weight = collegeData.getString(Config.KEY_Weight);
        sex = collegeData.getString(Config.KEY_Sex);
        age = collegeData.getString(Config.KEY_Age);
        maxSpeed = collegeData.getString(Config.KEY_MaxSpeed);
        longestT = collegeData.getString(Config.KEY_LongestT);
        farthestDis = collegeData.getString(Config.KEY_FarthestDis);



        double tDis = Double.parseDouble(totalDis);
        tDis= Math.round(tDis);
        String ttDis = String.valueOf(tDis);

        double tCa = Double.parseDouble(totalCalories);
        tCa= Math.round(tCa);
        String ttCa = String.valueOf(tCa);

        double fDis = Double.parseDouble(farthestDis);
        fDis= Math.round(fDis);
        String ffDis = String.valueOf(fDis);

        double ms = Double.parseDouble(maxSpeed);
        ms= Math.round(ms);
        String mms = String.valueOf(ms);

        txtUserDetail.setText("Weight: " + weight + "kg\nSex: "+sex + "\nAge: "+age);
        txtTotalTime.setText("Total Distance: "+ttDis+"m\nTotal Time: "+totalTime+"s\nTotal Calories: "+ttCa);
        txtX.setText("Fastest Speed: "+mms+"m/s\nLongest Duration: "+longestT+"\nFarthest Distance: "+ffDis+"m");
    }
    catch (JSONException e){
        e.printStackTrace();
    }

Config.class

public class Config {
public static final String DATA_URL = "http://10.1.1.1/display.php?user=";
public static final String KEY_TOTALDIS = "totalDis";
public static final String KEY_TOTALTIME = "totalTime";
public static final String KEY_TOTALCALORIES = "totalCalories";
public static final String KEY_Weight = "Weight";
public static final String KEY_Sex = "Sex";
public static final String KEY_Age = "Age";
public static final String KEY_MaxSpeed = "MaxSpeed";
public static final String KEY_LongestT = "LongestT";
public static final String KEY_FarthestDis = "FarthestDis";
public static final String JSON_ARRAY = "result";
}

1 个答案:

答案 0 :(得分:0)

     JSONObject jsonObject=yourResult.getJsonObject();
    JSONArray jsonArray=jsonObject.getJSONArray("result2");
    for (int i = 0; i <jsonArray.length() ; i++) {
        JSONObject jsonObject=jsonArray.getJSONObject(i);
        String speed=jsonObject.getString("speed");
        String distance=jsonObject.getString("distance");
        speedArray.add(speed);
        distanceArray.add(distance);
    }

This will retreive the json results in corresponding arrays