0

I am working on velocty and java, I was to take values from an json file (which I took using recursion of JSON OBjects and storing path of recursion(See context below to get idea)

String hhh="I am a  ${root.primaryApplicant.firstName} ${firstName} ";
Velocity.init();
VelocityContext vm=new VelocityContext();

 for(String m : mp.keySet())
        {
            vm.put(m,mp.get(m));
        }

StringWriter w = new StringWriter();
Velocity.evaluate( vm, w, "mystring", forma );

The map mp is obtained from a json file

{
  "id": 45288,
  "code": null,
  "name": null,
  "external": false,
  "leadReferenceId": "APPID1573716175142",
  "createdBy": null,
  "createdDate": "2017-10-26T12:14:17.000Z",
  "agentName": null,
  "ipAddress": null,
  "applicationType": "HOME",
  "loanType": "HOME",
  "applicantType": {
    "id": 269,
    "code": "Single",
    "name": "Single",
    "external": false
  },
  "relationship": null,
  "primaryApplicant": {
    "id": 45289,
    "code": null,
    "name": null,
    "external": false,
    "existingCustomer": null,
    "customerId": null,
    "partyRoleType": {
      "id": 348,
      "code": "0",
      "name": "PRIMARY",
      "external": false
    },
    "partyRelationshipType": null,
    "salutation": {
      "id": 289,
      "code": "MR",
      "name": "Mr",
      "external": false
    },
    "firstName": "Anuj",
    "middleName": "",
    "lastName": "singh",
    "dateOfBirth": "1986-12-11T18:30:00.000Z",
    "genderType": {

using a debugger the context of vm contains

"root.primaryApplicant.firstName" -> "Anuj"
"firstName" -> "Anuj"

after Velocity evaluate I get

"I am a  ${root.primaryApplicant.firstName} Anuj ";

i am assuming it cant replace keys with . in between. Is there any better way to populate the string

----------------

The velocity file has a "root.*" specified and since I cant edit those, I am using the following recursion program to get the keys


    private static void rexuse(String a,Map<String, Object> mp,JSONObject js,String parent) throws JSONException {
        
        
        Iterator<String> keys = js.keys();
        while(keys.hasNext()) {
            String key = keys.next();
            if(key.equals("name"))
            {
                mp.put(parent,js.get(key)); 
            }
            if (js.get(key) instanceof JSONObject) {
                rexuse(a+"."+key,mp,js.getJSONObject(key),key);
            }
            else
            {
                if(!mp.containsKey(key) ||( mp.containsKey(key) && mp.get(key)==null))
                    mp.put(key, js.get(key));           
                mp.put(a+"."+key, js.get(key) );
            }
        }
        
    }

where a is the prefix and called the above using

String a="root";
rexuse(a,mp,js,"root");
Community
  • 1
  • 1
user34096
  • 15
  • 5

1 Answers1

0

There is an inconsistency, here.

With the Java initialization code you give, the Velocity template should contain:

${primaryApplicant.firstName}

If you want to use ${root.primaryApplicant.firstName}, then the other reference should also be prefixed by root, as in ${root.firstName}, and the Java context initialization code should be:

vm.put("root", mp);

But in both cases you must also check that the json library you are using provides a Json object with a generic getter, so that the 'dot' operator will recursively call the Java method get(<fieldname>) on the json object. There are tons of those.

Claude Brisson
  • 4,085
  • 1
  • 22
  • 30
  • Hey thanks for replying, actually I am using a recursion program that is appending a "root." in the starting because I cant edit the velocity file. I have attached my program to get root in an edit – user34096 Nov 27 '19 at 03:54
  • @user34096 Try to debug by just displaying `$root`, or `$primaryApplicant`, to see which are defined and what they contain. – Claude Brisson Nov 27 '19 at 12:40