1

I am fetching some json from server like this

    "applications": [
        {
          "packageName": "com.facebook.mlite",
          "defaultPermissionPolicy": "PROMPT",
          "delegatedScopes": [
            "DELEGATED_SCOPE_UNSPECIFIED",
            "CERT_INSTALL",
            "MANAGED_CONFIGURATIONS",
            "BLOCK_UNINSTALL",
            "PERMISSION_GRANT",
            "PACKAGE_ACCESS",
            "ENABLE_SYSTEM_APP"
          ],
          "permissionGrants": [
            {
              "permission": "tt",
              "policy": "PROMPT"
            }
          ],
          "disabled": false,
          "minimumVersionCode": 0
        },


 {
      "packageName": "com.facebook.mlite",
      "defaultPermissionPolicy": "PROMPT",
      "delegatedScopes": [
        "DELEGATED_SCOPE_UNSPECIFIED",
        "CERT_INSTALL",
        "MANAGED_CONFIGURATIONS",
        "BLOCK_UNINSTALL",
        "PERMISSION_GRANT",
        "PACKAGE_ACCESS",
        "ENABLE_SYSTEM_APP"
      ],
      "permissionGrants": [
        {
          "permission": "tt",
        }
      ],
     }
      ]

Now there is a json array "application":[] in which there are several json object. Now these object are not same. Some json objects are missing like first object contains installType but second one doesn't. Now i want to add this in a list for a recyclerview if a json object is missing i want to send empty tring in contrustor of my pojo class

   public Application(String defaultPermissionPolicy, List<String> delegatedScopes, List<com.ariaware.enrolldevice.PolicyPojos.PermissionGrants> permissionGrants, Boolean disabled, String installType, Integer minimumVersionCode, String packageName) {
        this.defaultPermissionPolicy = defaultPermissionPolicy;
        this.delegatedScopes = delegatedScopes;
        PermissionGrants = permissionGrants;
        this.disabled = disabled;
        this.installType = installType;
        this.minimumVersionCode = minimumVersionCode;
        this.packageName = packageName;
    }

This is constructor of my class. Now how will i loop through json array and check either if an object exists or not or if doesn't exist then send empty string. I need to check every object

6
  • add the code that you use to fetch, what library do you use? Commented Apr 23, 2020 at 16:19
  • i m using android managment api to fetch the data ... i m just trying to figuring out to loop through json array and check if json object exist and then add in list class Commented Apr 23, 2020 at 16:21
  • Okay then show some code on how do you fetch the data, to know how to help. Commented Apr 23, 2020 at 16:26
  • can u come on teamviewer. I can't show whole code here. If u come on teamviewer it will be ease for me Commented Apr 23, 2020 at 16:31
  • I want that other people see the problem in your question, I don't want the whole code....I want the part where you get a call back and the response.....just a part of this callback. Commented Apr 23, 2020 at 16:33

1 Answer 1

1

You could implement another constructor which accepts a JSONObject as a parameter and create the object. Inside the constructor use optString which returns an empty string if the field doesn't exist (also accepts another parameter for the fallback value).

public Application(JSONObject jsonObject) {
    this.installType = jsonObject.optString("installType");

    // example of an array
    JSONArray scopes = jsonObject.optJSONArray("delegatedScopes");
    this.delegatedScopes = new ArrayList<>();
    for (int i = 0; i < scopes.length(); i++)
        this.delegatedScopes.add(scopes.optString(i));

    //other initialization...
}

Finally, you retrieve each JSONObject from the applications array.

try {
    JSONArray res = data.optJSONArray("applications");
    Application[] items =  new Application[res.length()];
    for (int i = 0; i < res.length(); i++)
        items[i] = new Application(res.getJSONObject(i));
    } catch (JSONException e) {
        e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

9 Comments

let me try for it and then tell u response back
and what about json array inside json object
As it's inside the JSONObject, you have access to it in the constructor. Let me edit the answer.
it is giving me exception W/System.err: org.json.JSONException: No value for delegatedScopes . as i told if json object doesn't exist then i need to send empty string
as i my question i posted json objects can be missing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.