JSON JavaÓëAJAX(Jquery)
Ò»¡¢JSON ÊÇʲô£¿
JSON µÄÈ«³ÆÊÇJavaScript Object Notation£¬ÊÇÒ»ÖÖÇáÁ¿¼¶µÄÊý¾Ý½»»»¸ñʽ¡£
JSON ÓëXML ¾ßÓÐÏàͬµÄÌØÐÔ£¬ÀýÈçÒ×ÓÚÈ˱àдºÍÔĶÁ£¬Ò×ÓÚ»úÆ÷Éú³ÉºÍ½âÎö¡£µ«ÊÇJSON ±È
XML Êý¾Ý´«ÊäµÄÓÐЧÐÔÒª¸ß³öºÜ¶à¡£JSON ÍêÈ«¶ÀÁ¢Óë±à³ÌÓïÑÔ£¬Ê¹ÓÃÎı¾¸ñʽ±£´æ¡£
JSON Êý¾ÝÓÐÁ½Öֽṹ£º
• Name-Value ¶Ô¹¹³ÉµÄ¼¯ºÏ£¬ÀàËÆÓÚJava ÖеÄMap¡£
• Value µÄÓÐÐòÁÐ±í£¬ÀàËÆÓÚJava ÖеÄArray¡£
Ò»¸öJSON ¸ñʽµÄÊý¾ÝʾÀý£º
{
"Name": "Apple",
"Expiry": "2007/10/11 13:54",
"Price": 3.99,
"Sizes": [
"Small",
"Medium",
"Large"
]
}
¸ü¶à¹ØÓÚJSON Êý¾Ý¸ñʽµÄ˵Ã÷²Î¿´JSON ¹Ù·½ÍøÕ¾£ºhttp://www.json.org£¨ÖÐÎÄ
ÄÚÈݲο´£ºhttp://www.json.org/json-zh.html£©
¶þ¡¢Í¨¹ýjavaÀ´´´½¨JSON¶ÔÏó
1.ÒýÈëjar°ü
ÎÒÕâÀïʹÓõÄÊÇjson-lib-2.3-jdk15.jar£¬ÏÂÔصØÖ·£ºhttp://sourceforge.net/projects/json-lib/files/
Json-lib requires (at least) the following dependencies in your classpath:
jakarta commons-lang 2.4
jakarta commons-beanutils 1.7.0
jakarta commons-collections 3.2
jakarta commons-logging 1.1.1
ezmorph 1.0.6
2.ÖØÒªµÄ¶ÔÏó¼°·½·¨
1£©JSONObject£ºJSON¶ÔÏó{}¡£
2£©JSONArray£ºJSONÊý×é¶ÔÏó£¬[{},{}]¡£
3£©fromObject(object)£º½«¶ÔÏóת»»ÎªJSON¶ÔÏó¡£
4£©JSONObject.accumulate(key,value)£ºÏòJSONObjectÖÐÔö¼ÓJSONÊý¾Ý£¬¿ÉÒÔÖظ´¡£
5£©element(key,value)£ºÏòJSON¶ÔÏóÖÐÔö¼ÓJSONÊý¾Ý£¬Èç¹ûÖظ´ºóÒ»¸ö»áÌ滻ǰһ¸ö¡£
6£©toString(i,i)£º½«JSON¶ÔÏóת»»Îª×Ö·û´®£¬Èç¹û°üº¬²ÎÊý£¬Êǽ«ÆäÃÀ»¯ºóÊä³ö¡£
ÒÔÏÂÊÇÒ»¸öservletÊä³öJSONµÄÀý×Ó£º
package com.netqin.function.demo.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import com.netqin.function.demo.model.People;
import com.netqin.function.demo.model.Phone;
public class JsonServlet extends
Ïà¹ØÎĵµ£º
ËùÓеijÌÐò¿ª·¢Êֲᶼ°üº¬Á˸÷ÖÖ¹æÔò¡£Ò»Ð©Ï°¹ß×ÔÓɳÌÐòÈËÔ±¿ÉÄܶÔÕâЩ¹æÔòºÜ²»ÊÊÓ¦£¬µ«ÊÇÔÚ¶à¸ö¿ª·¢ÈËÔ±¹²Í¬Ð´×÷µÄÇé¿öÏ£¬ÕâЩ¹æÔòÊDZØÐèµÄ¡£Õâ²»½ö½öÊÇΪÁË¿ª·¢Ð§ÂÊÀ´¿¼ÂÇ£¬¶øÇÒÒ²ÊÇΪÁ˺óÆÚά»¤¿¼ÂÇ¡£
ÃüÃû¹æ·¶
¶¨ÒåÕâ¸ö¹æ·¶µÄÄ¿µÄÊÇÈÃÏîÄ¿ÖÐËùÓеÄÎĵµ¶¼¿´ÆðÀ´ÏñÒ»¸öÈËдµÄ£¬Ôö¼Ó¿É¶ÁÐÔ£¬¼õÉÙÏîÄ¿×éÖÐÒòΪ ......
<FORM METHOD="post" ACTION="input.jsp">
ÇëÊäÈëÐÅÏ¢:
<input type="text" id="biao">
<input type="submit" name="Submit" value="Ìá½»">
</FORM>
ÒÔÉÏΪinput.htm Îļþ
ÒÔÏÂΪinput.jsp Îļþ
<%
String str = request.getParameter("biao");
&nbs ......
×Öĸ ÈÕÆÚ»òʱ¼äÔªËØ ±íʾ ʾÀý
G
Era ±êÖ¾·û
Text
AD
y
Äê
Year
1996; 96
M
ÄêÖеÄÔ·Ý
Month
July; Jul; 07
w
ÄêÖеÄÖÜÊý
Number
27
W
Ô·ÝÖеÄÖÜÊý
Number
2
D
ÄêÖеÄÌìÊý
Number
189
d
Ô·ÝÖеÄÌìÊý
Number
10
F
Ô·ÝÖеÄÐÇÆÚ
Number
2
E
ÐÇÆÚÖ ......
1.É¢Áм¯HashSet
HashSetÀàÔÚjava.util°üÖÐ
A.¹¹Ôì·½·¨£ºHashSet()
B.³£Ó÷½·¨
public boolean add(Object o)£ºÏò¼¯ºÏÖÐÌí¼ÓÖ¸¶¨ÔªËØo
&nb ......
ÏÂÔØ dom4j-1.6.1.jar¡£
1: package org.zzp.common.xml.dom4j;
2:
3: import java.io.FileWriter;
4: import java.io.IOException;
5: import org.dom4j.Document;
6: import org.dom4j.DocumentHelper;
7: import org.dom4j.Element;
8: import org.dom4j.io.OutputFormat;
9: impor ......