如何解析DWR返回的对象数组?

发布于 2024-07-09 21:46:56 字数 103 浏览 6 评论 0 原文

DWR 非常直接地处理原语列表。 我无法确定 DWR 方法调用返回的对象数组是否表示 JSON 对象。 有什么线索吗? 或者我是否必须将表示对象数组的 JSON 字符串返回给浏​​览器?

DWR handles lists of primitives quite straight forward. I could not find whether array of objects returned by a DWR method call represent a JSON object. Any clues? Or do I have to return a JSON string representing the array of objects back to the browser?

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

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

发布评论

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

评论(1

淡淡の花香 2024-07-16 21:46:56

这个答案有点晚了,但这里是:)

好消息:DWR 还以一种非常直接的方式处理 Java 数组和集合。 只需返回它们,在客户端您将获得 JavaScript 数组对象。 (在典型情况下,例如数组或集合中的基元或字符串。如果内容更奇特,您可能需要定义转换器;更多内容见下文。)

以下是 DWR 文档(强调我的):

默认情况下,以下所有内容都是
无需进一步为您转换
声明:

  • 所有原始类型,boolean、int、double 等。
  • 这些布尔值、整数等的基于类的版本。
  • java.lang.String
  • java.util.Date 和 3 个 SQL 衍生物
  • 上述数组
  • 上述集合(列表、集合、映射、迭代器等)
  • 来自 DOM、XOM、JDOM 和 DOM4J 的 DOM 对象(例如 Element 和 Document)

因此,您绝对不需要这些 JSON 字符串(尽管这可能是对于更复杂的数据结构来说是一个不错的选择)。

实际上,您可以返回更多类型的对象,而无需执行大量手动工作,因为 DWR 附带“转换器< /a>”用于许多典型用途。 例如,要使自定义“bean”样式 Java 对象在客户端 JS 中工作,您只需在 dwr.xml 中说明您要使用 bean 转换器

<convert converter="bean" match="com.company.YourBean" />

即使你的方法返回这些 bean 对象的列表(或数组)

public static List<YourBean> getData(){ ... }

......上面的配置就够了,还不错。

This answer is a little late, but here goes :)

Good news: DWR also handles Java arrays and Collections in a really straight-forward way. Just return them and on client side you'll get JavaScript Array objects. (In typical cases like primitives or Strings inside your array or Collection, that is. If the contents is something more exotic, you may need to define converters; more below.)

Here's a quote from DWR documentation (emphasis mine):

By default all of the following are
converted for you without further
declaration:

  • All primitive types, boolean, int, double, etc.
  • The Class based versions of the these Boolean, Integer, etc.
  • java.lang.String
  • java.util.Date and the 3 SQL derivatives
  • arrays of the above
  • Collections (Lists, Sets, Maps, Iterators, etc) of the above
  • DOM objects (like Element and Document) from DOM, XOM, JDOM and DOM4J

So you definitely won't need JSON strings for these (although that may be a good option for more complicated data structures).

You can actually return many more kinds of objects without doing a lot of manual work because DWR comes with "converters" for lots of typical uses. For example, to make your custom "bean" style Java objects work in client-side JS, all you need to say in dwr.xml is that you want to use the bean converter:

<convert converter="bean" match="com.company.YourBean" />

Even if your method returns a List (or array) of those bean objects...

public static List<YourBean> getData(){ ... }

... the above configuration suffices, which is pretty nice.

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