JSON

FastJson 介绍

字号+ 作者:H5之家 来源:H5之家 2017-08-02 16:00 我要评论( )

FastJson 介绍,Fast json 介绍Fastjson是一个 Java 语言编写的JSON 处理器 , 由 阿里巴巴 公司开发。 1、遵循 http://json.org 标准,为其官方 网站收录 的参考

Fastjson的最主要的使用入口是 com.alibaba.fastjson.JSON

importcom.alibaba.fastjson.JSON;

把 文本 文本 文本 序列化为 序列化为带格式的 转换为 JSONObject 或者 JSONArray

1 import java.util.*; Foo { 4 private String vString = "vStringhehhehe"; vchar = 'x'; vbyte = 64; vshort = 128; vint = 65535; vlong = 9999999L; vfloat = 12.1f; vdouble = 22.203d; vboolean = false; Date dddd = new Date(); 19 private Date vDate = new Date(); 20 private Date v_Date = new Date(); 21 private Object vnull = null; String[] avString = { "aaa", "bbb", "ccc" }; [] avint = { 1, 2, 3, 4 }; [] avboolean = { true, false, true, true }; List<String> listString = new ArrayList<String>(); 28 private Map<String, String> map = new HashMap<String, String>(); Bar bar = new Bar(); 31 private Bar[] avBar = { new Bar(), new Bar() }; 32 private List<Bar> listBar = new ArrayList<Bar>(); 33 34 { 35 listString.add("listString1"); 36 listString.add("listString2"); 37 listString.add("listString3"); 38 39 map.put("x", "s11111x"); 40 map.put("y", "s22222y"); 41 map.put("z", "s33333z"); 42 43 listBar.add(new Bar()); 44 listBar.add(new Bar()); 45 listBar.add(new Bar()); 46 } String getvString() { 49 return vString; 50 } setvString(String vString) { 53 this.vString = vString; 54 } getVchar() { 57 return vchar; 58 } setVchar(char vchar) { 61 this.vchar = vchar; 62 } getVbyte() { 65 return vbyte; 66 } setVbyte(byte vbyte) { 69 this.vbyte = vbyte; 70 } getVshort() { 73 return vshort; 74 } setVshort(short vshort) { 77 this.vshort = vshort; 78 } getVint() { 81 return vint; 82 } setVint(int vint) { 85 this.vint = vint; 86 } getVlong() { 89 return vlong; 90 } setVlong(long vlong) { 93 this.vlong = vlong; 94 } getVfloat() { 97 return vfloat; 98 } setVfloat(float vfloat) { 101 this.vfloat = vfloat; 102 } getVdouble() { 105 return vdouble; 106 } setVdouble(double vdouble) { 109 this.vdouble = vdouble; 110 } isVboolean() { 113 return vboolean; 114 } setVboolean(boolean vboolean) { 117 this.vboolean = vboolean; 118 } Date getDddd() { 121 return dddd; 122 } setDddd(Date dddd) { 125 this.dddd = dddd; 126 } Date getV_Date() { 129 return v_Date; 130 } setV_Date(Date v_Date) { 133 this.v_Date = v_Date; 134 } Date getvDate() { 137 return vDate; 138 } setvDate(Date vDate) { 141 this.vDate = vDate; 142 } Object getVnull() { 145 return vnull; 146 } setVnull(Object vnull) { 149 this.vnull = vnull; 150 } String[] getAvString() { 153 return avString; 154 } setAvString(String[] avString) { 157 this.avString = avString; 158 } [] getAvint() { 161 return avint; 162 } setAvint(int[] avint) { 165 this.avint = avint; 166 } [] getAvboolean() { 169 return avboolean; 170 } setAvboolean(boolean[] avboolean) { 173 this.avboolean = avboolean; 174 } List<String> getListString() { 177 return listString; 178 } setListString(List<String> listString) { 181 this.listString = listString; 182 } Map<String, String> getMap() { 185 return map; 186 } setMap(Map<String, String> map) { 189 this.map = map; 190 } Bar getBar() { 193 return bar; 194 } setBar(Bar bar) { 197 this.bar = bar; 198 } Bar[] getAvBar() { 201 return avBar; 202 } setAvBar(Bar[] avBar) { 205 this.avBar = avBar; 206 } List<Bar> getListBar() { 209 return listBar; 210 } setListBar(List<Bar> listBar) { 213 this.listBar = listBar; 214 } 215 } Foo.java 1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.serializer.SerializeConfig; 3 import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; java.util.Date; 6 import java.util.Random; Bar { SerializeConfig mapping = new SerializeConfig(); 10 private String barName; barAge; 12 private Date barDate = new Date(); 13 static { 14 mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd")); 15 } 16 { 17 Random r = new Random(); 18 barName = "sss_"+String.valueOf(r.nextFloat()); 19 barAge = r.nextInt(10); 20 } main(String[] args) { 23 Object obj = JSON.toJSON(new Bar()); 24 String x1 = JSON.toJSONString(new Bar(), true); 25 System.out.println(x1); 26 String x2 = JSON.toJSONString(new Bar(), mapping); 27 System.out.println(x2); 28 } String getBarName() { 31 return barName; 32 } setBarName(String barName) { 35 this.barName = barName; 36 } getBarAge() { 39 return barAge; 40 } setBarAge(int barAge) { 43 this.barAge = barAge; 44 } Date getBarDate() { 47 return barDate; 48 } setBarDate(Date barDate) { 51 this.barDate = barDate; 52 } 53 54 @Override 55 public String toString() { 56 return "Bar{" + 57 "barName='" + barName + '\'' + 58 ", barAge=" + barAge + 59 ", barDate=" + barDate + 60 '}'; 61 } 62 } Bar.java 1 import com.alibaba.fastjson.JSON; 2 import com.alibaba.fastjson.JSONArray; 3 import com.alibaba.fastjson.serializer.SerializeConfig; 4 import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer; java.util.*; Main { SerializeConfig mapping = new SerializeConfig(); static { 10 mapping.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss")); 11 } main(String[] args) { 14 Test.json2Bean(); 15 // Test.json2List(); 16 // Test.json2Map(); 17 // Test.array2JSON(); 18 // Test.array2JSON2(); } json2Bean(){ Bar bar1 = new Bar(); 25 String str = JSON.toJSONString(bar,true); 26 System.out.println(str); 27 Bar bar2 = new Bar(); 28 bar2 = JSON.parseObject(str,Bar.class); 29 System.out.println(bar2); 30 31 } json2List(){ List<Bar> barList = new ArrayList<Bar>(); 36 barList.add(new Bar()); 37 barList.add(new Bar()); 38 barList.add(new Bar()); 39 String json= JSON.toJSONString(barList, true); 40 System.out.println(json); List<Bar> barList1 = JSON.parseArray(json,Bar.class); 43 for (Bar bar : barList1) { 44 System.out.println(bar.toString()); 45 } 46 } json2Map(){ Map<String,Bar> map = new HashMap<String, Bar>(); 51 map.put("a",new Bar()); 52 map.put("b",new Bar()); 53 map.put("c",new Bar()); 54 String json = JSON.toJSONString(map,true); 55 System.out.println(json); Map<String,Bar> map1 = (Map<String,Bar>)JSON.parse(json); 58 for (String key : map1.keySet()) { 59 System.out.println(key+":"+map1.get(key)); 60 } 61 } array2JSON(){ 64 String[] arr_String = {"a","b","c"}; 65 String json_arr_String = JSON.toJSONString(arr_String,true); 66 System.out.println(json_arr_String); 67 JSONArray jsonArray = JSON.parseArray(json_arr_String); 68 for (Object o : jsonArray) { 69 System.out.println(o); 70 } 71 System.out.println(jsonArray); 72 } array2JSON2(){ 74 Bar[] arr_Bar = {new Bar(),new Bar(),new Bar()}; 75 String json_arr_Bar = JSON.toJSONString(arr_Bar,true); 76 System.out.println(json_arr_Bar); 77 JSONArray jsonArray = JSON.parseArray(json_arr_Bar); 78 for (Object o : jsonArray) { 79 System.out.println(o); 80 } 81 System.out.println(jsonArray); 82 } map2JSON(){ 85 Map map = new HashMap(); 86 map.put("a","aaa"); 87 map.put("b","bbb"); 88 map.put("c","ccc"); 89 String json=JSON.toJSONString(map); 90 System.out.println(json); 91 Map map1 = JSON.parseObject(json); 92 for (Object o : map.entrySet()) { 93 Map.Entry<String,String> entry = (Map.Entry<String,String>)o; 94 System.out.println(entry.getKey()+"--->"+entry.getValue()); 95 } 96 } 97 } Main

fastjson-1.1.39.jar:

fastjson-1.1.39-sources.jar:

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • JSON入门级学习小结--JSON数据结构

    JSON入门级学习小结--JSON数据结构

    2017-08-02 18:06

  • golang 提交json、获取json、解析json 热门技术

    golang 提交json、获取json、解析json 热门技术

    2017-08-02 16:00

  • 【源码分析】cJSON库学习

    【源码分析】cJSON库学习

    2017-08-01 18:00

  • C#操作JSON学习

    C#操作JSON学习

    2017-08-01 16:03

网友点评