How to loop and get the specific value of the json

2019-09-18 15:50发布

问题:

This question already has an answer here:

  • How to loop through JSON array? 8 answers
  1. I am using java.

  2. I have pasted below response for reference. I need to loop the below JSON Array response.

  3. I can able to get the full response. But, I need to access the device Type first for example:deviceType=android and then using that device type I need to get the id of that particular device type for example: id=16.

Response:
{
  "BannerConfigurations": [
    {
      "id": 16,
      "partnerId": 69,
      "appId": "28470216",
      "affiliateData": "",
      "status": true,
      "deviceType": "ios",
      "daysHidden": 15,
      "daysReminder": 30
    },
    {
      "id": 161,
      "partnerId": 69,
      "appId": "com.android.news",
      "affiliateData": "",
      "status": true,
      "deviceType": "android",
      "daysHidden": 15,
      "daysReminder": 30
    }
  ]
}

回答1:

I'm assuming you are starting with the full JSONObject but you can skip the first line if you've for the banner configurations array.

JSONObject data = [insert something following your structure above];
JSONArray bannerConfigurations = data.get("BannerConfigurations");
for (int i=0; i<bannerConfigurations.length(); i++) {
    JSONObject device = bannerConfigurations.getJSONObject(i);
    String deviceType = device.getString("deviceType");
    int id = device.getInt("id");
    // do stuff!
}


回答2:

JSON is a Serialization Format. This means you should rarely analysis JSON directly.

The typical pattern would be to deserialize the JSON into Java objects and then use those in your analysis. Maybe even using a object query API such as QueryDSL on the resulting Collection for example.



回答3:

first create a class called Devices that contains setters and getters for all of the device attributes and a constructor "contract class"

then in your main class "Activity" define an instance:

static ArrayList<Devices> jsonDevice = new ArrayList<Devices>();

then in your AsyncTask-> onPostExecute() method write down this code:

try {
                jsonTicList.clear();
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {

                    JSONObject jObject = jArray.getJSONObject(i);
                    String id = jObject.getString("id");
                    String pid = jObject.getString("partnerId");
                    String adata = jObject.getString("affiliateData");
                    String status = jObject.getString("status");
                    String detype = jObject.getString("deviceType");
                    String datype = jObject.getString("daysHidden");
                    String darem = jObject.getString("daysReminder");
                    Devices tic = new Devices(id, pid,
                            adata,status,detype,datype,darem);
                    jsonDevice.add(tic);

                }

                this.progressDialog.dismiss();
            } catch (JSONException e) {
                Log.e("JSONException", "Error: " + e.toString());
            }

then loop around the data:

ArrayList<Devices> d = new ArrayList<Devices>(jsonDevice);
    for (Devices d1 : d) {
        if(!d1.getDeviceType().equals("Android")){
       //DO What ever you want 
         }}