本文记录使用Gson将json数据解析成对象,和将对象生成json格式的数据。
效果图,转换前:
转换后:
json解析成对象--------------------
要解析的json格式数据:
private String json_Str = "[{name:'abc',age:22},"
+ "{name:'xyz', age:18}]";
json转对象方法:
private List<person> mPersonLis;
private void json转对象(){
//json转对象
Type listType = new TypeToken<LinkedList<person>>() {
}.getType();
fromJson方法将json转换成对象
Gson gson = new Gson();
mPersonLis = gson.fromJson(json_Str, listType);
//显示解析后的数据
mTV1.setText(mPersonLis.toString());
}
对象转成json------------------
转换方法:
private void 对象转json(){
//对象转json
List<person> personLis = new LinkedList<person>();
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<person> 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<LinkedList<person>>() {
}.getType();
fromJson方法将json转换成对象
Gson gson = new Gson();
mPersonLis = gson.fromJson(json_Str, listType);
//显示解析后的数据
mTV1.setText(mPersonLis.toString());
}
private void 对象转json(){
//对象转json
List<person> personLis = new LinkedList<person>();
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);
}
}
--布局文件activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
>
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="json转对象"
/>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="对象:" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="对象转json"
android:layout_marginTop="10dp"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="json:" />
</LinearLayout>
</RelativeLayout>
----------------------------