Json is a data transmission tool similar to XML functions,
Resolution difference: one line of Json is read and parsed, while XML is partial first.
The Json file format is as follows:
{"City code": [{"Province": "Beijing", "city": [{"city name": "Beijing", "code": "101010100 "}, {"city name": "Chaoyang", "encoding": "101010300" },{ "city name": "Shunyi", "encoding": "101010400 "}, {"city name": "Huairou", "encoding": "101010500" },{ "city name": "Tongzhou", "encoding": "101010600 "}, {"city name": "Changping", "encoding": "101010700" },{ "city name": "Yanqing", "encoding": "101010800 "}, {"city name": "Fengtai", "encoding": "101010900" },{ "city name": "Shijingshan", "encoding": "101011000 "}, {"city name": "Daxing", "encoding": "101011100" },{ "city name": "Fangshan", "encoding": "101011200 "}, {"city name": "Miyun", "encoding": "101011300" },{ "city name": "Mentougou", "encoding": "101011400 "}, {"city name": "Pinggu", "encoding": "101011500" },{ "city name": "Badaling", "encoding": "101011600 "}, {"city name": "Buddha top", "encoding": "101011700" },{ "city name": "Tang Hekou", "encoding": "101011800 "}, {"city name": "Miyun dianzi", "encoding": "101011900" },{ "city name": "Zhai", "encoding": "101012000 "}, {"city name": "xia yunling", "encoding": "101012100" },{ "city name": "Beijing city", "encoding": "101012200 "}, {"city name": "Haidian", "code": "101010200"}] },{ "Province": "Tianjin", "city": [{"city name ": "Tianjin", "encoding": "101030100" },{ "city name": "baodi", "encoding": "101030300" },{ "city name": "dongli ", "encoding": "101030400" },{ "city name": "Xi Qing", "encoding": "101030500" },{ "city name": "bei Chen", "encoding ": "101030600" },{ "city name": "Jixian", "encoding": "101031400" },{ "city name": "hangu", "encoding ": "101030800" },{ "city name": "Jinhai", "encoding": "101030900" },{ "city name": "Jinnan", "encoding ": "101031000" },{ "city name": "Tanggu", "encoding": "101031100" },{ "city name": "Dagang", "encoding ": "101031200" },{ "city name": "Wuqing", "encoding": "101030200" },{ "city name": "Ninghe", "encoding ": "101030700"}] },{ "Province": "Shanghai", "city": [{"city name": "Shanghai", "encoding": "101020100 "}, {"city name": "Baoshan", "encoding": "101020300" },{ "city name": "Jiading", "encoding": "101020500 "}, {"city name": "Nanhui", "encoding": "101020600" },{ "city name": "Pudong", "encoding": "101021300 "}, {"city name": "Qingpu", "encoding": "101020800" },{ "city name": "Songjiang", "encoding": "101020900 "}, {"city name": "Fengxian", "encoding": "101021000" },{ "city name": "Chongming", "encoding": "101021100 "}, {"city name": "Xujiahui", "encoding": "101021200" },{ "city name": "Minhang", "encoding": "101020200 "}, {"city name": "Jinshan", "encoding": "101020700"}]}
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" }, { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" }, { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" } ], "authors": [ { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" }, { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" }, { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" } ], "musicians": [ { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" }, { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } ] }
Example of key code for parsing Json in android:
Super. onCreate (savedInstanceState); super. setContentView (R. layout. main); mButton = (Button) findViewById (R. id. mybut); String str = "{\" data \ ": [{\" id \ ": 1, \" name \ ": \" Anjoy \", \ "age \": 28 }," + "{\" id \ ": 2, \" name \ ": \" Zheng Xia \ ", \" age \": 28}], \ "company \": \ "Teng Rui Information Technology Co., Ltd. \"} "; StringBuffer buf = new StringBuffer (); try {Map <String, object> result = this. parseJson (str); // parse the json Data Text buf. append ("company name:" + result. get ("company") + "\ n"); List <Map <String, Object> all = (List <Map <String, Object>) result. get ("data"); Iterator <Map <String, Object> iter = all. iterator (); while (iter. hasNext () {Map <String, Object> map = iter. next (); buf. append ("ID No.:" + map. get ("id") + ", name:" + map. get ("name") + ", age:" + map. get ("age") + "\ n") ;}} catch (Exception e) {e. printStackTrace ();} mButton. setText (buf);} private Map <String, Object> parseJson (String data) throws Exception {Map <String, Object> allMap = new HashMap <String, Object> (); JSONObject allData = new JSONObject (data); // all content is changed to an item allMap. put ("company", allData. getString ("company"); // retrieves the item JSONArray jsonArr = allData. getJSONArray ("data"); // retrieve the array List <Map <String, Object> all = new ArrayList <Map <String, Object> (); for (int I = 0; I <jsonArr. length (); I ++) {Map <String, Object> map = new HashMap <String, Object> (); JSONObject jsonObj = jsonArr. getJSONObject (I); map. put ("id", jsonObj. getInt ("id"); map. put ("name", jsonObj. getString ("name"); map. put ("age", jsonObj. getInt ("age"); all. add (map);} allMap. put ("data", all); return allMap ;}