Gson解析Json和将对象生成Json

本文记录使用Gson将json数据解析成对象,和将对象生成json格式的数据。


效果图,转换前:

Gson解析Json和将对象生成Json_第1张图片

转换后:

Gson解析Json和将对象生成Json_第2张图片

json解析成对象--------------------

要解析的json格式数据:

private String json_Str = "[{name:'abc',age:22},"
			+ "{name:'xyz', age:18}]";

json转对象方法:

private List mPersonLis;

private void json转对象(){
		//json转对象
		Type listType = new TypeToken>() {
		}.getType();
		////fromJson方法将json转换成对象
		Gson gson = new Gson();
		mPersonLis = gson.fromJson(json_Str, listType);
		//显示解析后的数据
		mTV1.setText(mPersonLis.toString());
}

对象转成json------------------

转换方法:

private void 对象转json(){
	//对象转json
	 List personLis = new LinkedList();
	 person p1 = new person("aaa", 17);
	 personLis.add(p1);
	 person p2 = new person("bbb", 19);
	 personLis.add(p2);
	 
	 ////toJson方法将对象转换成json
	 Gson gson1 = new Gson();
	 String ss = gson1.toJson(personLis).toString();
	 //显示转换后的数据
	 mTV2.setText(ss);
}
----------完整代码-----------

对象类:person.java

public class person {
	
	private String name;
	private int age;
	
	public person(String n, int a) {
		// TODO Auto-generated constructor stub
		this.name = n;
		this.age = a;
	}
	
	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 + "]";
	}

}

java代码:MainActivity.java

public class MainActivity extends Activity {

	private String TAG = "MainActivity";
	
	//////
	@ViewInject(R.id.btn1)
	private  Button mBtn1;
	@ViewInject(R.id.btn2)
	private  Button mBtn2;
	@ViewInject(R.id.tv1)
	private TextView mTV1;
	@ViewInject(R.id.tv2)
	private TextView mTV2;
	
	private String json_Str = "[{name:'abc',age:22},"
			+ "{name:'xyz', age:18}]";
	
	private List mPersonLis;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 ViewUtils.inject(this); //注入view和事件
		 
		 init();	
	}

	
	private void init() {
		// TODO Auto-generated method stub
		mBtn1.setOnClickListener(myClickLis);
		mBtn2.setOnClickListener(myClickLis);
		mTV1.setText(json_Str);
		mTV2.setText("aaa,17; bbb,19");
	}


  private OnClickListener myClickLis = new OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn1:
			json转对象();
			break;
		case R.id.btn2:
			对象转json();
			break;
		default:
			break;
		}
	}
};

private void json转对象(){
		//json转对象
		Type listType = new TypeToken>() {
		}.getType();
		////fromJson方法将json转换成对象
		Gson gson = new Gson();
		mPersonLis = gson.fromJson(json_Str, listType);
		//显示解析后的数据
		mTV1.setText(mPersonLis.toString());
}

private void 对象转json(){
	//对象转json
	 List personLis = new LinkedList();
	 person p1 = new person("aaa", 17);
	 personLis.add(p1);
	 person p2 = new person("bbb", 19);
	 personLis.add(p2);
	 
	 ////toJson方法将对象转换成json
	 Gson gson1 = new Gson();
	 String ss = gson1.toJson(personLis).toString();
	 //显示转换后的数据
	 mTV2.setText(ss);
}
}


--Libs: gson-2.3.1.jar,  xUtils-2.6.14.jar

--布局文件activity_main.xml:


    
    
        

----------------------------





你可能感兴趣的:(Android)