[英]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) 我正在开发velocty和java,我是从json文件中获取值的(我使用JSON对象的递归并存储了递归的路径(请参阅下面的上下文以了解想法)
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 映射mp从json文件获得
{
"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 使用调试器,vm的上下文包含
"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 速度文件指定了“ root。*”,由于无法编辑它们,因此我正在使用以下递归程序来获取键
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 其中a是前缀,并使用
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: 使用您提供的Java初始化代码,Velocity模板应包含:
${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: 如果要使用
${root.primaryApplicant.firstName}
,则另一个引用也应以root为前缀,例如${root.firstName}
,并且Java上下文初始化代码应为:
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. 但是在这两种情况下,您还必须检查所使用的json库是否为Json对象提供了通用的getter,以便“点”运算符将在json对象上递归调用Java方法
get(<fieldname>)
。 There are tons of those . 有很多这些 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.