java返回json和json数组
1 package winclpt;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.text.SimpleDateFormat;
7
8 import javax.servlet.ServletInputStream;
9 import javax.servlet.ServletOutputStream;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import net.sf.json.JSONArray;
14 import net.sf.json.JSONObject;
15 import net.sf.json.JsonConfig;
16 import net.sf.json.processors.JsonValueProcessor;
17 import wumart.base.BasePO;
18
19 public class BaseService extends wumart.base.BaseService{
20 final protected JsonConfig jsonConfig = new JsonConfig();
21 private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22 public BaseService(){
23 jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() { //处理单个Date对象
24 public Object processObjectValue(String key, Object date, JsonConfig jsonConfig) {
25 return value == null ?"" : sd.format(date);
26 }
27 public Object processArrayValue(Object date, JsonConfig jsonConfig) { //处理数组中的Date对象
28 return null;
29 }
30 });
31 }
32 protected void ReturnPO(HttpServletResponse response, BasePO po) { //返回json
33 ServletOutputStream out = null;
34 try {
35 response.setContentType("text/json; charset=UTF-8");
36 out = response.getOutputStream();
37
38 JSONObject json = JSONObject.fromObject(po , jsonConfig);
39 out.write(json.toString().getBytes("UTF-8"));//
40
41 } catch (IOException e) {
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 } finally {
45 if (out != null)
46 try {
47 out.close();
48 } catch (IOException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52 }
53 out = null;
54 }
55
56 protected void ReturnPOs(HttpServletResponse response, Object[] pos) { //返回json数组
57 ServletOutputStream out = null;
58 try {
59 response.setContentType("text/json; charset=UTF-8");
60 out = response.getOutputStream();
61 JSONArray array = JSONArray.fromObject(pos , jsonConfig);
62 out.write(array.toString().getBytes("UTF-8"));
63
64 } catch (Exception e) {
65 // TODO Auto-generated catch block
66 e.printStackTrace();
67 } finally {
68 if (out != null)
69 try {
70 out.close();
71 } catch (IOException e) {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 }
75 }
76 out = null;
77 }
78
79 protected JSONObject GetJsonObject(HttpServletRequest request) {
80 String json = getJson(request);
81 JSONObject reqJson = JSONObject.fromObject(json);
82 return reqJson;
83 }
84
85 protected String getJson(HttpServletRequest request) {
86
87 try {
88 ServletInputStream inputStream = (ServletInputStream) request
89 .getInputStream();
90 InputStreamReader streamReader = new InputStreamReader(inputStream,
91 "utf-8");
92 BufferedReader reader = new BufferedReader(streamReader);
93 StringBuffer sb = new StringBuffer("");
94 String temp;
95 while ((temp = reader.readLine()) != null) {
96 sb.append(temp);
97 }
98 reader.close();
99 reader = null;
100 streamReader.close();
101 streamReader = null;
102 inputStream.close();
103 inputStream = null;
104
105 return sb.toString();
106
107 } catch (IOException e) {
108 // TODO Auto-generated catch block
109 e.printStackTrace();
110 }
111
112 return null;
113 }
114 }
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.text.SimpleDateFormat;
7
8 import javax.servlet.ServletInputStream;
9 import javax.servlet.ServletOutputStream;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import net.sf.json.JSONArray;
14 import net.sf.json.JSONObject;
15 import net.sf.json.JsonConfig;
16 import net.sf.json.processors.JsonValueProcessor;
17 import wumart.base.BasePO;
18
19 public class BaseService extends wumart.base.BaseService{
20 final protected JsonConfig jsonConfig = new JsonConfig();
21 private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
22 public BaseService(){
23 jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() { //处理单个Date对象
24 public Object processObjectValue(String key, Object date, JsonConfig jsonConfig) {
25 return value == null ?"" : sd.format(date);
26 }
27 public Object processArrayValue(Object date, JsonConfig jsonConfig) { //处理数组中的Date对象
28 return null;
29 }
30 });
31 }
32 protected void ReturnPO(HttpServletResponse response, BasePO po) { //返回json
33 ServletOutputStream out = null;
34 try {
35 response.setContentType("text/json; charset=UTF-8");
36 out = response.getOutputStream();
37
38 JSONObject json = JSONObject.fromObject(po , jsonConfig);
39 out.write(json.toString().getBytes("UTF-8"));//
40
41 } catch (IOException e) {
42 // TODO Auto-generated catch block
43 e.printStackTrace();
44 } finally {
45 if (out != null)
46 try {
47 out.close();
48 } catch (IOException e) {
49 // TODO Auto-generated catch block
50 e.printStackTrace();
51 }
52 }
53 out = null;
54 }
55
56 protected void ReturnPOs(HttpServletResponse response, Object[] pos) { //返回json数组
57 ServletOutputStream out = null;
58 try {
59 response.setContentType("text/json; charset=UTF-8");
60 out = response.getOutputStream();
61 JSONArray array = JSONArray.fromObject(pos , jsonConfig);
62 out.write(array.toString().getBytes("UTF-8"));
63
64 } catch (Exception e) {
65 // TODO Auto-generated catch block
66 e.printStackTrace();
67 } finally {
68 if (out != null)
69 try {
70 out.close();
71 } catch (IOException e) {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 }
75 }
76 out = null;
77 }
78
79 protected JSONObject GetJsonObject(HttpServletRequest request) {
80 String json = getJson(request);
81 JSONObject reqJson = JSONObject.fromObject(json);
82 return reqJson;
83 }
84
85 protected String getJson(HttpServletRequest request) {
86
87 try {
88 ServletInputStream inputStream = (ServletInputStream) request
89 .getInputStream();
90 InputStreamReader streamReader = new InputStreamReader(inputStream,
91 "utf-8");
92 BufferedReader reader = new BufferedReader(streamReader);
93 StringBuffer sb = new StringBuffer("");
94 String temp;
95 while ((temp = reader.readLine()) != null) {
96 sb.append(temp);
97 }
98 reader.close();
99 reader = null;
100 streamReader.close();
101 streamReader = null;
102 inputStream.close();
103 inputStream = null;
104
105 return sb.toString();
106
107 } catch (IOException e) {
108 // TODO Auto-generated catch block
109 e.printStackTrace();
110 }
111
112 return null;
113 }
114 }