什么是json_encode(在PHP中)的等价物? [英] What is the JSP equivalent to json_encode ( in PHP )?

查看:107
本文介绍了什么是json_encode(在PHP中)的等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将JSP servlet编码为JSON。 JSP中的json_encode()在PHP中的等价物是什么?

I am trying to encode a JSP servlet into JSON. What's the equivalent in JSP to json_encode() in PHP ?

推荐答案

JSP / Servlet不像PHP这样高级别实际上内置任何东西。在Java中,您可以更自由地从库中进行选择。 Java中有几个Java可用的JSON库,你可以在你的webapp中实现,每个 JSON.org Jackson Google Gson

JSP/Servlet isn't that high-level as PHP which has practically "anything built-in". In Java you've more freedom to choose from libraries. There are several JSON libraries in Java available which you can implement in your webapp, the popular ones being under each JSON.org, Jackson and Google Gson.

我们在这里使用Gson让我们满意。它对参数化集合和(嵌套)Javabeans提供了出色的支持。它基本上如下所示:

We use here Gson to our satisfaction. It has excellent support for parameterized collections and (nested) Javabeans. It's basically as simple as follows:

String json = new Gson().toJson(anyObject); // anyObject = List<Bean>, Map<K, Bean>, Bean, String, etc..
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

使用Gson将JSON转换为完整的Javabean也很简单,参见这个例子

Converting JSON to a fullworthy Javabean is also simple with Gson, see this example.

这篇关于什么是json_encode(在PHP中)的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆