Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Json解析

Json解析

編輯:關於Android編程

對象的解析

例如

{
"id": 1,
"name": "網游之靈魂守衛",
"imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
"author": "雷童的仰望",
"tag": "自傳",
"desc": "寫給曾經的一個女孩子,希望她能好好照顧自己"
}


分析

兩個花括號 { } 包裝起來的數據解析出來是一個對象,bean的成員屬性要與json的一一對應


代碼

public class Novel {
    private Integer id;
    private String name;
    private String imgPath;
    private String author;
    private String tag;
    private String desc;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getImgPath() {
        return imgPath;
    }
    public void setImgPath(String imgPath) {
        this.imgPath = imgPath;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public String getTag() {
        return tag;
    }
    public void setTag(String tag) {
        this.tag = tag;
    }
    public String getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }

}
// json為要解析的數據,格式如上邊的而例子
public Novel parseJson(String json) {
        Gson gson = new Gson();
        Novel novel = gson.fromJson(json, City.class);
        return novel ;
}

鏈表的解析(不包含鏈表名)

例子

[
    {
        "id": 1,
        "name": "網游之靈魂守衛",
        "imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
        "author": "雷童的仰望",
        "tag": "自傳",
        "desc": "寫給曾經的一個女孩子,希望她能好好照顧自己"
    },
    {
        "id": 26,
        "name": "三國演義",
        "imgPath": "http://192.168.31.43:8080/read/source_images/sanguo.jpg",
        "author": "羅貫中",
        "tag": "三國",
        "desc": "中國第一部長篇章回體歷史演義小說,全名為《三國志通俗演義》"
    },
    {
        "id": 24,
        "name": "三個火槍手",
        "imgPath": "http://192.168.31.43:8080/read/source_images/san.jpg",
        "author": "大仲馬",
        "tag": "驚心動魄",
        "desc": "米萊狄,小說中最大的女反派,外表美麗動人,風情萬種,花言巧語,為人心狠手辣,毒如蛇蠍。執行紅衣主教大部分陰謀。"
    },
    {
        "id": 16,
        "name": "簡愛",
        "imgPath": "http://192.168.31.43:8080/read/source_images/jian.jpg",
        "author": "夏洛蒂·勃朗特",
        "tag": "自由平等",
        "desc": "有一定影響力的書,百余年來,簡·愛的形象是不朽的,而這部小說一直受到世界各國人民的歡迎,專家們也多給出了好評。"
    },
    {
        "id": 3,
        "name": "茶花女",
        "imgPath": "http://192.168.31.43:8080/read/source_images/cha.jpg",
        "author": "亞歷山大·小仲馬",
        "tag": "愛情",
        "desc": "瑪格麗特原來是個貧苦的鄉下姑娘,來到巴黎後,開始了賣笑生涯。由於生得花容月貌,巴黎的貴族公子爭相追逐,成了紅極一時的“社交明星”。她隨身的裝扮總是少不了一束茶花,人稱“茶花女”。"
    },
    {
        "id": 12,
        "name": "虹",
        "imgPath": "http://192.168.31.43:8080/read/source_images/hong.jpg",
        "author": "勞倫斯",
        "tag": "詩意",
        "desc": "豐富而深刻的思想內容,史詩般的畫面,以及對兩性關系的先鋒性探索,《虹》已經被公認作現代主義小說的經典作品。"
    },
    {
        "id": 14,
        "name": "呼嘯山莊",
        "imgPath": "http://192.168.31.43:8080/read/source_images/huxiao.jpg",
        "author": "艾米莉·勃朗特",
        "tag": "爭取",
        "desc": "英格蘭北部,有一座幾乎與世隔絕的“呼嘯山莊”。主人恩肖收養了一個棄兒.取名希斯克利夫.讓他與自己的兒女辛德雷和凱瑟琳一起生活。希斯克利夫與凱瑟琳朝夕相處並萌發了愛情,但辛德雷十分憎惡他。"
    }
]

分析

兩個中括號 [ ] 包裝起來的數據解析出來是一個鏈表


代碼

// Novel類與上邊的一樣
public List parseJson(String json) {
   Gson gson = new Gson();
   // 解析出來的是一個鏈表對象
   List datas = gson.fromJson(json, new TypeToken>() {}.getType());
   return datas;
}

鏈表的解析(包含鏈表名)

例子

"zhudas": [
        {
            "id": 1,
            "name": "網游之靈魂守衛",
            "imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
            "author": "雷童的仰望",
            "tag": "自傳",
            "desc": "寫給曾經的一個女孩子,希望她能好好照顧自己"
        },
        {
            "id": 17,
            "name": "基督山伯爵",
            "imgPath": "http://192.168.31.43:8080/read/source_images/jidush.jpg",
            "author": "大仲馬",
            "tag": "復仇",
            "desc": "唐泰斯越獄後找到了寶藏,成為巨富,從此化名基督山伯爵(水手森巴),經過精心策劃,報答了恩人,懲罰了仇人。充滿傳奇色彩,奇特新穎,引人入勝。"
        }
    ]

分析

兩個中括號 [ ] 包裝起來的數據解析出來是一個鏈表,中括號前邊有一個名稱,為鏈表名稱,所以就可以看成一個類裡邊包含著一個鏈表List,鏈表的名稱為 zhudas (名稱必須一樣,否則會出錯)


代碼

public class City {
    // 鏈表名稱要與json一一對應
    private List zhudas;

    public List getZhudas() {
        return zhudas;
    }

    public void setZhudas(List zhudas) {
        this.zhudas = zhudas;
    }
}
public City parseJson(String json) {
        Gson gson = new Gson();
        City city = gson.fromJson(json, City.class);
        return city;
    }

整體的解析

例子

{
    "zhudas": [
        {
            "id": 1,
            "name": "網游之靈魂守衛",
            "imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
            "author": "雷童的仰望",
            "tag": "自傳",
            "desc": "寫給曾經的一個女孩子,希望她能好好照顧自己"
        },
        {
            "id": 17,
            "name": "基督山伯爵",
            "imgPath": "http://192.168.31.43:8080/read/source_images/jidush.jpg",
            "author": "大仲馬",
            "tag": "復仇",
            "desc": "唐泰斯越獄後找到了寶藏,成為巨富,從此化名基督山伯爵(水手森巴),經過精心策劃,報答了恩人,懲罰了仇人。充滿傳奇色彩,奇特新穎,引人入勝。"
        }
    ],
    "gudians": [
        {
            "id": 4,
            "name": "楚辭",
            "imgPath": "http://192.168.31.43:8080/read/source_images/chuci.jpg",
            "author": "屈原",
            "tag": "詩歌",
            "desc": "最早的浪漫主義詩歌總集及浪漫主義文學源頭"
        },
        {
            "id": 5,
            "name": "春秋左傳",
            "imgPath": "http://192.168.31.43:8080/read/source_images/chun.jpg",
            "author": "左丘明",
            "tag": "歷史",
            "desc": "主要記載了東周前期二百五十四年間各國政治、經濟、軍事、外交和文化方面的重要事件和重要人物"
        },
        {
            "id": 11,
            "name": "漢書",
            "imgPath": "http://192.168.31.43:8080/read/source_images/hanshu.jpg",
            "author": "班固",
            "tag": "歷史",
            "desc": "紀12篇,主要記載西漢帝王的事跡;表8篇,主要記載漢代的人物事跡等;志10篇,專述典章制度、天文、地理以及各種社會現象;傳70篇,主要記載各類人物的生平以及少數民族的歷史等。"
        },
        {
            "id": 15,
            "name": "三十六計",
            "imgPath": "http://192.168.31.43:8080/read/source_images/ji36.jpg",
            "author": "軍法家",
            "tag": "策論",
            "desc": "中國古代三十六個兵法策略,語源於南北朝,成書於明清。它是根據中國古代漢族軍事思想和豐富的斗爭經驗總結而成的兵書"
        },
        {
            "id": 27,
            "name": "三國志",
            "imgPath": "http://192.168.31.43:8080/read/source_images/sanguozhi.jpg",
            "author": "陳壽",
            "tag": "三國",
            "desc": "記載中國三國時代的斷代史,同時也是二十四史中評價最高的“前四史”之一。"
        }
    ],
    "zhanzhengs": [
        {
            "id": 7,
            "name": "封神演義",
            "imgPath": "http://192.168.31.43:8080/read/source_images/feng.jpg",
            "author": "許仲琳",
            "tag": "神話",
            "desc": "姜子牙輔佐周室(周文王、周武王)討伐商纣的歷史為背景,描寫了闡教、截教諸仙斗智斗勇、破陣斬將封神的故事"
        },
        {
            "id": 17,
            "name": "基督山伯爵",
            "imgPath": "http://192.168.31.43:8080/read/source_images/jidush.jpg",
            "author": "大仲馬",
            "tag": "復仇",
            "desc": "唐泰斯越獄後找到了寶藏,成為巨富,從此化名基督山伯爵(水手森巴),經過精心策劃,報答了恩人,懲罰了仇人。充滿傳奇色彩,奇特新穎,引人入勝。"
        },
        {
            "id": 1,
            "name": "網游之靈魂守衛",
            "imgPath": "http://192.168.31.43:8080/read/source_images/lt.jpg",
            "author": "雷童的仰望",
            "tag": "自傳",
            "desc": "寫給曾經的一個女孩子,希望她能好好照顧自己"
        },
        {
            "id": 40,
            "name": "西線無戰事",
            "imgPath": "http://192.168.31.43:8080/read/source_images/xixian.jpg",
            "author": "雷馬克",
            "tag": "戰爭",
            "desc": "那些陳詞濫調的口號即被炸飛了。青年人的理想主義抵抗不住連天的炮火和壕溝裡野蠻的搏殺。他們的幻想破滅了。他們的惟一動力就是活下去,他們的精神支柱就是在前線的共同命運中形成的友誼"
        },
        {
            "id": 45,
            "name": "這裡的黎明靜悄悄",
            "imgPath": "http://192.168.31.43:8080/read/source_images/zheli.jpg",
            "author": "瓦西裡耶夫",
            "tag": "戰爭",
            "desc": "通過一個淒美的故事,向人們展示戰爭對人,尤其是對婦女的摧殘,對美好生活和人類文明造成的災難,表達了強烈的反法西斯和反侵略的主題思想和深刻的人性關懷主題"
        }
    ],
    "lunbos": [
        {
            "id": 1,
            "imgPath": "http://192.168.31.43:8080/read/source_images/lunbo1.png"
        },
        {
            "id": 1,
            "imgPath": "http://192.168.31.43:8080/read/source_images/lunbo2.png"
        },
        {
            "id": 1,
            "imgPath": "http://192.168.31.43:8080/read/source_images/lunbo3.png"
        }
    ]
}

分析

json裡最外層是一個對象 { },裡邊是一個個鏈表 [ ] 與相應的名稱,鏈表裡是一個個的對象 { }


代碼

// Lunbo、PriceNovel這些類沒有貼出來,根據Novel舉一反三
public class City {
    private List lunbos;
    private List zhudas;
    private List gudians;
    private List zhanzhengs;
    private List zhekous;
    private List frees;

    public List getLunbos() {
        return lunbos;
    }

    public void setLunbos(List lunbos) {
        this.lunbos = lunbos;
    }

    public List getZhudas() {
        return zhudas;
    }

    public void setZhudas(List zhudas) {
        this.zhudas = zhudas;
    }

    public List getGudians() {
        return gudians;
    }

    public void setGudians(List gudians) {
        this.gudians = gudians;
    }

    public List getZhanzhengs() {
        return zhanzhengs;
    }

    public void setZhanzhengs(List zhanzhengs) {
        this.zhanzhengs = zhanzhengs;
    }

    public List getZhekous() {
        return zhekous;
    }

    public void setZhekous(List zhekous) {
        this.zhekous = zhekous;
    }

    public List getFrees() {
        return frees;
    }

    public void setFrees(List frees) {
        this.frees = frees;
    }
}
 public City parseJson(String json) {
        Gson gson = new Gson();
        City city = gson.fromJson(json, City.class);
        return city;
    }

注意

請添加Gson解析包

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved