ÔÚ Java ÖÐʹÓà JSON
±¾½Ì³Ì½«»á½ÌÎÒÃÇÈçºÎʹÓà Java ±à³ÌÓïÑÔ±àÂëºÍ½âÂë JSON¡£ÈÃÎÒÃÇÏÈÀ´×¼±¸»·¾³ÒÔ±ãÕë¶Ô JSON ½øÐÐ Java ±à³Ì¡£
»·¾³ÔÚÎÒÃÇʹÓà Java ±àÂëºÍ½âÂë JSON ֮ǰ£¬ÎÒÃÇÐèÒª°²×°Ò»¸ö¿ÉÓÃµÄ JSON Ä£¿é¡£¶ÔÓÚÕâ¸ö½Ì³ÌÇëÏÂÔغͰ²×° JSON.simple£¬È»ºó°Ñ jsonsimple-1.1.1.jar ÎļþµÄ·¾¶Ìí¼Óµ½»·¾³±äÁ¿ CLASSPATH ÖС£
JSON ºÍ Java ʵÌåÓ³ÉäJSON.simple ʵÌåÓ³Éä´Ó×ó²àµ½ÓÒ²àΪ½âÂë»ò½âÎö£¬ÊµÌåÓ³Éä´ÓÓҲൽ×ó²àΪ±àÂë¡£
JSON Java
string java.lang.String
number java.lang.Number
true | false java.lang.Boolean
null null
array java.util.List
object java.util.Map
½âÂëʱ£¬java.util.List µÄĬÈϾßÌåÀàÊÇ org.json.simple.JSONArray£¬java.util.Map µÄĬÈϾßÌåÀàÊÇ org.simple.JSONObject¡£
ÔÚ Java ÖбàÂë JSONÏÂÃæÕâ¸ö¼òµ¥µÄʾÀýչʾÁËʹÓà java.util.HashMap µÄ×ÓÀà JSONObject ±àÂëÒ»¸ö JSON ¶ÔÏó¡£ÕâÀﲢûÓÐÌṩ˳Ðò¡£Èç¹ûÄãÐèÒªÑϸñµÄÔªËØ˳Ðò£¬ÇëʹÓà JSONValue.toJSONString(map) ·½·¨µÄÓÐÐòÓ³ÉäʵÏÖ£¬±ÈÈç java.util.LinkedHashMap¡£
import org.json.simple.JSONObject; class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name", "foo"); obj.put("num", new Integer(100)); obj.put("balance", new Double(1000.21)); obj.put("is_vip", new Boolean(true)); System.out.print(obj); } }±àÒëºÍÖ´ÐÐÉÏÃæµÄ³ÌÐòʱ£¬»áÉú³ÉÈçÏÂËùʾ½á¹û£º
{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"}ÏÂÃæÊÇÁíÒ»¸öʾÀý£¬Ê¹Óà Java JSONObject չʾÁË JSON ¶ÔÏóÁ÷£º
java import org.json.simple.JSONObject; class JsonEncodeDemo { public static void main(String[] args) { JSONObject obj = new JSONObject(); obj.put("name","foo"); obj.put("num",new Integer(100)); obj.put("balance",new Double(1000.21)); obj.put("is_vip",new Boolean(true)); StringWriter out = new StringWriter(); obj.writeJSONString(out); String jsonText = out.toString(); System.out.print(jsonText); } }±àÒëºÍÖ´ÐÐÉÏÃæµÄ³ÌÐòʱ£¬»áÉú³ÉÈçÏÂËùʾ½á¹û£º
{"balance": 1000.21, "num":100, "is_vip":true, "name":"foo"} ÔÚ Java ÖнâÂë JSONÏÂÃæµÄÀý×ÓʹÓÃÁË JSONObject ºÍ JSONArray£¬ÆäÖÐ JSONObject ¾ÍÊÇ java.util.Map£¬JSONArray ¾ÍÊÇ java.util.List£¬Òò´ËÎÒÃÇ¿ÉÒÔʹÓà Map »ò List µÄ±ê×¼²Ù×÷·ÃÎÊËüÃÇ¡£
import org.json.simple.JSONObject; import org.json.simple.JSONArray; import org.json.simple.parser.ParseException; import org.json.simple.parser.JSONParser; class JsonDecodeDemo { public static void main(String[] args) { JSONParser parser=new JSONParser(); String s = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]"; try{ Object obj = parser.parse(s); JSONArray array = (JSONArray)obj; System.out.println("The 2nd element of array"); System.out.println(array.get(1)); System.out.println(); JSONObject obj2 = (JSONObject)array.get(1); System.out.println("Field \"1\""); System.out.println(obj2.get("1")); s = "{}"; obj = parser.parse(s); System.out.println(obj); s= "[5,]"; obj = parser.parse(s); System.out.println(obj); s= "[5,,2]"; obj = parser.parse(s); System.out.println(obj); }catch(ParseException pe){ System.out.println("position: " + pe.getPosition()); System.out.println(pe); } } }±àÒëºÍÖ´ÐÐÉÏÃæµÄ³ÌÐòʱ£¬»áÉú³ÉÈçÏÂËùʾ½á¹û£º
The 2nd element of array {"1":{"2":{"3":{"4":[5,{"6":7}]}}}} Field "1" {"2":{"3":{"4":[5,{"6":7}]}}} {} [5] [5,2]ÉÏһƪ: ÔÚ Ruby ÖÐʹÓà J... ÏÂһƪ: JSON Óë Ajax
¡¡