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/5
本文来自:http://www.cnblogs.com/mybkn/archive/2012/05/18/2508306.html