1、GSON 的介紹:
GSON 是一個JAVA 庫,可用於將JAVA 對象轉換稱JSON,它也可以被用於將一個JSON 字符串轉換爲等效的Java對象。
GSON 可以與任意Java對象,包括預先存在的對象或者你沒有源代碼結合。
背景:已經有很多開源項目,可以轉換Java對象到JSON。然而,他們中大多數要求在你的類上使用元註釋 Annotation,如果你沒有源碼,將不能給代碼加上Java這些元註解。大多數人也並不完全支持Java泛型的使用。GSON 認爲這些是非常重要的設計目標;
2、用法準備:
下載GsonAPI:google-gson-1.7.1-release.zip ; 並投入使用,怎麼用大家都明白
3、解析JSON數據的兩種方法:
(1): 通過獲取JsonReader對象解析JSON數據;
(2): 通過把JSON數據映射成一個對象,使用Gson對象的fromJson()方法獲取一個對象數組進行操作;
4、案例:
- package com.example.file;
- import java.io.StringReader;
- import java.net.Proxy.Type;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import android.util.Log;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.google.gson.stream.JsonReader;
- /**
- * GSON 解析 json 數據
- * @author Administrator
- *
- */
- public class MyGson {
- /**
- * 使用JsonReader對象解析 JSON 數據
- * @throws Exception
- */
- public void jsonReader() throws Exception{
- String json = "[{\"name\":\"aa\",\"age\":20,\"score\":90},{\"name\":\"ff\",\"age\":21,\"score\":80}]";
- // 1、創建JsonReader
- JsonReader reader = new JsonReader(new StringReader(json));
- reader.beginArray();
- while(reader.hasNext()){
- reader.beginObject();
- while(reader.hasNext()){
- String str = reader.nextName();
- if(str.equals("name")){
- String name = reader.nextString();
- Log.i("msg", "name:"+name);
- }
- if(str.equals("age")){
- int age = reader.nextInt();
- Log.i("msg", "age:"+age);
- }
- if(str.equals("score")){
- int score = reader.nextInt();
- Log.i("msg", "score:"+score);
- }
- }
- reader.endObject();
- }
- reader.endArray();
- }
- /**
- * 測試:
- * 把對象映射成JSON數據格式
- * java to json
- */
- public void tojson(){
- Student s1 = new Student("小黑",13, 90);
- Student s2 = new Student("小白",14, 70);
- Student s3 = new Student("小黃",15, 80);
- Student s4 = new Student("小綠",16, 100);
- List<Student> list = new ArrayList<Student>();
- list.add(s1);
- list.add(s2);
- list.add(s3);
- list.add(s4);
- Gson gson = new Gson();
- String s = gson.toJson(list);
- Log.i("msg", "對象映射稱json格式爲:"+s);
- }
- /**
- * 測試:
- * 通過把JSON 數據映成一個對象,使用GSON對象的fromJson()方法獲取一個對象數組進行操作;
- *
- * json to java
- *
- * 如果要處理的JSON字符串只包含一個JSON 對象,則可以直接使用fromJson獲取一個對象
- */
- public void fromjson(){
- Gson gson = new Gson();
- // 單個對象的操作
- String strjson = "{"+"\"name\":\"aa\",\"score\":90,\"age\":12"+"}";
- Student stu = gson.fromJson(strjson, Student.class); // 映射獲取數據
- Log.i("msg", "姓名:"+stu.getName()+",年齡:"+stu.getAge());
- /* 多個對象的操作 */
- String json = "[{\"name\":\"aa\",\"age\":20,\"score\":90}," +
- "{\"name\":\"ff\",\"age\":21,\"score\":80}]";
- java.lang.reflect.Type listType = new TypeToken<LinkedList<Student>>(){}.getType();
- Gson gson1 = new Gson();
- LinkedList<Student> stus = gson1.fromJson(json, listType);
- for(Iterator iterator=stus.iterator();iterator.hasNext();){
- Student st = (Student) iterator.next();
- Log.i("msg",st.getName());
- }
- }
- }
[自創]
/**
* 測試:
* 通過把JSON 數據映成一個對象,使用GSON對象的fromJson()方法獲取一個對象數組進行操作;
*
* json to java
*
* 如果要處理的JSON字符串只包含一個JSON 對象,則可以直接使用fromJson獲取一個對象
*/
public void fromjson(){
Gson gson = new Gson();
/* 多個對象的操作 */
String json = " [4, 480, 396, 25, 7, 23, 1, 17]";
java.lang.reflect.Type listType = new TypeToken<ArrayList<String>>(){}.getType();
Gson gson1 = new Gson();
ArrayList<String> stus = gson1.fromJson(json, listType);
for(Iterator iterator=stus.iterator();iterator.hasNext();){
String st = (String) iterator.next();
Log.i("Hanjhmsg",st);
}
}
m_snsFriends: {qq=[25, 23, 396, 4, 1, 7, 480, 17], weibo=[1049], mobile=[426, 23, 348, 347, 34, 427, 424, 824, 5036], system=[33, 1651, 174, 1659]}
for (String snstype : m_snsFriends.keySet()) {
List<String> friendsInType = m_snsFriends.get(snstype);
Log.e("Hanjh","friendsInType: "+friendsInType);
Iterator it = friendsInType.iterator();
String accountID;
while (it.hasNext()) {
accountID = (String) it.next();
Log.w("Hanjh",accountID);
WeModelAccount weModelAccount = WeAccountMgr.getInst().checkCreateAccount(accountID);
Player player = WePlayerMgr.createPlayer(weModelAccount);
m_friendList.add(player);
}
}