android_json解析的服务端

来源:互联网 发布:c语言简单表白代码 编辑:程序博客网 时间:2024/06/11 00:00

1.User

2.JsonTools

package com.json.tools;import net.sf.json.JSONObject;public class JsonTools {public JsonTools() {// TODO Auto-generated constructor stub}/** * @param key 表示json字符串的头信息 * @param object 是对解析的集合的类开题  * @return */public static String createJsonString(String key,Object value){JSONObject jsonObject=new JSONObject();jsonObject.put(key, value);return jsonObject.toString();}}

3.JsonService

package com.json.service;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import com.json.domain.User;public class JsonService {public JsonService() {// TODO Auto-generated constructor stub}public User getUser(){User user=new User(1001, "jack", 32);return user;}public List<User> getListUser(){List<User> list=new ArrayList<User>();User user1=new User(1001, "jack", 32);User user2=new User(1002, "rose", 31);list.add(user1);list.add(user2);return list;}public List<String> getListString(){List<String> list=new ArrayList<String>();list.add("上海");list.add("北京");list.add("广州");return list;}public List<Map<String,Object>> getListMaps(){List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();Map<String,Object> map1=new HashMap<String, Object>();map1.put("id", 1001);map1.put("name", "jack");map1.put("age", 32);Map<String,Object> map2=new HashMap<String, Object>();map2.put("id", 1002);map2.put("name", "rose");map2.put("age", 31);list.add(map1);list.add(map2);return list;}}

4.JsonAction

package com.json.action;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.json.service.JsonService;import com.json.tools.JsonTools;public class JsonAction extends HttpServlet {private JsonService service;/** * Constructor of the object. */public JsonAction() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {this.doPost(request, response);}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. *  * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=utf-8");request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");PrintWriter out = response.getWriter();/*String jsonString=JsonTools.createJsonString("user", service.getUser());*/String jsonString="";String action_flag=request.getParameter("action_flag");if(action_flag.equals("user")){jsonString=JsonTools.createJsonString("user", service.getUser());}else if(action_flag.equals("users")){jsonString=JsonTools.createJsonString("users", service.getListUser());}else if(action_flag.equals("listString")){jsonString=JsonTools.createJsonString("listString", service.getListString());}else if(action_flag.equals("listmap")){jsonString=JsonTools.createJsonString("listmap", service.getListMaps());}out.println(jsonString);out.flush();out.close();}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code hereservice=new JsonService();}}

5.JsonTest

package com.json.test;import java.util.HashMap;import java.util.List;import java.util.Map;import com.json.domain.User;import com.json.service.JsonService;import com.json.tools.JsonTools;public class JsonTest {public JsonTest() {// TODO Auto-generated constructor stub}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubString msg="";String msg1="";String msg2="";String msg3="";JsonService jsonService=new JsonService();User user=jsonService.getUser();msg=JsonTools.createJsonString("user", user);List<User> list=jsonService.getListUser();msg1=JsonTools.createJsonString("user",list);List<String> list1=jsonService.getListString();msg2=JsonTools.createJsonString("user", list1);List<Map<String, Object>> list2=jsonService.getListMaps();msg3=JsonTools.createJsonString("user", list2);System.out.println(msg);System.out.println(msg1);System.out.println(msg2);System.out.println(msg3);}}

6.其输出结果:

{"user":{"age":32,"id":1001,"name":"jack"}}
{"user":[{"age":32,"id":1001,"name":"jack"},{"age":31,"id":1002,"name":"rose"}]}
{"user":["上海","北京","广州"]}
{"user":[{"id":1001,"age":32,"name":"jack"},{"id":1002,"age":31,"name":"rose"}]}


源码

0 0
原创粉丝点击