fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map

前端之家收集整理的这篇文章主要介绍了fastJson在java后台转换json格式数据探究(二)--处理数组/List/Map前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接给代码

[java] view plain copy
  1. packagefastJson.test;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importcom.alibaba.fastjson.JSON;
  7. importcom.alibaba.fastjson.JSONArray;
  8. publicclassFastJsonTest1
  9. {
  10. /**
  11. *数组转json格式字符串
  12. */
  13. voidarray2Json(){
  14. String[]arr={"bill","green","maks","jim"};
  15. StringjsonText=JSON.toJSONString(arr,true);
  16. System.out.println("array2Json()方法:jsonText=="+jsonText);
  17. //输出结果:jsonText==["bill","green","maks","jim"]
  18. }
  19. *json格式字符串转数组
  20. voidjson2Array(){
  21. StringjsonText="[\"bill\",\"green\",\"maks\",\"jim\"]";
  22. JSONArrayjsonArr=JSON.parseArray(jsonText);
  23. System.out.println("json2Array()方法:jsonArr=="+jsonArr);
  24. //输出结果:jsonArr==["bill",153); font-weight:bold; background-color:inherit">voidarray2Json2(){
  25. Useruser1=newUser("P001","TOM",16);
  26. Useruser2=newUser("P002","JACKSON",0); background-color:inherit">21);
  27. Useruser3=newUser("P003","MARTIN",0); background-color:inherit">20);
  28. User[]userArr={user1,user2,user3};
  29. StringjsonText=JSON.toJSONString(userArr,153); font-weight:bold; background-color:inherit">true);
  30. System.out.println("array2Json2()方法:jsonText=="+jsonText);
  31. //输出结果:jsonText==[{"age":16,"id":"P001","name":"TOM"},{"age":21,"id":"P002","name":"JACKSON"},{"age":20,"id":"P003","name":"MARTIN"}]
  32. }
  33. /**
  34. *json格式字符串转数组
  35. */
  36. voidjson2Array2(){
  37. StringjsonText="[{\"age\":16,\"id\":\"P001\",\"name\":\"TOM\"},{\"age\":21,\"id\":\"P002\",\"name\":\"JACKSON\"},{\"age\":20,\"id\":\"P003\",\"name\":\"MARTIN\"}]";
  38. JSONArrayjsonArr=JSON.parseArray(jsonText);
  39. System.out.println("json2Array2()方法:jsonArr=="+jsonArr);
  40. //输出结果:jsonArr==[{"age":16,0); background-color:inherit">*list集合转json格式字符串
  41. voidlist2Json(){
  42. Listlist=newArrayList();
  43. newUser("L001",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> list.add(user1);
  44. Useruser2=newUser("L002",0); background-color:inherit">21);
  45. list.add(user2);
  46. newUser("L003",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> list.add(user3);
  47. StringjsonText=JSON.toJSONString(list,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> System.out.println("list2Json()方法:jsonText=="+jsonText);
  48. voidlist2Json2(){
  49. Addressaddress1=newAddress("广东省","深圳市","科苑南路","580053");
  50. Useruser1=16,address1);
  51. list.add(user1);
  52. Addressaddress2=newAddress("江西省","南昌市","阳明路","330004");
  53. 21,address2);
  54. Addressaddress3=newAddress("陕西省","西安市","长安南路","710114");
  55. Useruser3=20,address3);
  56. list.add(user3);
  57. StringjsonText=JSON.toJSONString(list,248)"> System.out.println("list2Json2()方法:jsonText=="+jsonText);
  58. //输出结果:jsonText==[{"address":{"city":"深圳市","post":"580053","province":"广东省","street":"科苑南路"},"age":16,{"address":{"city":"南昌市","post":"330004","province":"江西省","street":"阳明路"},"age":21,{"address":{"city":"西安市","post":"710114","province":"陕西省","street":"长安南路"},"age":20,"name":"MARTIN"}]
  59. *map转json格式字符串
  60. voidmap2Json(){
  61. Mapmap=newHashMap();
  62. Addressaddress1="580053");
  63. map.put("address1",address1);
  64. map.put("address2",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> Addressaddress3="710114");
  65. map.put("address3",address3);
  66. StringjsonText=JSON.toJSONString(map,248)"> System.out.println("map2Json()方法:jsonText=="+jsonText);
  67. //输出结果:jsonText=={"address1":{"city":"深圳市","address2":{"city":"南昌市","address3":{"city":"西安市","street":"长安南路"}}
  68. staticvoidmain(String[]args)
  69. {
  70. FastJsonTest1test=newFastJsonTest1();
  71. //数组转json格式字符串
  72. test.array2Json();
  73. //json格式字符串转数组
  74. test.json2Array();
  75. test.array2Json2();
  76. test.json2Array2();
  77. //list集合转json格式字符串
  78. test.list2Json();
  79. //list集合转json格式字符串
  80. test.list2Json2();
  81. //map转json格式字符串
  82. test.map2Json();
  83. }

控制台输出结果:

[html] copy
    array2Json()方法jsonText==["bill","jim"]
  1. json2Array()方法jsonArr==["bill","jim"]
  2. array2Json2()方法jsonText==[{
  3. "age":16,
  4. "id":"P001",
  5. "name":"TOM"
  6. },{
  7. "age":21,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "id":"P002",248)"> "name":"JACKSON"
  8. "age":20,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "id":"P003",248)"> "name":"MARTIN"
  9. }]
  10. json2Array2()方法jsonArr==[{"age":16,"name":"MARTIN"}]
  11. list2Json()方法jsonText==[
  12. "age":16,248)"> "id":"L001",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"TOM"
  13. },108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "id":"L002",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "age":20,248)"> "id":"L003",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"MARTIN"
  14. ]
  15. list2Json2()方法jsonText==[
  16. "address":{
  17. "city":"深圳市",248)"> "post":"580053",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "province":"广东省",248)"> "street":"科苑南路"
  18. "id":"L001",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "address":{
  19. "city":"南昌市",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "post":"330004",248)"> "province":"江西省",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "street":"阳明路"
  20. "age":21,248)"> "id":"L002",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "name":"JACKSON"
  21. "city":"西安市",248)"> "post":"710114",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "province":"陕西省",248)"> "street":"长安南路"
  22. "id":"L003",248)"> ]
  23. map2Json()方法jsonText=={"address1":{
  24. "city":"深圳市",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "post":"580053",248)"> "province":"广东省",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "street":"科苑南路"
  25. "city":"南昌市",248)"> "post":"330004",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "province":"江西省",248)"> "street":"阳明路"
  26. "city":"西安市",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "post":"710114",248)"> "province":"陕西省",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "street":"长安南路"
  27. }}


附:javabean类 User.java

copy
    importjava.io.Serializable;
  1. classUserimplementsSerializable{
  2. privatefinallongserialVersionUID=1L;
  3. privateStringid;
  4. privateStringname;
  5. intage;
  6. privateAddressaddress;
  7. publicUser(){
  8. super();
  9. publicUser(Stringid,Stringname,intage){
  10. this.id=id;
  11. this.name=name;
  12. this.age=age;
  13. intage,Addressaddress)
  14. this.address=address;
  15. intgetAge(){
  16. returnage;
  17. voidsetAge(this.age=age;
  18. publicStringgetId(){
  19. returnid;
  20. voidsetId(Stringid){
  21. this.id=id;
  22. publicStringgetName(){
  23. returnname;
  24. voidsetName(Stringname){
  25. publicAddressgetAddress()
  26. returnaddress;
  27. voidsetAddress(Addressaddress)
  28. }

Address.java

copy
    classAddressimplementsSerializable
  1. privateStringprovince;
  2. privateStringcity;
  3. privateStringstreet;
  4. privateStringpost;
  5. publicAddress()
  6. super();
  7. publicAddress(Stringprovince,Stringcity,Stringstreet,Stringpost)
  8. this.province=province;
  9. this.city=city;
  10. this.street=street;
  11. this.post=post;
  12. publicStringgetCity()
  13. returncity;
  14. voidsetCity(Stringcity)
  15. publicStringgetPost()
  16. returnpost;
  17. voidsetPost(Stringpost)
  18. publicStringgetProvince()
  19. returnprovince;
  20. voidsetProvince(Stringprovince)
  21. this.province=province;
  22. publicStringgetStreet()
  23. returnstreet;
  24. voidsetStreet(Stringstreet)
  25. this.street=street;
  26. }

猜你在找的Json相关文章