android:json解析的兩個工具:Gson和Jackson的使用小例子

來源:互聯網
上載者:User

標籤: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}]}

2.android中常用的兩個json庫為:Gson和Jackson,一下就看一下它們是如何將對象轉化為json字串,以及如何將json字串轉化為java對象。

定義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:    [email protected] *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;}}

定義School類:

/* * $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:    [email protected] *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;}}

初始化一個School的java對象,待使用:

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());}

可見,Gson是通過toJson和fromJson兩個方法進行轉換的,使用起來十分方便。

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 blocke.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 blocke.printStackTrace();} catch (JsonMappingException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

使用Jackson,主要是使用writeValueAsString和readValue這兩個方法。此外,Jackson的writeValue和readValue有很多種參數類型,適用於多中場合。

注意:以上只是這兩個工具類的基本用法,更多瞭解,參見官方文檔。

Gson文檔:http://code.google.com/p/google-gson/

Jackson:http://jackson.codehaus.org/



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.