日期:2014-05-16  浏览次数:20336 次

Android--用JSON解析数据

gson-1.7.1.jar,Gson在Android3.0以上才能直接使用,在3.0以下想使用可以从外部导入jar包

下载链接:http://download.csdn.net/detail/zlqqhs/5075995

 

新建工程,新建一个libs文件夹,将gson-1.7.1.jar复制到libs文件夹中

右键gson-1.7.1.jar,选择Build Path,选择Add...那一项

 

package com.vince.json;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

public class JSONActivity extends Activity {
	private Button btnJson1,btnJson2,btnJson3,btnJson4,btnJson5;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnJson1 = (Button) findViewById(R.id.button1_json1);
        btnJson2 = (Button) findViewById(R.id.button2_json2);
        btnJson3 = (Button) findViewById(R.id.button3_json3);
        btnJson4 = (Button) findViewById(R.id.button4_json4);
        btnJson5 = (Button) findViewById(R.id.button5_json5);
        
        btnJson1.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				json1();
			}
		});
        btnJson2.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				json2();
			}
		});
        btnJson3.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				json3();
			}
		});
        btnJson4.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				json4();
			}
		});
        btnJson5.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				json5();
			}
		});
    }
    
    //使用GSON把一组对象转换成json数据
    protected void json5() {
    	Student s1 = new Student(1,"jack","男",20);
    	Student s2 = new Student(2,"lucy","女",21);
    	List<Student> list = new ArrayList<Student>();
    	list.add(s1);
    	list.add(s2);
    	
    	Gson gson = new Gson();
    	Type type = new TypeToken<List<Student>>(){}.getType();
    	String json = gson.toJson(list, type);
    	Toast.makeText(this, json, Toast.LENGTH_SHORT).show();
	}

	//使用GSON把一个对象转换成json数据
    protected void json4() {
		Student s = new Student(1,"jack","男",20);
		Gson gson = new Gson();
		String json = gson.toJson(s);
		Toast.makeText(this, json, Toast.LENGTH_SHORT).show();
	}

	class MyTypeToken extends TypeToken<List<Student>>{
    	
    }
    
    //使用GSON解析一组json数据
    protected void json3() {
		String json = "[{\"id\":1,\"name\":\"tom\",\"sex\":\"男\",\"age\":18},{\"id\":2,\"name\":\"lily\",\"sex\":\"女\",\"age\":19}]";
		Gson gson = new Gson();
		Type type = new TypeToken<List<Student>>(){}.getType();
		
//		Type type = new MyTypeToken().getType();
		List<Student> list = gson.fromJson(json, type);
		
		//测试
		for (Student student : list) {
			Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
		}
	}
	//使用GSON解析一个json数据
    protected void json2() {
		String json = "{\"id\":1,\"name\":\"tom\",\"sex\":\"男\",\"age\":18}";
		Gson gson = new Gson();
		Student s = gson.fromJson(json, Student.class);
		Toast.makeText(this, s.toString(), Toast.LENGTH_SHORT).show();
	}
	//使用JsonReader解析json数据
	protected void json1() {
		String json = "[{\"id\":1,\"name\":\"tom\",\"sex\":\"男\",\"age\":18},{\"id\":2,\"name\":\"lily\",\"sex\":\&q