首页 > 代码库 > Gson和Fastjson
Gson和Fastjson
1。PersonBean
package com.louis.fastjsondemo;
/** * Created by Administrator on 2016/2/26. */
public class PersonBean {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public PersonBean() {
super();
// TODO Auto-generated constructor stub
}
public PersonBean(int id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "PersonBean [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}
2。Gson aty
package com.example.louisgsondemo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gson gson = new Gson();
// JavaBean数据
PersonBean personBean = new PersonBean(1, "name1", "address1");
String jsonString = gson.toJson(personBean);
System.out.println("==1.jsonString=="+jsonString);
// JavaBean数据解析
PersonBean person = gson.fromJson(jsonString, PersonBean.class);
System.out.println("==1.person=="+person);
// List<JavaBean>数据
PersonBean person1 = new PersonBean(1001, "香港", "刘德华");
PersonBean person2 = new PersonBean(1002, "韩国", "宋慧乔");
List<PersonBean> persons = new ArrayList<PersonBean>();
persons.add(person1);
persons.add(person2);
jsonString = gson.toJson(persons);
System.out.println("==2.jsonString=="+jsonString);
// List<JavaBean>数据解析
List<PersonBean> persons2 = gson.fromJson(jsonString, new TypeToken<List<PersonBean>>() {
}.getType());
System.out.println("==2.persons2=="+persons2);
// List<String>数据
List<String> list = new ArrayList<String>();
list.add("gson1");
list.add("gson2");
list.add("gson3");
jsonString = gson.toJson(list);
System.out.println("==3.jsonString=="+jsonString);
// List<String>数据解析
List<String> list2 = gson.fromJson(jsonString, new TypeToken<List<String>>() {
}.getType());
System.out.println("==3.list2=="+list2);
// List<Map<String,Object>>数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("key1", 1);
map2.put("key2", 2);
List<Map<String, Object>> listm = new ArrayList<Map<String, Object>>();
listm.add(map);
listm.add(map2);
jsonString = gson.toJson(listm);
System.out.println("==4.jsonString=="+jsonString);
List<Map<String, Object>> listm2 = gson.fromJson(jsonString, new TypeToken<List<Map<String, Object>>>() {
}.getType());
System.out.println("==4.listm2=="+listm2);
}
}
3。Fastjson aty
package com.louis.fastjsondemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// JavaBean数据
PersonBean personBean = new PersonBean(1, "name1", "address1");
String jsonString = JSON.toJSONString(personBean);
System.out.println("louis==1.jsonString==" + jsonString);
// JavaBean数据解析
PersonBean person =JSON.parseObject(jsonString, PersonBean.class);
System.out.println("louis==1.person=="+person);
// List<JavaBean>数据
PersonBean person1 = new PersonBean(1001, "香港", "刘德华");
PersonBean person2 = new PersonBean(1002, "韩国", "宋慧乔");
List<PersonBean> persons = new ArrayList<PersonBean>();
persons.add(person1);
persons.add(person2);
jsonString = JSON.toJSONString(persons);
System.out.println("louis==2.jsonString=="+jsonString);
// List<String>数据
List<String> list = new ArrayList<String>();
list.add("fasjson1");
list.add("fasjson2");
list.add("fasjson3");
jsonString =JSON.toJSONString(list);
System.out.println("louis==3.jsonString=="+jsonString);
// List<String>数据解析
List<String> list2 = JSON.parseArray(jsonString, String.class);
System.out.println("louis==3.list2=="+list2);
// List<Map<String,Object>>数据
Map<String, Object> map = new HashMap<String, Object>();
map.put("key1", "value1");
map.put("key2", "value2");
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("key1", 1);
map2.put("key2", 2);
List<Map<String, Object>> listm = new ArrayList<Map<String, Object>>();
listm.add(map);
listm.add(map2);
jsonString = JSON.toJSONString(listm);
System.out.println("louis==4.jsonString=="+jsonString);
List<Map<String, Object>> listm2 = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){});
System.out.println("louis==4.listm2=="+listm2);
}
}
from: http://www.voidcn.com/blog/RichieZhu/article/p-5038995.html
Gson和Fastjson
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。