android中有提供json的解析,但其JsonReader的SDK是從3.0之後才能使用的
其解析方法可參考http://developer.android.com/reference/android/util/JsonReader.html
對於低版本的我們可以使用一些外加包來方便實現解析
使用Gson包
com.google.gson.stream.JsonReader
:http://code.google.com/p/google-gson/downloads/list
文檔Docs:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
例如要解析的資料為
String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
其解析方法可以如下:
try{ //如果需要解析JSON資料,首要要產生一個JsonReader對象 JsonReader reader = new JsonReader(new StringReader(jsonData)); reader.beginArray(); while(reader.hasNext()){ reader.beginObject(); while(reader.hasNext()){ String tagName = reader.nextName(); if(tagName.equals("name")){ System.out.println("name--->" + reader.nextString()); } else if(tagName.equals("age")){ System.out.println("age--->" + reader.nextInt()); } } reader.endObject(); } reader.endArray(); } catch(Exception e){ e.printStackTrace(); }
解析一個json對象到一個自訂對象,例如:
要解析String jsonData = "{\"name\":\"Michael\",\"age\":20}";
可以定義model類user
public class User { private String name ; private int age ; 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; }}
解析過程:
Gson gson = new Gson(); User user = gson.fromJson(jsonData, User.class); System.out.println("name--->" + user.getName()); System.out.println("age---->" + user.getAge());
補充:若要解析的json資料如下:
{AList: [{ a1:1, a2:2, a3:3},{ a1:11, a2:22, a3:33}],BList: [{ b1:1, b2:2, b3:3},{ b1:11, b2:22, b3:33}],C: 0}
可自訂對象如下:
public class Model { private List<A> AList; private List<B> BList; private int C; public class A { private int a1; private int a2; private int a3; ......... } public class B { private int b1; private int b2; private int b3; ......... } ........}//省略了get set
解析一組json對象到自訂對象
例如要解析的資料為:
String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
解析到user,其定義如上,
解析過程為
public void parseUserFromJson(String jsonData){ Type listType = new TypeToken<LinkedList<User>>(){}.getType(); Gson gson = new Gson(); LinkedList<User> users = gson.fromJson(jsonData, listType); //用迭代器遍曆users for (Iterator iterator = users.iterator(); iterator.hasNext();) { User user = (User) iterator.next(); System.out.println("name--->" + user.getName()); System.out.println("age---->" + user.getAge()); }
and check it :http://stackoverflow.com/questions/2779251/convert-json-to-hashmap-using-gson-in-java
對象-->JSON
假如我們有一個類Person:
public class Person {
public long id;
public String name;
public boolean isMale;
public String avatar;
}
通過一個Gson執行個體:
Gson gson = new Gson();
我們可以直接將一個Person執行個體轉換成Json字串:
Person person = new Person();
person.id= 111;
person.name= "張三";
person.isMale= true;
person.avatar= "http://aaaaaaaaa";
String jsonStr = gson.toJson(person);
Log.v("=============", jsonStr);
即使是List和Map也可以(以List為例):
ArrayList<Person> list = new ArrayList<Person>();
Person person = new Person();
person.id= 111;
person.name= "張三";
person.isMale= true;
person.avatar= "http://aaaaaaaaa";
list.add(person);
person = new Person();
person.id= 222;
person.name= "李四";
person.isMale= false;
person.avatar= "http://bbbbbbbbb";
list.add(person);
String jsonStr = gson.toJson(list);
Log.v("=============", list);
可參考:http://iandroiddev.com/post/2011-10-08/5647743