java如何返回json格式
展开全部
在Java中,可以使用Jackson库将Java对象转换为JSON格式。例如,假设有一个名为person的Person对象,可以使用以下代码将其转换为JSON格式:
import com.fasterxml.jackson.databind.ObjectMapper;ObjectMapper mapper = new ObjectMapper();String json = mapper.writeValueAsString(person);
这段代码中,ObjectMapper类是Jackson库中的一个核心类,用于将Java对象转换为JSON格式。首先创建一个ObjectMapper对象,然后调用其writeValueAsString方法将Person对象转换为JSON格式的字符串。最后将该字符串返回即可。
需要注意的是,如果要将Java对象转换为JSON格式,需要为该对象添加getter方法,以便ObjectMapper类能够访问该对象的属性。另外,需要在pom.xml文件中添加Jackson库的依赖,例如:
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version></dependency>
import com.fasterxml.jackson.databind.ObjectMapper;ObjectMapper mapper = new ObjectMapper();String json = mapper.writeValueAsString(person);
这段代码中,ObjectMapper类是Jackson库中的一个核心类,用于将Java对象转换为JSON格式。首先创建一个ObjectMapper对象,然后调用其writeValueAsString方法将Person对象转换为JSON格式的字符串。最后将该字符串返回即可。
需要注意的是,如果要将Java对象转换为JSON格式,需要为该对象添加getter方法,以便ObjectMapper类能够访问该对象的属性。另外,需要在pom.xml文件中添加Jackson库的依赖,例如:
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version></dependency>
展开全部
例如:
Student st1 = new Student(1, "dg", 18, new Date());
Student st2 = new Student(2, "dg", 18, new Date());
Student st3 = new Student(3, "dg", 18, new Date());
Student st4 = new Student(4, "dg", 18, new Date());
Student st5 = new Student(5, "dg", 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put("Result", "OK");
ma.put("Records", Ja);
JSONObject js = new JSONObject(ma);
out.print(js);
返回结果:
{"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}
Student st1 = new Student(1, "dg", 18, new Date());
Student st2 = new Student(2, "dg", 18, new Date());
Student st3 = new Student(3, "dg", 18, new Date());
Student st4 = new Student(4, "dg", 18, new Date());
Student st5 = new Student(5, "dg", 18, new Date());
List li = new ArrayList();
JSONObject JO1 = new JSONObject(st1);
JSONObject JO2 = new JSONObject(st2);
JSONObject JO3 = new JSONObject(st3);
JSONObject JO4 = new JSONObject(st4);
JSONObject JO5 = new JSONObject(st5);
li.add(JO1);
li.add(JO2);
li.add(JO3);
li.add(JO4);
li.add(JO5);
JSONArray Ja = new JSONArray(li);
Map ma = new HashMap();
ma.put("Result", "OK");
ma.put("Records", Ja);
JSONObject js = new JSONObject(ma);
out.print(js);
返回结果:
{"Result":"OK","Records":[{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":1},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":2},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":3},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":4},{"recordDate":"Fri Dec 16 17:54:39 CST 2011","name":"dg","age":18,"personId":5}]}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public static void returnJson(HttpServletResponse response, CharSequence text)
throws Exception {
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-store");
response.setHeader("Pragrma", "no-cache");
response.setDateHeader("Expires", 0);
response.getWriter().write(text.toString());
}
其中text这个是JSONObject或者Map
response.setContentType("application/json;charset=UTF-8");这个表示以json格式返回
前台需要eval接收到的数据,就是json格式了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-15
展开全部
在后台拼接好JSON字符串后,直接用out输出到界面,
Writer out = = response.getWriter();
if(str!=null){
out.write(jsonstr);
out.flush();
}
Writer out = = response.getWriter();
if(str!=null){
out.write(jsonstr);
out.flush();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-07-15
展开全部
0.0 json是一种javaweb的技术吧 不沾边吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询