新建Person实体类。
package com.steven.gasontest.bean;
public class Person {
private String name;
private int age;
public Person() {
super();
// TODO Auto-generated constructor stub
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
通过Gson的fromJson方法和toJson方法来实现解析Json和组装Json的操作。
package com.steven.gasontest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.steven.gasontest.bean.Person;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
/**
* 将给定的 JSON 字符串转换成指定的类型对象
*/
String json = "{\"name\":\"Tom\",\"age\":90}";
Person person = gson.fromJson(json, Person.class);
Log.e("GSON", person.toString());
/**
* 将给定的目标对象转换成 JSON 格式的字符串
*/
String json_Person = gson.toJson(person);
Log.e("GSON", json_Person);
/**
* 将给定的集合对象转换成 JSON 格式的字符串
*/
ArrayList persons = new ArrayList();
Collections.addAll(persons, new Person("tom", 10), new Person("jon", 20));
String json_list = gson.toJson(persons);
Log.e("GSON", json_list);
/**
* 将给定的 JSON 格式字符串转换为带泛型的集合对象
*/
List retList = gson.fromJson(json_list, new TypeToken>() {}.getType());
for (Person p : retList) {
Log.e("GSON", p.toString());
}
}
}
切换到project结构目录下,下载Gson的jar包并导入libs文件夹中,并右键通过Add as library添加到当前moudle中,此时build.gradle会生成一句编译代码。
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"
defaultConfig {
applicationId "com.example.administrator.testapplication"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
compile files('libs/gson-2.3.1.jar')
}
新建Person实体类。
package com.example.administrator.testapplication.bean;
/**
* Created by Administrator on 2015/7/12 0012.
*/
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
通过Gson的fromJson方法和toJson方法来实现解析Json和组装Json的操作。
package com.example.administrator.testapplication;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.example.administrator.testapplication.bean.Person;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
/**
* 将给定的 JSON 字符串转换成指定的类型对象
*/
String json = "{\"name\":\"Tom\",\"age\":90}";
Person person = gson.fromJson(json, Person.class);
Log.e("GSON", person.toString());
/**
* 将给定的目标对象转换成 JSON 格式的字符串
*/
String json_Person = gson.toJson(person);
Log.e("GSON", json_Person);
/**
* 将给定的集合对象转换成 JSON 格式的字符串
*/
ArrayList persons = new ArrayList();
Collections.addAll(persons, new Person("tom", 10), new Person("jon", 20));
String json_list = gson.toJson(persons);
Log.e("GSON", json_list);
/**
* 将给定的 JSON 格式字符串转换为带泛型的集合对象
*/
List retList = gson.fromJson(json_list, new TypeToken>() {}.getType());
for (Person p : retList) {
Log.e("GSON", p.toString());
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
http://download.csdn.net/detail/chaozhouzhang/8892589
http://download.csdn.net/detail/chaozhouzhang/8892607