您的位置:首页 > 移动开发 > Unity3D

[Unity&JSON]LitJSON 读取数组文件,多个数据,玩家得分表数据

2017-05-17 12:00 1011 查看
地址来源:

How to Read JSON Data in Unity (LitJSON)

-------------------------------------------------------------------ReadJson

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using System.IO;

using LitJson;//*******************************************************

public class ReadJson : MonoBehaviour {

    private string jsonstring;

    private JsonData itemData;

    // Use this for initialization

    void Start () {

        string filename = Application.dataPath + "/Resources/" + "Items.json";

        //Debug.Log(Application.dataPath + "/Resources/"+"Items.json");

        jsonstring = File.ReadAllText(filename);

       // Debug.Log(jsonstring);

        Debug.Log(jsonstring);

        itemData = JsonMapper.ToObject(jsonstring);

        Debug.Log(itemData["Weapons"][1]["name"]);

        Debug.Log(GetItem("Light Rifle","Weapons")["power"] );

    }

    JsonData GetItem(string name,string type)

    {

        for (int i = 0; i < itemData[type].Count; i++)

        {

            if(itemData[type][i]["name"].ToString() == name || itemData[type][i]["slug"].ToString() == name)

            {

                return itemData[type][i];

            }

        }

        return null;

    }

}

-------------------------------------------------------------------Items.json

{

    "Weapons":

 [

        

{

            "id": 0,

            "name": "Light Rilfe","type":["long-range","2-handed"]

,            "mag-size": 6,"power":32,"slug":"light_rilfe"

        }

,

        {

            "id": 1,

            "name": "Rocket Pistol","type":["handgun"]

,            "mag-size": 2,"power":50,"explosive_rounds":true,"slug":"rocket_pistol"

        }

    ]

,

    "Hats":

 [

        

{

            "id": 0,

            "name": "Large Fedora","special":true,"slug":"large_fedora"

        }

,

{

            "id": 1,

            "name": "Stetson","special":true,"slug":"black_stetson"

        }    ]

}

-------------------------------------------------------------------





-------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: