一、單個對象生成json
生成以下類,該怎麼生成呢?
{
"createDate": "2015-02-01 10:39:50",
"id": "1",
"name": "傳說之美",
"password": "123456"
}
先定義一個account類,屬性有id、name、password、createDate。
public class Account {
private String id;
private String password;
private String name;
private String createDate;
public Account() {
super();
}
public Account(String id, String password, String name, String createDate) {
super();
this.id = id;
this.password = password;
this.name = name;
this.createDate = createDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
@Override
public String toString() {
return "Account [id=" + id + ", password=" + password + ", name=" + name + ", createDate=" + createDate + "]\n\n";
}
}
定義好這個類,就可以利用Gson生成json字符串了。
// 生成account對象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Account account = new Account("1", "123456", "傳說之美", sdf.format(new Date()));
// 利用gson對象生成json字符串
Gson gson = new Gson();
String jsonString = gson.toJson(account);
Log.i("", jsonString);
二、解析json字符串為單個對象
在上面已生成了jsonString,那如何將其解析為單個對象,很簡單。
// 利用gson解析json字符串為單個對象
Account account1 = gson.fromJson(jsonString, Account.class);
Log.i("", account1.toString());
三、生成單個對象的json數組
什麼事json數組,類似下面的
[
{
"id": "2",
"createDate": "2015-02-01 11:21:27",
"password": "123456",
"name": "傳說"
},
{
"id": "2",
"createDate": "2015-02-01 11:21:27",
"password": "123456",
"name": "之美"
}
]
生成json數組代碼如下
Account account2 = new Account("2", "123456", "傳說", sdf.format(new Date()));
Account account3 = new Account("2", "123456", "之美", sdf.format(new Date()));
List<Account> accountList = new ArrayList<Account>();
accountList.add(account2);
accountList.add(account3);
JSONArray accountArray = new JSONArray();
for (int i = 0; i < accountList.size(); i++) {
String accountStr = gson.toJson(accountList.get(i));
JSONObject accountObject;
try {
accountObject = new JSONObject(accountStr);
accountArray.put(i, accountObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
Log.i("", accountArray.toString());
四、由多個單個對象的json數組解析為對個單個對象
多個單個對象組成的json數組解析如下
// 解析json數組
List<Account> accountList2 = new ArrayList<Account>();
for(int i=0;i<accountArray.length(); i++){
JSONObject jsonObject = null;
try {
jsonObject = accountArray.getJSONObject(i);
} catch (JSONException e) {
e.printStackTrace();
}
if(jsonObject != null){
Account tempAccount = gson.fromJson(jsonObject.toString(), Account.class);
accountList2.add(tempAccount);
}
}
Log.i("accountList2", accountList2.toString());
或者用更快捷的轉化方法
Account[] accountArrays = new Gson().fromJson(accountArray.toString(), Account[].class);
for(int i=0;i<accountArrays.length;i++){
Log.i("accountArrays",accountArrays[i].toString());
}
// 轉化為List
List<Account> foosList = Arrays.asList(accountArrays);
更快捷地解析成List
// 更快捷地解析成List
Type listType = new TypeToken<ArrayList<Account>>(){}.getType();
ArrayList<Account> accsList = new Gson().fromJson(accountArray.toString(), listType);
Log.i("accsList",accsList.toString());
五、生成一個對象嵌套對象的json
嵌套的json類似如下
{
"member": {
"id": "4",
"name": "我是傳說"
},
"id": "4",
"createDate": "2015-02-02 12:03:32",
"password": "888888",
"name": "傳說之美"
}
生成這個json有2種方法。
1、再添加一個member類,像添加account類一樣添加即可。
public class Member {
private String id;
private String name;
public Member() {
super();
}
public Member(String id, String name) {
super();
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Member [id=" + id + ", name=" + name + "]\n\n";
}
}
生成代碼如下
// 生成對象嵌套對象的json
Account account4 = new Account("4", "888888", "傳說之美", sdf.format(new Date()));
Member member = new Member("4", "我是傳說");
String accountStr = gson.toJson(account4);
String memberStr = gson.toJson(member);
JSONObject object = null;
try {
JSONObject memberObject = new JSONObject(memberStr);
object = new JSONObject(accountStr);
object.put("member", memberObject);
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("", object.toString());
六、解析對象嵌套對象的json
Account account5 = gson.fromJson(object.toString(), Account.class);
Log.i("解析對象嵌套對象的json", account5.toString());
// 這裡用isNull來判斷是否存在這個object,存在就拿出member的JSONObject
JSONObject memberObject = null;
if(!object.isNull("member")){
try {
memberObject = object.getJSONObject("member");
} catch (JSONException e) {
e.printStackTrace();
}
}
Member member5 = null;
if(null != memberObject){
member5 = gson.fromJson(memberObject.toString(), Member.class);
Log.i("解析對象嵌套對象的json", member5.toString());
}
7、另外一種解析對象嵌套對象的json
定義一個類
public class AccountObject {
private String id;
private String password;
private String name;
private String createDate;
private MemberObject memberObject = new MemberObject();
public class MemberObject {
private String id;
private String name;
@Override
public String toString() {
return "MemberObject [id=" + id + ", name=" + name+ "]\n\n";
}
}
@Override
public String toString() {
return "AccountObject [id=" + id + ", password=" + password + ", name=" + name + ", createDate=" + createDate + ","+ memberObject.toString() + "]\n\n";
}
}
生成json並解析
try {
JSONObject mObject = new JSONObject(memberStr);
object = new JSONObject(accountStr);
object.put("memberObject", mObject);
} catch (JSONException e) {
e.printStackTrace();
}
AccountObject accountObject = gson.fromJson(object.toString(), AccountObject.class);
Log.i("accountObject", accountObject.toString());