Libgdx 之JSON檔案解析

來源:互聯網
上載者:User

標籤:

上一節我們學習了XML檔案的解析,接下來我們再瞭解JSON檔案的解析。在Libgdx中也提供了Json類來協助我們解析檔案,最常用的方法是fromJson(java.lang.Class type, FileHandle file) 通過此方法我們必須先定義一個內部類來抽象的表示Json檔案。

{    "name": "David",    "experience": 2534,    "strength": 6,    "dexterity": 8,    "intelligence": 6,    "items" : [        { name: "iron-sword", number: 1 },        { name: "wooden-shield", number: 1 },        { name: "leather-armor", number: 1 },        { name: "leather-boots", number: 1 },        { name: "healing-potion", number: 3 },        { name: "poison-herbs", number: 2 }    ]}

上面的Json檔案我們可以看出,Json檔案中包含對象name, experience, strength, dexterity, dexterity, intelligence, items, 共7個屬性,因此我們需要定義一個Charactor.class類來容納這7個屬性。 同時我們可以知道items包含2個屬性: name, number,因此我們需要定義一個類Item.class來容納這7個屬性。
下面是具體的代碼實現:

public class JsonTest extends ApplicationAdapter {    @Override    public void create() {        Json json = new Json();        json.setElementType(Character.class, "items", Item.class);  // 指定Character中的item資料類型        Character character = json.fromJson(Character.class, Gdx.files.internal("character.json"));    // 從Json檔案中建立一個Charactor對象        System.out.println(character);//        System.out.println("dexterity: " + character.dexterity);  // 訪問單個屬性,需要建立對應的class        System.out.println();        System.out.println("=====================");        System.out.println("Serializing character");        System.out.println("=====================");        System.out.println(json.prettyPrint(json.toJson(character)));    }    public static class Item {        private String name;        private int number;        @Override        public String toString() {            return name + "(" + number + ")";        }    }     public static class Character {            private String name = "";            private int experience = 0;            private  int strength = 1;            private  int dexterity = 1;            private  int intelligence = 1;            public Array<Item> items = new Array<Item>();            @Override            public String toString() {                String string = new String();                string += "Name: " + name + "\n";                string += "Experience: " + experience + "\n";                string += "Strength: " + strength + "\n";                string += "Dexterity: " + dexterity + "\n";                string += "Intelligence: " + intelligence + "\n";                string += "Items: ";                for (Item item : items) {                    string += item.toString() + " ";                }                return string;            }        }}

Libgdx 之JSON檔案解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.