Gson解析json数据

浏览: 23 发布日期: 2016-11-05 分类: android

使用Gson解析json数据
1.当json数据是JsonObject时对应大括号
2.当json数据是jsonArray时对应中括号
[Person [name=xiaoming, age=1, address=meiguo, colorString= null, height =0], Person [name=xiaoming1, age=2, address=meiguo1, colorString= null, height =0], Person [name=xiaoming2, age=3, address=meiguo2, colorString= null, height =0]]
[{"name":"xiaoming","age":1,"address":"meiguo","colorString":null,"height":0},{"name":"xiaoming1","age":2,"address":"meiguo1","colorString":null,"height":0},{"name":"xiaoming2","age":3,"address":"meiguo2","colorString":null,"height":0}]

/**
 * @param args
 * 实体类中增加属性,而json数据中没有该字段,不影响生成实体类,则生成的实体json数据不包含该字段部分为默认值
 * 使用Gson gson = new GsonBuilder().serializeNulls().create();在json数据的字段值值为空的时候也保留该字符串
 */


public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        String jsonString ="{\"name\":\"xiaoming\",\"age\" : 1,\"address\":\"meiguo\"}";
        
        String arrayString ="{\"list\":[{\"name\":\"xiaoming\",\"age\" : 1,\"address\":\"meiguo\"}," +
                "{\"name\":\"xiaoming1\",\"age\" : 2,\"address\":\"meiguo1\"}," +
                "{\"name\":\"xiaoming2\",\"age\" : 3,\"address\":\"meiguo2\"}]}";
        String arrayStrings ="[{\"name\":\"xiaoming\",\"age\" : 1,\"address\":\"meiguo\"}," +
                "{\"name\":\"xiaoming1\",\"age\" : 2,\"address\":\"meiguo1\"}," +
                "{\"name\":\"xiaoming2\",\"age\" : 3,\"address\":\"meiguo2\"}]";
testJsonToObject(jsonString);
testObjectToJson(arrayString);    
tokenObjectToJson(arrayStrings);
    }
    public static void testJsonToObject(String jsonString){
        Gson gson = new Gson();
        Bean2.Person fromJson = gson.fromJson(jsonString,Bean2.Person.class);
        System.out.println(fromJson);
        
        
    }
    public static void testObjectToJson(String json) {
        Gson gson = new Gson();
        Bean2 bean2 = gson.fromJson(json,Bean2.class);
        System.out.println(bean2);
        System.out.println(bean2.list);
        
    }
    //使用TypeToken解析成集合
    public static void tokenObjectToJson(String json) {
        //在值为空的时候也保留该字符串
        Gson gson = new GsonBuilder().serializeNulls().create();
        Type type = new TypeToken<List<Bean2.Person>>(){}.getType();
        Object fromJson = gson.fromJson(json,type);
        System.out.println(fromJson);
        String json2 = gson.toJson(fromJson);//生成json数据
        System.out.println(json2);

        
    }
}
 class Bean2 {
    List<Person> list;
    public class Person {
        public String name;
        public int age;
        public String address;
        public String colorString;//增加属性,json字段中没有
        public int height;//增加属性,json字段中没有
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", address="
                    + address + ", colorString= "+colorString+", height ="+height+"]";
        }
    }
    @Override
    public String toString() {
        return "Bean2 [list=" + list + ", getClass()=" + getClass()
                + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }
    
}
返回顶部