合并模板时为什么无法访问速度模板中的嵌套org.json.JSONObject属性

无法访问包含各种数组的JSONObject中的某些(不是全部)属性字段。

例外:

org.apache.velocity.exception.MethodinvocationException: 
  invocation of method 'get' in  class org.json.JSONObject threw exception org.json.JSONException: 
  JSONObject["published"] not found. at templates/eventbrite.vsl[line 43,column 41]

我已经尝试了来自各种Web资源的速度模板属性访问方法$object.published / "$object.published" / "{$object.published}的每个可构想的排列。

该项目的目标是[在服务器端]

  1. [工作完美]从网站/ API调用(例如Eventbrite)获取字符串json_result。
  2. [似乎也可以工作]由此创建一个org.json.JSONObject jsonObj 字符串。
  3. [作品]将其放置在VelocityContext上下文中。
  4. [不起作用]使用自定义模板从中提取所需数据 jsonObj。
  5. [在4没失败时有效]创建新的标准化JSON以符合其他事件API来源。事件json数据的Rosetta Stone。
public String publish(String templatePath,String json_result,Map model,VelocityContext context) throws IOException
{      
    Writer writer = new StringWriter();     
    try
    {
        JSONObject jsonObj = new JSONObject(json_result.trim());
        Object valueObj;

        for (String key : jsonObj.keySet())
        {
            valueObj = jsonObj.get(key.trim());
            context.put(key,valueObj);
            logger.info("Adding K,V: "+key+" in context");
        }

        Iterator it = model.entryset().iterator();
        while (it.hasnext())
        {
            Map.Entry pair = (Map.Entry) it.next();
            logger.info("Adding K,V: " + pair.getKey().toString() + "," + pair.getvalue() + " to velocity context");
            context.put(pair.getKey().toString(),pair.getvalue());
            it.remove(); /* avoids a ConcurrentModificationException */
        }
    // following line is where it all dies apparently

        velocityEngine.mergeTemplate(templatePath,"UTF-8",context,writer);
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }        

    writer.flush();
    String result = writer.toString();
    logger.info("\n[MERGED JSON START - "+templatePath+"]\n"+result+"\n[MERGED JSON END]\n");    
    return result;
}
符合

json_result 数据(为清晰起见,大量编辑):

[

    {
    "shareable":true,"is_series_parent":false,"description":{"html":"......<BR>really long description","text":"same minus html"
    },"source":"create_2.0","format_id":"6","logo":{},"end":{
    "utc":"2019-11-19T17:30:00Z","timezone":"America/New_York","local":"2019-11-19T12:30:00"
    },"currency":"USD","id":"68282188865","venue_id":"36369841","summary":"\u201cLady Bug: action Hero!\u201d is the story of a brave little lady bug and the animal friends that live with her in the rainforest. Performed by D","start":{
    "utc":"2019-11-19T16:30:00Z","local":"2019-11-19T11:30:00"
    },"published":"2019-08-07T15:13:47Z","version":"3.0.0"
    },{},{} 50 items total in this array...
]
d332426126 回答:合并模板时为什么无法访问速度模板中的嵌套org.json.JSONObject属性

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3168301.html

大家都在问