json-ioÓÐÁ½¸öÖ÷ÒªµÄÀ࣬һ¸ö¶ÁºÍÒ»¸öд£¬ÅųýÁËʹÓÃObjectInputStreamºÍObjectOutputStreamÁ½¸öÀàÈ¥¶Áд¡£Json-ioÄÜÐòÁл¯ÈÎÒâµÄJava¶ÔÏóͼ£¨graph£©×ª±ä³ÉJSON£¬²¢ÇÒÄܼÇÒäÍêÕûµÄÓïÒåͼ£¨graphsemantics£©ºÍ¶ÔÏóÀàÐÍ¡£ÏÂÔصØÖ·£º
Ëü²»ÐèÒªÆäËûÒÀÀµ¡£
Àý×Ó£ºJava¶ÔÏóתJSON
1
2
3
4
5
6
JsonjsonObj=newJson();
System.out.println("ConvertJavaobjecttoJSONformatandsavetofile");
try(JsonWriterwriter=newJsonWriter(newFileOutputStream("c:json-io.json"))){
writer.write(jsonObj);
}catch(IOExceptione){
}
JSONתJava¶ÔÏó
1
2
3
4
5
6
System.out.println("ReadJSONfromfile,convertJSONstringbacktoobject");
try(JsonReaderreader=newJsonReader(newFileInputStream(newFile("c:json-io.json")))){
jsonObj=(Json)reader.readObject();
}catch(FileNotFoundExceptione){
}catch(IOExceptione){
}
ËüµÄÎĵµÉÏ˵£¬Json-io±ÈJDKµÄObjectInputStreamºÍObjectOutputStreamµÄÐòÁл¯²Ù×÷Òª¿ì£¬ÎÒÃǽ«»áÔÚºóÃæµÄÎÄÕÂÖÐ˵Ã÷¡£
£¨6£©Ê¹ÓÃGensonÀà¿â
GensonÊÇÒ»¸ö¿ÉÀ©Õ¹µÄ£¬¿ÉÉìËõµÄ£¬Ò×ÓÚʹÓõĿªÔ´¿â¡£³ý´ËÖ®Í⣬GensonÍêÕûÖ§³ÖÁË·ºÐÍ£¬Ö§³ÖJSONÔÚJAX-RSµÄʵÏÖ£¬Ö§³ÖJAXBµÄ×¢ÊÍ£¨annotation£©ºÍÀàÐÍ£¨types£©£¬²¢ÇÒÔÊÐíÐòÁл¯ºÍ·´ÐòÁл¯ÓµÓи´Ôӹؼü×ÖµÄmap¡£
ÏÂÔصØÖ·£º£¬ËüûÓÐÈκÎÒÀÀµ¡£
Àý×Ó£ºJava¶ÔÏóתJSON
1
2
3
4
5
6
7
JsonjsonObj=newJson();
Gensongenson=newGenson();
System.out.println("ConvertJavaobjecttoJSONformatandsavetofile");
try(FileWriterwriter=newFileWriter("c:genson.json")){
writer.write(genson.serialize(jsonObj));
}catch(IOException|TransformationExceptione){
}
JSONתJava¶ÔÏó
1
2
3
4
5
6
System.out.println("ReadJSONfromfile,convertJSONstringbacktoobject");
try(BufferedReaderreader=newBufferedReader(newFileReader("c:genson.json"))){
jsonObj=genson.deserialize(reader,Json.class);
}catch(FileNotFoundExceptione){
}catch(IOException|TransformationExceptione){
}
£¨7£©Ê¹ÓÃJSONiJÀà¿â
×îºóÒ»¸öÌÖÂÛµÄÊÇJSONiJ¡£JSONiJÊÇJSONµÄ½âÎöÆ÷£¬Ò»¸öJPathºÍMarshallerµÄʵÏÖ£¬ÄÜʵÏÖJava¶ÔÏóºÍJSONµÄÏ໥ת»»¡£ÏÂÔصØÖ·£ºhttps://bitbucket.org/jmarsden/jsonij/downloads
Ëü²»ÐèÒªÈκÎÒÀÀµ¡£
Àý×Ó£ºJava¶ÔÏóתJSON
1
2
3
4
5
6
JsonjsonObj=newJson();
System.out.println("ConvertJavaobjecttoJSONformatandsavetofile");
try(FileWriterwriter=newFileWriter("c:jsonij.json")){
writer.write(JSONMarshaler.marshalObject(jsonObj).toJSON());
}catch(IOException|JSONMarshalerExceptione){
}
JSONתJava¶ÔÏó
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
System.out.println("ReadJSONfromfile,convertJSONstringbacktoobject");
try(BufferedReaderreader=newBufferedReader(newFileReader("c:jsonij.json"))){
JSONjson=JSON.parse(reader);
//NowweneedtoparsetheJSONObjectobjectandputvaluesback
//toourJsonobject
for(Fieldfield:jsonObj.getClass().getDeclaredFields()){
try{
field.setAccessible(true);
field.set(field.getName(),json.get(field.getName()));
}catch(IllegalArgumentException|IllegalAccessExceptione){
}
}
}catch(FileNotFoundExceptione){
}catch(IOException|ParserExceptione){
}
¿´ÆðÀ´JSONiJÐèÒªµÄ´úÂë¶àЩ£¬ÐÔÄÜÔõôÑù£¬ÎÒÃÇ¿´ÏÂÃæ¡£
£¨¶þ£©»ù×¼²âÊÔ
ÏÖÔÚÎÒÃÇÒªÀ´¿´¿´ÐÔÄÜÁË£¬²âÊÔÓ²¼þÅäÖãºIntelCorei5laptopwith2.50GHzµ¥Í¨µÀDDR3RAM4G£¬Èí¼þÅäÖãºWindows7Ultimate64-bitSP1
»ù×¼²âÊÔÔËÐÐÒ»ÑùµÄÐéÄâ»ú£¨JVM£©£¬ÔÚ²âÊÔ֮ǰ£¬Ã¿Ò»¸öÀà¿â¶¼ÓÐÒ»¸öÈÈÉí£¬È¥ÏÞÖÆÄÚ´æʹÓõÄÔì³ÉµÄÓ°Ï죬ÓÃÒ»¸öÏÔʽµ÷ÓÃÀ¬»øÊÕ¼¯Æ÷¡£ÏÂÃæµÄͼ±í´ú±íµÄÊÇÐòÁл¯ºÍ·´ÐòÁл¯JSONÊý¾ÝÒÔºÁÃ뼶ʹÓÃ50´Îµü´úºÍ10´ÎÈÈÉí£¨warm-up£©µü´úµÄƽ¾ùµÄʱ¼ä¡£
£¨ÒëÕß×¢£ººìɫΪÐòÁл¯£¨Java¶ÔÏóתJSON£©£¬À¶É«Îª·´ÐòÁл¯£¨JSONתJava¶ÔÏ󣩣©
ÉÏÃæµÄͼ±íÏÔʾ£¬FlexjsonÐòÁл¯Ð¡Êý¾ÝʱÊÇ×î¿ìµÄ£¬¶øJSON-libÊÇ×îÂýµÄ¡£·´ÐòÁл¯µÄʱºò£¬Gson×î¿ì£¬JSON-lib»¹ÊÇ×îÂýµÄ¡£
ÏÂÃæµÄͼ±í´ú±íµÄÊÇÎÒÃǵÄÊý¾ÝÔÚ287kbʱ£¬ÐòÁл¯ºÍ·´ÐòÁл¯Ëù»¨·ÑµÄƽ¾ùʱ¼ä¡£
ÕâÕÅͼÏÔʾ£¬ÎÒÃǶÔÉÙÁ¿µÄÊý¾Ý²Ù×÷ʱ£¬×î¿ìµÄÊÇGson,Ö®ºóµÄÊÇGensonºÍFlexjson¡£
¡¡