JSON

ÔÚ Java ÖÐʹÓà JSON

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-04-18 12:06 ÎÒÒªÆÀÂÛ( )

±¾½Ì³Ì½«»á½ÌÎÒÃÇÈçºÎʹÓÃJava±à³ÌÓïÑÔ±àÂëºÍ½âÂëJSON¡£ÈÃÎÒÃÇÏÈÀ´×¼±¸»·¾³ÒÔ±ãÕë¶ÔJSON½øÐÐJava±à³Ì¡£ÔÚÎÒÃÇʹ...

ÔÚ 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

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Java EE¿ª·¢¼¼Êõ

    Java EE¿ª·¢¼¼Êõ

    2017-02-11 13:00

  • lib½âÎöjsonÊý¾Ý»ñµÃµ±ÌìÌìÆøÇé¿ö

    lib½âÎöjsonÊý¾Ý»ñµÃµ±ÌìÌìÆøÇé¿ö

    2015-10-03 08:21

  • Ajax+JSON»ù´¡ÊµÕ½ÊÓƵ½Ì³Ì

    Ajax+JSON»ù´¡ÊµÕ½ÊÓƵ½Ì³Ì

    2015-09-20 18:45

  • JSON ÖÐÎÄ°æ

    JSON ÖÐÎÄ°æ

    2015-09-13 11:07

ÍøÓѵãÆÀ
¢