package com.gaoo.httpclientdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author gaao
*/
public class ParseJsonActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parse_json);
gsonFieldExample();
}
private void gsonFieldExample() {
Calendar dob = Calendar.getInstance();
dob.set(1990, 10, 11);
People people = new People("John", "350 Banana St.", dob.getTime());
people.setSecret("This is a secret!");
Gson gson = new Gson();
String json = gson.toJson(people);
System.out.println("json = " + json);
}
/**
* 示例如何 json 串转化成 Java对象
*/
private void jsonToStudent() {
String json = "{\"name\":\"Duke\",\"address\":\"Menlo Park\",\"dateOfBirth\":\"Feb 1, 2000 12:00:00 AM\"}";
Gson gson = new Gson();
Student student = gson.fromJson(json, Student.class);
System.out.println("student.getName() = " + student.getName());
System.out.println("student.getAddress() = " + student.getAddress());
System.out.println("student.getDateOfBirth() = " + student.getDateOfBirth());
System.out.println("json 串转化成 Java对象 ");
}
/**
* 示例如何将一个 Student 对象转换成 json 串,实际操作中我们也可以将任意的 Java 类转换为 json 串
*/
private void studentToJson() {
Calendar calendar = Calendar.getInstance();
calendar.set(2000, 10, 10, 10, 10, 10);
Student student = new Student("深冬", "Menlo Park", calendar.getTime());
Gson gson = new Gson();
String json = gson.toJson(student);
System.out.println("json = " + json);
}
/**
* 示例如何将java.util.Map转化成 json 串,然后再将 json 串转换为java.util.Map。
*/
private void mapToJson() {
Map colours = new HashMap();
colours.put("BLACK", "#000000");
colours.put("RED", "#FF0000");
colours.put("GREEN", "#008000");
colours.put("BLUE", "#0000FF");
colours.put("YELLOW", "#FFFF00");
colours.put("WHITE", "#FFFFFF");
Gson gson = new Gson();
String json = gson.toJson(colours);
System.out.println("json = " + json);
System.out.println(" ------分割线1 ----------------------------- ");
Type type = new TypeToken
下面是用到的 JavaBean对象
package com.gaoo.httpclientdemo;
import java.util.Date;
public class Student {
private String name;
private String address;
private Date dateOfBirth;
public Student() {
}
public Student(String name, String address, Date dateOfBirth) {
this.name = name;
this.address = address;
this.dateOfBirth = dateOfBirth;
}
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 Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
}
package com.gaoo.httpclientdemo;
import java.util.Date;
/**
* Created by Administrator on 2016/7/7.
*/
public class People {
private String name;
private String address;
private Date dateOfBirth;
private Integer age;
private transient String secret;
public People(String name, String address, Date dateOfBirth) {
this.name = name;
this.address = address;
this.dateOfBirth = dateOfBirth;
}
public String getSecret() {
return secret;
}
public void setSecret(String secret) {
this.secret = secret;
}
}