在JSP中呈现JSON数据的最佳实践是什么?

时间:2021-05-06 20:58:05

I need to render JSON data in my JSPs for some AJAX requests. I'd like to know what's the best way to do it in terms of easy to use and stability.

我需要在JSP中为一些AJAX请求呈现JSON数据。我想知道在易于使用和稳定性方面做到最好的方法是什么。

2 个解决方案

#1


1  

I'm assuming you have checked out json.org already...

我假设你已经检查过json.org了......

I recently switched from json-lib to XStream:

我最近从json-lib切换到XStream:

http://x-stream.github.io/

Definitely much easier. Just a few lines of code and you are done. Note, it started as an XML serializer, deserializer. It now supports JSON ouput.

绝对容易多了。只需几行代码即可完成。注意,它最初是作为XML序列化程序,解串器。它现在支持JSON输出。

#2


3  

Assuming you want to generate the JSON from one or more Java objects, the following is a fairly straightforward approach:

假设您要从一个或多个Java对象生成JSON,以下是一种相当简单的方法:

  • Set the Java object(s) as attributes in request/session scope
  • 将Java对象设置为请求/会话范围中的属性

  • Convert the objects to JSON using a tag library such as http://json-taglib.sourceforge.net/index.html
  • 使用标记库(如http://json-taglib.sourceforge.net/index.html)将对象转换为JSON

#1


1  

I'm assuming you have checked out json.org already...

我假设你已经检查过json.org了......

I recently switched from json-lib to XStream:

我最近从json-lib切换到XStream:

http://x-stream.github.io/

Definitely much easier. Just a few lines of code and you are done. Note, it started as an XML serializer, deserializer. It now supports JSON ouput.

绝对容易多了。只需几行代码即可完成。注意,它最初是作为XML序列化程序,解串器。它现在支持JSON输出。

#2


3  

Assuming you want to generate the JSON from one or more Java objects, the following is a fairly straightforward approach:

假设您要从一个或多个Java对象生成JSON,以下是一种相当简单的方法:

  • Set the Java object(s) as attributes in request/session scope
  • 将Java对象设置为请求/会话范围中的属性

  • Convert the objects to JSON using a tag library such as http://json-taglib.sourceforge.net/index.html
  • 使用标记库(如http://json-taglib.sourceforge.net/index.html)将对象转换为JSON