首页 > 代码库 > Java之Json
Java之Json
使用gson,我们可以非常轻松的实现数据对象和json对象的相互转化,其中我们最常用的就是两个方法,一个是fromJSON(),将json对象转换成我们需要的数据对象,另一个是toJSON(),这个就是将我们的数据对象转换成json对象。下面我们也通过一个综合的例子来看看gson的使用方法:
gson(http://code.google.com/p/google-gson/)
public class JsonService { public Person getPerson() { Person person = new Person(1, "xiaoluo", "广州"); return person; } public List<Person> getPersons() { List<Person> persons = new ArrayList<Person>(); Person person = new Person(1, "xiaoluo", "广州"); Person person2 = new Person(2, "android", "上海"); persons.add(person); persons.add(person2); return persons; } public List<String> getString() { List<String> list = new ArrayList<String>(); list.add("广州"); list.add("上海"); list.add("北京"); return list; } public List<Map<String, String>> getMapList() { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); Map<String, String> map1 = new HashMap<String, String>(); map1.put("id", "001"); map1.put("name", "xiaoluo"); map1.put("age", "20"); Map<String, String> map2 = new HashMap<String, String>(); map2.put("id", "002"); map2.put("name", "android"); map2.put("age", "33"); list.add(map1); list.add(map2); return list; }}
public class Person { private int id; private String name; private String address; public Person() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Person(int id, String name, String address) { super(); this.id = id; this.name = name; this.address = address; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", address=" + address + "]"; }}
public class test { public void name() { } public static void main(String[] args) { //Person person = new Person(1, "xiaoluo", "广州"); // 将Person对象转换成一个json类型的字符串对象 //String personString = JsonTools.getJsonString("person", person); // System.out.println(personString.toString()); Gson gson = new Gson(); JsonService jsonService = new JsonService(); Person person = jsonService.getPerson(); System.out.println("person: " + gson.toJson(person)); // 对于Object类型,使用 fromJson(String, Class)方法来将Json对象转换成Java对象 Person person2 = gson.fromJson(gson.toJson(person), Person.class); System.out.println(person2); System.out.println("------------------------------------------------"); List<Person> persons = jsonService.getPersons(); System.out.println("persons: " + gson.toJson(persons)); /* * 对于泛型对象,使用fromJson(String, Type)方法来将Json对象转换成对应的泛型对象 * new TypeToken<>(){}.getType()方法 */ List<Person> persons2 = gson.fromJson(gson.toJson(persons), new TypeToken<List<Person>>(){}.getType()); System.out.println(persons2); System.out.println("------------------------------------------------"); List<String> list = jsonService.getString(); System.out.println("String---->" + gson.toJson(list)); List<String> list2 = gson.fromJson(gson.toJson(list), new TypeToken<List<String>>(){}.getType()); System.out.println("list2---->" + list2); System.out.println("------------------------------------------------"); List<Map<String, String>> listMap = jsonService.getMapList(); System.out.println("Map---->" + gson.toJson(listMap)); List<Map<String, String>> listMap2 = gson.fromJson(gson.toJson(listMap), new TypeToken<List<Map<String, String>>>(){}.getType()); System.out.println("listMap2---->" + listMap2); System.out.println("------------------------------------------------"); }}
Java之Json
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。