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\":\"女\",\"age\":19}]";
		//把字符串封装成一个字符流对象
		StringReader sr = new StringReader(json);
		//创建JSON解析器
		JsonReader reader = new JsonReader(sr);
		List<Student> list = new ArrayList<Student>();
		try {
			reader.beginArray();//开始解析数组
			while(reader.hasNext()){
				Student s = new Student();
				reader.beginObject();//开始解析对象
				while(reader.hasNext()){
					String name= reader.nextName();//获取当前对象属性名称
					if("id".equals(name)){
						s.setId(reader.nextInt());
					}else if("name".equals(name)){
						s.setName(reader.nextString());
					}else if("sex".equals(name)){
						s.setSex(reader.nextString());
					}else if("age".equals(name)){
						s.setAge(reader.nextInt());
					}
				}
				reader.endObject();//结束解析对象
				list.add(s);
			}
			reader.endArray();//结束解析数组
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		//测试输出
		for (Student student : list) {
			Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
		}
	}
}


 

 

 

package com.vince.json;

public class Student {
	private int id;
	private String name;
	private String sex;
	private int age;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", sex=" + sex
				+ ", age=" + age + "]";
	}
	public Student(int id, String name, String sex, int age) {
		super();
		this.id = id;
		this.name = name;
		this.sex = sex;
		this.age = age;
	}
	public Student() {
		super();
	}
}


 

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1_json1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用JsonReader解析json数据" />

    <Button
        android:id="@+id/button2_json2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用Gson解析单个json数据" />

    <Button
        android:id="@+id/button3_json3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="使用Gson解析多个json数据" />

    <Button
        android:id="@+id/button4_json4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="把一个对象转换成json数据" />

    <Button
        android:id="@+id/button5_json5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="把一组对象转换民json数据" />

</LinearLayout>


 

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页