JSON的使用

1.JSON在Java中的使用

 1 package com.zdsofe.json;
 2 
 3 import net.sf.json.JSONArray;
 4 import net.sf.json.JSONObject;
 5 
 6 public class Json {
 7    
 8     public static void main(String[] args) {
 9         String json="{'name':'张三','sex':'女','adress':{'city':'武汉','street':'汉阳'}}";
10         JSONObject jion=JSONObject.fromObject(json);
11         System.out.println(jion.get("name"));
12         System.out.println(jion.get("adress"));
13         JSONObject jjon=JSONObject.fromObject(jion.get("adress"));
14         System.out.println(jjon.get("city"));
15         
16          int[] a={1,2,3,4,5};
17          JSONArray jjjo= JSONArray.fromObject(a);
18          System.out.println(jjjo.toString());
19     }
20 }
View Code

2.JSON在jsp中的使用

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <script type="text/javascript" src="../js/jquery-1.7.2.js"></script>
10 <script type="text/javascript">
11 $(function(){
12     $(":button").click(function(){
13         
14         var arr=[];
15         var hobbys=$(":checked");
16         if(hobbys.length>0)
17         {
18             
19             for(var i=0;i<hobbys.length;i++)
20             {
21                 arr.push(hobbys[i].value);
22             }
23         }
24         
25         var array=[];
26         array.push({name:"userName",value:$(":text").val()});
27         array.push({name:"password",value:$(":password").val()});
28         array.push({name:"hobby",value:arr});
29         $.get(
30                 
31                 "../servlet1",
32                 array,
33                 function(data)
34                 {
35                     
36                     if(data)
37                     {
38                         
39                         alert(data); 
40                         
41                     }
42                 },
43         
44                 "text"
45         
46         
47             );
48     });
49     
50 })
51 </script>
52 <body>
53 <form action="">
54 名字:<input type="text" name="userName"/><br/>
55 密码:<input type="password" name="password"/><br/>
56 爱好:<input type="checkbox" name="hobby" value="01">游泳
57     <input type="checkbox" name="hobby" value="02">打游戏
58     <input type="checkbox" name="hobby" value="03">睡觉
59     <input type="checkbox" name="hobby" value="04">喝水<br/>
60     <button type="button">点击</button>    
61 </form>
62 </body>
63 </html>
View Code
 1 package com.zdsofe.servlet;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 import java.util.Iterator;
 6 import java.util.Map;
 7 import java.util.Set;
 8 
 9 import javax.servlet.ServletException;
10 import javax.servlet.annotation.WebServlet;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 
15 /**
16  * Servlet implementation class servlet1
17  */
18 @WebServlet("/servlet1")
19 public class servlet1 extends HttpServlet {
20     private static final long serialVersionUID = 1L;
21        
22     /**
23      * @see HttpServlet#HttpServlet()
24      */
25     public servlet1() {
26         super();
27         // TODO Auto-generated constructor stub
28     }
29 
30     /**
31      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
32      */
33     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
34 
35         Map<String, String[]>  map= request.getParameterMap();
36         Set<String> keys= map.keySet();
37         Iterator<String> it= keys.iterator();
38         PrintWriter out=response.getWriter();
39        while(it.hasNext())
40        {
41         String str=(String) it.next();
42         String [] s=map.get(str);
43         
44         for(int i=0;i<s.length;i++)
45         {
46             out.print(s[i]);
47         }
48         
49         
50        }
51        
52         
53         
54     }
55 
56     /**
57      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
58      */
59     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
60         // TODO Auto-generated method stub
61     }
62 
63 }
View Code

 

posted @ 2017-08-05 15:18  初夏的一棵歪脖子树  阅读(143)  评论(0编辑  收藏  举报