Generates a JSON-formatted string.
Need related packages
1 PackageCn.itcast.controller;2 3 Import Staticorg.junit.assert.*;4 5 Importjava.util.ArrayList;6 Importjava.util.List;7 8 ImportNet.sf.json.JSONArray;9 ImportNet.sf.json.JSONObject;Ten ImportNet.sf.json.JsonConfig; One A Importorg.junit.Test; - - Importcn.itcast.domain.Province; the - Public classJsonlibtest { - - @Test + Public voidtest1 () { -Province p =NewProvince ("Shandong province", "250000"); +Jsonobject jsonobj =Jsonobject.fromobject (p); A System.out.println (jsonobj.tostring ()); at } - @Test - Public voidtest2 () { -list<province> PS =NewArraylist<province>(); -Ps.add (NewProvince ("Anhui province", "001")); -Ps.add (NewProvince ("Zhejiang province", "002")); inPs.add (NewProvince ("Jiangsu province", "003")); -Jsonarray Jsonarr =Jsonarray.fromobject (PS); to System.out.println (jsonarr.tostring ()); + } - @Test the Public voidtest3 () { *list<province> PS =NewArraylist<province>(); $Ps.add (NewProvince ("Anhui province", "001"));Panax NotoginsengPs.add (NewProvince ("Zhejiang province", "002")); -Ps.add (NewProvince ("Jiangsu province", "003")); the +Jsonconfig cfg =NewJsonconfig ();//JSON string Filtering ACfg.setexcludes (Newstring[]{"ZipCode"});//list of fields not included the +Jsonarray Jsonarr =Jsonarray.fromobject (ps,cfg); - System.out.println (jsonarr.tostring ()); $ } $}
In the JSP page
1 2 var data=this. responsetext; 3 4 var provincejson=eval ("(" +data+ ")");