Difficulty in generating a JSON object in the correct order

0

I'm trying to generate a JSON object by following this template:

{
    "schemas": [
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User",
        "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
    ],
    "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User": {
        "userLoginAttemptsCounter": 0,
        "ldapCommonNameGenerated": 0,
        "userPasswordResetAttemptsCounter": 0,
        "ldapCommonName": "System Administrator",
        "passwordWarnDate": "2015-06-30T01:51:27.000-07:00",
        "lastSuccessfulLoginDate": "2015-03-11T00:00:00.000-07:00",
        "homeOrganization": {
            "value": "1",
            "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1"
        },
        "passwordPolicyDescription": [
            {
                "value": "Password must not match or contain first name."
            }
        ],
        "disabled": false,
        "dataLevel": "2",
        "organizations": [
            {
                "value": "1",
                "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1",
                "display": "Xellerate Users"
            }
        ]
    }

}

but I'm getting this template:

{
    "schema": [
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User",
        "urn:ietf:params:scim:schemas:core:2.0:User",
        "urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User",
        "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
    ],
    "urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User": {
        "userLoginAttemptsCounter": 0,
        "ldapCommonNameGenerated": 0,
        "passwordWarnDate": "2015-06-30T01:51:27.000-07:00",
        "homeOrganization": {
            "value": "1",
            "$ref": "http:\HOST_NAME:PORT\idaas\im\scim\v1\Organizations\1"
        },
        "lastSuccessfulLoginDate": "2015-03-11T00:00:00.000-07:00",
        "organizations": [
            {
                "display": "Xellerate Users",
                "value": "1",
                "$ref": "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1"
            }
        ],
        "disabled": false,
        "ldapCommonName": "System Administrator",
        "passwordPolicyDescription": [
            {
                "value": "Password must not match or contain first name."
            }
        ],
        "userPasswordResetAttemptsCounter": 0,
        "dataLevel": "2"
    }
}

My code:

import org.json.JSONException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

public class Main {

    public static void main(String[] args) throws JSONException {

        //obj principal
        JSONObject obj = new JSONObject();
        //obj usuario
        JSONObject objuser = new JSONObject();
        objuser.put("userLoginAttemptsCounter", 0);
        objuser.put("ldapCommonNameGenerated", 0);
        objuser.put("userPasswordResetAttemptsCounter", 0);
        objuser.put("ldapCommonName", "System Administrator");
        objuser.put("passwordWarnDate", "2015-06-30T01:51:27.000-07:00");
        objuser.put("lastSuccessfulLoginDate", "2015-03-11T00:00:00.000-07:00");


        JSONObject objcompuser = new JSONObject();
        objcompuser.put("value", "1");
        objcompuser.put("$ref", "http:\HOST_NAME:PORT\idaas\im\scim\v1\Organizations\1");


        objuser.put("homeOrganization", objcompuser);

        JSONObject psw_des = new JSONObject();
        psw_des.put("value", "Password must not match or contain first name.");

        JSONObject org_des = new JSONObject();
        org_des.put("value", "1");
        org_des.put("$ref", "http://HOST_NAME:PORT/idaas/im/scim/v1/Organizations/1");
        org_des.put("display", "Xellerate Users");
        //obj headers
        JSONArray headers = new JSONArray();
        headers.add("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User");
        headers.add("urn:ietf:params:scim:schemas:core:2.0:User");
        headers.add("urn:ietf:params:scim:schemas:extension:oracle:2.0:IDM:User");
        headers.add("urn:ietf:params:scim:schemas:extension:enterprise:2.0:User");
        obj.put("schema", headers);
        obj.put("urn:ietf:params:scim:schemas:extension:oracle:2.0:OIG:User", objuser);

        JSONArray objpass = new JSONArray();
        objpass.add(psw_des);
        JSONArray objorg = new JSONArray();
        objorg.add(org_des);

        objuser.put("passwordPolicyDescription", objpass);
        objuser.put("disabled", false);
        objuser.put("dataLevel", "2");
        objuser.put("organizations", objorg);
        System.out.println(obj.toJSONString());
    }
}

Obs (I'm using the simple-json lib)

    
asked by anonymous 22.11.2017 / 17:25

1 answer

1

According to the JSON specification :

  

An object is an unordered collection of zero or more name / value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

That is, the order of the values of an object is irrelevant because an object is an unordered collection. You should not have to worry about it.

If you really need a JSON for some service that has been implemented wrongly , you can use a LinkedHashMap when creating your JSONObject.

    
23.11.2017 / 14:20