简体   繁体   中英

How to populate velocity string with a JSON Object in JAVA?

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");

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM