1. Introduction
JSON is a commonly used data format for Android and server communications, such as the following: A JSON-formatted string.
{"Address": "Nanjing", "name": "Nupt", "students": [{"Name": "STU1", "id": "10000", "Age": 20},{"name": "STU1", "id": " 10001 "," Age ": 21},{" name ":" STU1 "," id ":" 10002 "," Age ": 22}]}
the two JSON libraries commonly used in 2.android are: Gson and Jackson, just a look at how they convert an object to a JSON string, and how to convert a JSON string into a Java object.
Define the Student class:
/* * $filename: STUDENT.JAVA,V $ * $Date: 2014-5-12 $ * Copyright (C) Zhenghaibo, Inc. All rights reserved. * This software are 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.) {this.age = age;}}
Define the school class:
/* * $filename: SCHOOL.JAVA,V $ * $Date: 2014-5-12 $ * Copyright (C) Zhenghaibo, Inc. All rights reserved. * This software are 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;}}
Initializes a school Java object for use:
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 ("+i"), Student.setname ("Stu" + 1); List.add (student);} School.setstudents (list); return school;}
3. Use Gson to convert between Java objects and JSON strings
private void Testgson () {School School = Initschooldata (); Gson Gson = new Gson ();//Convert the object to a JSON string, string jsonstring = Gson.tojson (school). toString ();//Print System.out.println ("Gson = "+jsonstring");/****** Split line *****///convert string to JSON object School Resultschool = Gson.fromjson (jsonstring, school.class);// Print System.out.println (resultschool.getstudents (). Get (0). GetName ());}
It can be seen that the Gson is converted by Tojson and Fromjson two methods, which is very convenient to use.
4. Using Jackson
private void Testjackson () {School School = Initschooldata (); Objectmapper objectmapper = new Objectmapper (); String jsonstring = "";//Convert object to JSON string try {jsonstring = objectmapper.writevalueasstring (school); System.out.println ("jackson=" +jsonstring); catch (Jsonprocessingexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Parse the JSON string into a Java object try {School Resultschool = Objectmapper.readvalue (jsonstring, school.class);// Print System.out.println (resultschool.getstudents (). Get (0). GetName ());} catch (Jsonparseexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Jsonmappingexception e) {//T ODO auto-generated catch Blocke.printstacktrace ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
Using Jackson, the main use of writevalueasstring and ReadValue are the two methods. In addition, Jackson's WriteValue and ReadValue have a number of parameter types that are suitable for multi-medium applications.
Note: The above is just the basic usage of these two tool classes, learn more, see the official documentation.
Gson Document: http://code.google.com/p/google-gson/
jackson:http://jackson.codehaus.org/