[Article 3rd] converts an array to JSON data through Json-Lib, json-libjson

Source: Internet
Author: User

[Article 3rd] converts an array to JSON data through Json-Lib, json-libjson

Package ivyy.taobao.com. domain. jsonlib; import ivyy.taobao.com. entity. address; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import net. sf. json. JSONArray; import net. sf. json. JSONObject;/*** @ DEMO: json * @ Java: ArrayToJson. java * @ Date: 5:17:19 * @ Author: liangjilong * @ Email: jilongliang@sina.com * @ Weibo: http://weibo.com/jilongliang * @ Version: 1.0 * @ De Convert: converts an array to JSON data using Json-Lib */public class ArrayToJson {/*** @ param args */public static void main (String [] args) throws Exception {// getArrayToJson (); getArrayToJsonByMap ();} /*** convert the array to Json data through ByJSONLib * @ param url * @ throws Exception */private static void getArrayToJson () throws Exception {Address address = new Address (); address. setProvince ("Guangdong Province"); address. setCity ("yunfu City"); address. setDistrict ("Cloud City Dongsheng fabric "); address. setWeibo ("http://weibo.com/resourceljl"); JSONArray jsonArr = JSONArray. fromObject (address); // converts an object to a Json Object List <Address> lists = JSONArray. toList (jsonArr, Address. class); // converts an object Json array to a Json array for (Address obj: lists) {System. out. println (obj. getProvince (); System. out. println (obj. getCity (); System. out. println (obj. getDistrict (); System. out. println (obj. getWeibo ();}/*** use Map Convert the array to JSON data through Json-Lib * @ param url * @ throws Exception */private static void getArrayToJsonByMap () throws Exception {Address address = new Address (); address. setProvince ("Guangdong Province"); address. setCity ("yunfu City"); address. setDistrict ("Dongcheng District Dongsheng fabric"); address. setWeibo ("http://weibo.com/resourceljl"); JSONArray jsonArr = JSONArray. fromObject (address); // converts an object to a Json object // You can also perform Map <String, Class <Address> map = new HashMap <Str Ing, Class <Address> (); map. put ("address", Address. class); List <Address> lists = JSONArray. toList (jsonArr, Address. class, map); // converts an object Json array to a Json array System. out. println ("----------------------- method 1 -----------------------"); // ------------------------- method 1 --------------------- for (Address obj: lists) {System. out. println (obj. getProvince (); // obtain the provincial data System. out. println (obj. getCity (); // obtain the city data System. ou T. println (obj. getDistrict (); // obtain the sub-district data System. out. println (obj. getWeibo (); // get Weibo data} System. out. println ("----------------------- method 2 -----------------------"); // ------------------------- method 2 ----------------------- for (Iterator iterator = lists. iterator (); iterator. hasNext ();) {Address obj = (Address) iterator. next (); System. out. println (obj. getProvince (); System. out. println (obj. getCity (); System. out. Println (obj. getDistrict (); System. out. println (obj. getWeibo ();} System. out. println ("----------------------- method 3 -----------------------"); // ------------------------- method 3 --------------------- for (int I = 0; I <lists. size (); I ++) {JSONObject obj = JSONObject. fromObject (lists. get (I); System. out. println (obj. get ("province"); System. out. println (obj. get ("city"); System. out. println (obj. get ("district" ); System. out. println (obj. get ("weibo");} System. out. println ("----------------------- Method 4 -----------------------"); Iterator <Address> iator = lists. iterator (); do {Address obj = istrap. next (); System. out. println (obj. getProvince (); System. out. println (obj. getCity (); System. out. println (obj. getDistrict (); System. out. println (obj. getWeibo ();} while (iworkflow. hasNext (); System. out. println ("--------------------- -- Method 5 ----------------------- "); Iterator <Address> it = lists. iterator (); // obtain the value when the value must exist. If the value is not set, an error occurs while (! It. hasNext () {Address obj = iworkflow. next (); if (obj! = Null) System. out. println (obj. getProvince (); System. out. println (obj. getCity (); System. out. println (obj. getDistrict (); System. out. println (obj. getWeibo ());}}}
Package ivyy.taobao.com. entity; import java. io. serializable; import com. google. gson. annotations. expose;/*** Gson gson = new GsonBuilder (). excludeFieldsWithoutExposeAnnotation (). create (); * create a Gson object. attributes without @ Expose annotation will not be serialized * serialize serialization default true * deserialize deserialization default true * @ author liangjilong * @ Email: jilongliang@sina.com */public class Address implements Serializable {@ Expose (serialize = false) // serialization/Address class has serialized private int id; @ Expose (deserialize = false) // deserialize private String country; // country private String province; // province private String city; // city private String street; // street private String district; // region private String cityCode; // zip code private String streetNumber; // Street number private String weibo; // weibo public Address () {} public Address (int id, string country) {this. id = id; this. country = country;} public String getCityCode () {return cityCode;} public void setCityCode (String cityCode) {this. cityCode = cityCode;} public String getStreetNumber () {return streetNumber;} public void setStreetNumber (String streetNumber) {this. streetNumber = streetNumber;} public String getStreet () {return street;} public void setStreet (String street) {this. street = street;} public String getDistrict () {return district;} public void setDistrict (String district) {this. district = district;} public String getCity () {return city;} public void setCity (String city) {this. city = city;} public String getProvince () {return province;} public void setProvince (String province) {this. province = province;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getCountry () {return country;} public void setCountry (String country) {this. country = country;} public String getWeibo () {return weibo;} public void setWeibo (String weibo) {this. weibo = weibo ;}}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.