标签:android style blog class code java
1.简介
json是android与服务器通信过程中常用的数据格式,例如,如下是一个json格式的字符串:
{"address":"Nanjing","name":"NUPT","students":[{"name":"stu1","id":"10000","age":20},{"name":"stu1","id":"10001","age":21},{"name":"stu1","id":"10002","age":22}]}
定义Student类:
/* * $filename: Student.java,v $ * $Date: 2014-5-12 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package com.example.testjson; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-5-12 Nanjing,njupt,China */ public class Student { private String name; private String id; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
/* * $filename: School.java,v $ * $Date: 2014-5-12 $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */ package com.example.testjson; import java.util.List; /* *@author: ZhengHaibo *web: http://blog.csdn.net/nuptboyzhb *mail: zhb931706659@126.com *2014-5-12 Nanjing,njupt,China */ public class School { private String name; private String address; List<Student> students; 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 List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } }
private School initSchoolData(){ School school = new School(); school.setAddress("Nanjing"); school.setName("NUPT"); List<Student> list = new ArrayList<Student>(); for(int i= 0;i<3;i++){ Student student = new Student(); student.setAge(20+i); student.setId("1000"+i); student.setName("stu"+1); list.add(student); } school.setStudents(list); return school; }
3.使用Gson,进行java对象与json字符串之间的相互转换
private void testGson(){ School school = initSchoolData(); Gson gson = new Gson(); //将对象转化为json字符串 String jsonString = gson.toJson(school).toString(); //打印 System.out.println("GSON="+jsonString); /******分割线*****/ //将字符串转化为JSON对象 School resultSchool = gson.fromJson(jsonString, School.class); //打印 System.out.println(resultSchool.getStudents().get(0).getName()); }
4.使用Jackson
private void testJackson(){ School school = initSchoolData(); ObjectMapper objectMapper = new ObjectMapper(); String jsonString = ""; //将对象转化为Json字符串 try { jsonString = objectMapper.writeValueAsString(school); System.out.println("Jackson="+jsonString); } catch (JsonProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } //将json字符串解析成java对象 try { School resultSchool = objectMapper.readValue(jsonString, School.class); //打印 System.out.println(resultSchool.getStudents().get(0).getName()); } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JsonMappingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
注意:以上只是这两个工具类的基本用法,更多了解,参见官方文档。
Gson文档:http://code.google.com/p/google-gson/
Jackson:http://jackson.codehaus.org/
android:json解析的两个工具:Gson和Jackson的使用小例子,布布扣,bubuko.com
android:json解析的两个工具:Gson和Jackson的使用小例子
标签:android style blog class code java
原文地址:http://blog.csdn.net/nupt123456789/article/details/25654669