将速度响应转换为 JSON

发布于 2024-12-26 06:01:05 字数 1311 浏览 3 评论 0原文

我正在使用 struts 2 和velocity 模板来生成 JSON 响应。 现在的问题是响应不是使用某些速度 JSON 插件生成的 它只是一个字符串,一旦速度完成解析和渲染后就会出现 响应,在客户端我执行 eval 来获取从字符串到 JSON 的响应。

我真正需要的是速度或支柱方面的一些解决方案,一旦结果是 由速度生成,框架应该调用我的 API,我可以使用我自己的逻辑将 vm 文件的响应输出转换为 JSON。如何实现这一目标?

例如:

  1. 在使用 JavaScript 的浏览器上,我设计了一个树形小部件,用于以树形结构显示注释。
  2. 假设用户点击评论按钮。
  3. 我的 UI 小部件将触发 AJAX 来获取评论数据。
  4. 该请求被 STRUTS 2 框架拦截。
  5. 例如,它将调用 getComments() 操作 API,并使用注释对象(例如 cmt)填充 arrayList。
  6. 现在响应由速度模板(*.vm)处理。
  7. 现在在虚拟机中我正在编写这样的代码:

    { "评论数据" : [
    
        #set($sep="")
        #foreach($c in $cmt)
        $九月
        {
            “commentText”:$c.getText()
        }
        #set($sep=",")
        #结尾
    
    }
    
  8. 现在最终的响应可能是这样的:

    { "评论数据" : [
    
        {
            "commentText" : "这是评论 1"
        },
        {
            "commentText" : "这是评论 2"
        },
        {
            "commentText" : "这是评论 3"
        },
        {
            "commentText" : "这是评论 4"
        }`
    
        ]
    }
    
  9. 现在这可能看起来像 JSON,但它不是严格的 JSON;我的意思是如果我错过了 然后在 JavaScript 客户端的某个 , 处,我的 eval 可能会失败或 JSON.parse() 会失败,但在速度模板上我现在知道 JSON 是否格式错误。

  10. 因此,一旦生成上述速度模板,我需要一些控制,我可以在其中编写一些 Java 代码来对响应进行一些验证。

我发现我使用速度模板生成 JSON 输出(实际上是一个看起来像 JSON 的字符串)的方法可能是错误的。但我仍然需要处理我编写的每个速度模板的响应。

I am using struts 2 and velocity templates to generate JSON response.
Now the catch is the response is not generated using some velocity JSON plugin
it's just a String that comes out once velocity is done with its parsing and rendering of
response, and on client side I do eval to get the response from string to JSON.

What I really need is some solution on velocity's or struts' side where, once the result is
generated by velocity, the framework should call my API where I can convert the response output of vm file into JSON using my own logic. How do achieve this?

For example:

  1. On browser using JavaScript I have designed a tree widget that I use for displaying comments in tree structure.
  2. Say user clicks on comments button.
  3. My UI widget will fire an AJAX to get data for comments.
  4. This request is intercepted by STRUTS 2 framework.
  5. It will call, say, getComments() action API and will populate an arrayList with comment object say cmt.
  6. Now the response is handled by a velocity template(*.vm).
  7. Now in vm I am writing code like this:

    { "CommentsData" : [
    
        #set($sep="")
        #foreach($c in $cmt)
        $sep
        {
            "commentText" : $c.getText()
        }
        #set($sep=",")
        #end
    
    }
    
  8. Now the final response may turn out like this:

    { "CommentsData" : [
    
        {
            "commentText" : "This is comment 1"
        },
        {
            "commentText" : "This is comment 2"
        },
        {
            "commentText" : "This is comment 3"
        },
        {
            "commentText" : "This is comment 4"
        }`
    
        ]
    }
    
  9. Now this may look like JSON, but its not strict JSON; I mean if I miss
    some , somewhere then on client side in JavaScript my eval might fail or JSON.parse()
    will fail, but on velocity template I have now clue if JSON is malformed.

  10. So once the above velocity template is generated I need some control, where I can write some Java code to do some validations on the response.

I see that my approach to use velocity template to generate JSON output (actully a String that looks like JSON) may be wrong. But still I need to handle the response of every velocity template I have written.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝天白云 2025-01-02 06:01:05

不确定你如何使用速度。我们在输出 JSON 时不使用速度;我们只需创建一个 JSON 可转换对象并使用response.write(jsonObject.toJson()) 直接从控制器输出它。这样,始终会生成正确的 JSON。

Not sure how you are using velocity. We don't use velocity when outputting JSON; we just create a JSON convertible object and output it directly from controllers using response.write(jsonObject.toJson()). This way, proper JSON is always generated.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文