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

地址来源:

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"
        }    ]


}

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

[Unity&JSON]LitJSON 读取数组文件,多个数据,玩家得分表数据_第1张图片[Unity&JSON]LitJSON 读取数组文件,多个数据,玩家得分表数据_第2张图片

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



你可能感兴趣的:(Unity,Unity&JSON)