How to loop and get the specific value of the json object ,how can i use that json object to get the other entity present in that json object? [duplicate]

青春壹個敷衍的年華 提交于 2019-12-12 02:56:02

问题


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


来源:https://stackoverflow.com/questions/30757085/how-to-loop-and-get-the-specific-value-of-the-json-object-how-can-i-use-that-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!