什么是JSON:
JSON即JavaScript Object Natation, 它是一種輕量級(jí)的數(shù)據(jù)交換格式, 與XML一樣, 是廣泛被采用的客戶端和服務(wù)端交互的解決方案.
JSON對(duì)象: JSON中對(duì)象(Object)以"{"開(kāi)始, 以"}"結(jié)束. 對(duì)象中的每一個(gè)item都是一個(gè)key-value對(duì), 表現(xiàn)為"key:value"的形式, key-value對(duì)之間使用逗號(hào)分隔. 如:{"name":"coolxing", "age"=24, "male":true, "address":{"street":"huiLongGuan", "city":"beijing", "country":"china"}}. JSON對(duì)象的key只能是string類型的, 而value可以是string, number, false, true, null, Object對(duì)象甚至是array數(shù)組, 也就是說(shuō)可以存在嵌套的情況.
JSON數(shù)組: JSON數(shù)組(array)以"["開(kāi)始, 以"]"結(jié)束, 數(shù)組中的每一個(gè)元素可以是string, number, false, true, null, Object對(duì)象甚至是array數(shù)組, 數(shù)組間的元素使用逗號(hào)分隔. 如["coolxing", 24, {"street":"huiLongGuan", "city":"beijing", "country":"china"}].
JSON數(shù)據(jù)的解析:解析JSON數(shù)據(jù), 首先需要明確待解析的是JSON Object還是JSON array, 然后需要確定采用哪種解析技術(shù). android平臺(tái)上一般有2種解析技術(shù)可供選擇: android內(nèi)置的org.json包和google的開(kāi)源gson庫(kù). 以下將分別采用這兩種技術(shù)解析JSON對(duì)象和JSON數(shù)組.
1. 采用android內(nèi)置的org.json包解析JSON對(duì)象. 假設(shè)待解析的JSON數(shù)據(jù)為json = "{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}", 其中\(zhòng)用于轉(zhuǎn)義表達(dá)式中的雙引號(hào). 首先定義2個(gè)JavaBean:
- public class Address {
- private String street;
- private String city;
- private String country;
-
- public Address() {
- super();
- }
-
- public Address(String street, String city, String country) {
- super();
- this.street = street;
- this.city = city;
- this.country = country;
- }
-
- public String getStreet() {
- return street;
- }
-
- public void setStreet(String street) {
- this.street = street;
- }
-
- public String getCity() {
- return city;
- }
-
- public void setCity(String city) {
- this.city = city;
- }
-
- public String getCountry() {
- return country;
- }
-
- public void setCountry(String country) {
- this.country = country;
- }
-
- @Override
- public String toString() {
- return "Address [street=" + street + ", city=" + city + ", country="
- + country + "]";
- }
-
- }
- public class Person {
- private String name;
- private int age;
- private boolean male;
- private Address address;
-
- public Person() {
- super();
- }
-
- public Person(String name, int age, boolean male, Address address) {
- super();
- this.name = name;
- this.age = age;
- this.male = male;
- this.address = address;
- }
-
- public String getName() {
- return name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
-
- public int getAge() {
- return age;
- }
-
- public void setAge(int age) {
- this.age = age;
- }
-
- public boolean isMale() {
- return male;
- }
-
- public void setMale(boolean male) {
- this.male = male;
- }
-
- public Address getAddress() {
- return address;
- }
-
- public void setAddress(Address address) {
- this.address = address;
- }
-
- @Override
- public String toString() {
- return "Person [name=" + name + ", age=" + age + ", male=" + male
- + ", address=" + address + "]";
- }
-
- }
下面是具體的解析代碼:
- JSONObject jsonObject = new JSONObject(json);
- String name = jsonObject.getString("name");
- int age = jsonObject.getInt("age");
- boolean male = jsonObject.getBoolean("male");
- JSONObject addressJSON = jsonObject.getJSONObject("address");
- String street = addressJSON.getString("street");
- String city = addressJSON.getString("city");
- String country = addressJSON.getString("country");
- Address address = new Address(street, city, country);
- Person person = new Person(name, age, male, address);
- System.out.println(person);
LogCat的輸出為Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], 說(shuō)明已經(jīng)正確解析了JSON對(duì)象.
2. 采用gson庫(kù)解析JSON對(duì)象. 假設(shè)帶解析的JSON數(shù)據(jù)是json = "{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}", 首先需要到http://code.google.com/p/google-gson/下載jar包, 并將其添加到項(xiàng)目中. 具體的解析代碼如下:
- Gson gson = new Gson();
- Person person = gson.fromJson(json, Person.class);
- System.out.println(person);
LogCat的輸出為Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], 說(shuō)明已經(jīng)正確解析了JSON對(duì)象, 是不是很簡(jiǎn)單?
3. 采用android內(nèi)置的org.json包解析JSON數(shù)組. 假設(shè)待解析的JSON數(shù)據(jù)為json = "[{\"name\":\"coolxing\", \"age\"=24, \"male\":true, \"address\":{\"street\":\"huiLongGuan\", \"city\":\"beijing\", \"country\":\"china\"}}, {\"name\":\"min\", \"age\"=20, \"male\":false, \"address\":{\"street\":\"heiShiJiao\", \"city\":\"daLian\", \"country\":\"china\"}}]", 解析代碼如下:
- List<Person> persons = new ArrayList<Person>();
- JSONArray jsonArray = new JSONArray(json);
- for(int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- String name = jsonObject.getString("name");
- int age = jsonObject.getInt("age");
- boolean male = jsonObject.getBoolean("male");
- JSONObject addressJSON = jsonObject.getJSONObject("address");
- String street = addressJSON.getString("street");
- String city = addressJSON.getString("city");
- String country = addressJSON.getString("country");
- Address address = new Address(street, city, country);
- Person person = new Person(name, age, male, address);
- persons.add(person);
- }
- System.out.println(persons);
LogCat的輸出為[Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], Person [name=min, age=20, male=false, address=Address [street=heiShiJiao, city=daLian, country=china]]], 說(shuō)明已經(jīng)正確解析了JSON數(shù)組.
4. 采用gson庫(kù)解析JSON數(shù)組. 待解析的JSON數(shù)據(jù)同上, 具體代碼為:
- Gson gson = new Gson();
- Type listType = new TypeToken<List<Person>>(){}.getType();
- List<Person> persons = gson.fromJson(json, listType);
LogCat的輸出為[Person [name=coolxing, age=24, male=true, address=Address [street=huiLongGuan, city=beijing, country=china]], Person [name=min, age=20, male=false, address=Address [street=heiShiJiao, city=daLian, country=china]]], 說(shuō)明已經(jīng)正確解析了JSON數(shù)組.
new TypeToken<List<Person>>(){}.getType(); 這句代碼表示創(chuàng)建一個(gè)TypeToken的匿名子類對(duì)象, 并調(diào)用對(duì)象的getType()方法.
org.json包和gson庫(kù)還有很多其他有用的API, 需要的時(shí)候可查看文檔. 比如有的時(shí)候需要通過(guò)java對(duì)象或java集合和數(shù)組生成json數(shù)據(jù)上傳給服務(wù)器, 當(dāng)然你可以自我構(gòu)造json字符串, 但會(huì)很麻煩. 這個(gè)時(shí)候就可以使用其中的有關(guān)API, 方便的完成這項(xiàng)工作.