[腳本]Unity3d之json解析研究 json是好東西啊!JSON(JavaScript Object Notation) 是一種輕量級(jí)的數(shù)據(jù)交換格式 JSON簡單易用,我要好好研究一下,和大家共享一下. 想了解更詳細(xì)的數(shù)據(jù)可以參考一下百科:http://baike.baidu.com/view/136475.htm 好了,我們步入正題:unity3d使用json 我寫了4個(gè)方法:ReadJson(),ReadJsonFromTXT();WriteJsonAndPrint(),WriteJsonToFile(string,string). 想使用JSon需要dll動(dòng)態(tài)鏈接庫 還有一些相應(yīng)的命名空間using UnityEngine; using System.Collections; using LitJson; using System.IO; #if UNITY_EDITOR using UnityEditor; #endif 首先你需要2個(gè)字符串,一個(gè)為路徑,一個(gè)文txt文件。 我的寫法如下: public TextAsset txt; public string filePath; public string fileName; // Use this for initialization void Start () { filePath = Application.dataPath + "/TextFile"; fileName = filePath + "/File.txt"; } 1. //讀json數(shù)據(jù) void ReadJson() { //注意json格式。我的只能在一行寫入啊,要不就報(bào)錯(cuò),懂的大牛,不吝賜教啊,這是為什么呢? string str = "{'name':'taotao','id':10,'items':[{'itemid':1001,'itemname':'dtao'},{'itemid':1002,'itemname':'test_2'}]}"; //這里是json解析了 JsonData jd = JsonMapper.ToObject(str); Debug.Log("name=" + jd["name"]); Debug.Log("id=" + jd["id"]); JsonData jdItems = jd["items"]; //注意這里不能用枚舉foreach,否則會(huì)報(bào)錯(cuò)的,看到網(wǎng)上 //有的朋友用枚舉,但是我測試過,會(huì)報(bào)錯(cuò),我也不太清楚。 //大家注意一下就好了 for (int i = 0; i < jdItems.Count; i++) { Debug.Log("itemid=" + jdItems["itemid"]); Debug.Log("itemname=" + jdItems["itemname"]); } Debug.Log("items is or not array,it's " + jdItems.IsArray); } 2. //從TXT文本里都json void ReadJsonFromTXT() { //解析json JsonData jd = JsonMapper.ToObject(txt.text); Debug.Log("hp:" + jd["hp"]); Debug.Log("mp:" + jd["mp"]); JsonData weapon = jd["weapon"]; //打印一下數(shù)組 for (int i = 0; i < weapon.Count; i++) { Debug.Log("name="+weapon["name"]); Debug.Log("color="+weapon["color"]); Debug.Log("durability="+weapon["durability"]); } } 3. //寫json數(shù)據(jù)并且打印他 void WriteJsonAndPrint() { System.Text.StringBuilder strB = new System.Text.StringBuilder(); JsonWriter jsWrite = new JsonWriter(strB); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("Name"); jsWrite.Write("taotao"); jsWrite.WritePropertyName("Age"); jsWrite.Write(25); jsWrite.WritePropertyName("MM"); jsWrite.WriteArrayStart(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaomei"); jsWrite.WritePropertyName("age"); jsWrite.Write("17"); jsWrite.WriteObjectEnd(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaoli"); jsWrite.WritePropertyName("age"); jsWrite.Write("18"); jsWrite.WriteObjectEnd(); jsWrite.WriteArrayEnd(); jsWrite.WriteObjectEnd(); Debug.Log(strB); JsonData jd = JsonMapper.ToObject(strB.ToString()); Debug.Log("name=" + jd["Name"]); Debug.Log("age=" + jd["Age"]); JsonData jdItems = jd["MM"]; for (int i = 0; i < jdItems.Count; i++) { Debug.Log("MM name=" + jdItems["name"]); Debug.Log("MM age=" + jdItems["age"]); } } 4. //把json數(shù)據(jù)寫到文件里 void WriteJsonToFile(string path,string fileName) { System.Text.StringBuilder strB = new System.Text.StringBuilder(); JsonWriter jsWrite = new JsonWriter(strB); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("Name"); jsWrite.Write("taotao"); jsWrite.WritePropertyName("Age"); jsWrite.Write(25); jsWrite.WritePropertyName("MM"); jsWrite.WriteArrayStart(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaomei"); jsWrite.WritePropertyName("age"); jsWrite.Write("17"); jsWrite.WriteObjectEnd(); jsWrite.WriteObjectStart(); jsWrite.WritePropertyName("name"); jsWrite.Write("xiaoli"); jsWrite.WritePropertyName("age"); jsWrite.Write("18"); jsWrite.WriteObjectEnd(); jsWrite.WriteArrayEnd(); jsWrite.WriteObjectEnd(); Debug.Log(strB); //創(chuàng)建文件目錄 DirectoryInfo dir = new DirectoryInfo(path); if (dir.Exists) { Debug.Log("This file is already exists"); } else { Directory.CreateDirectory(path); Debug.Log("CreateFile"); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif } //把json數(shù)據(jù)寫到txt里 StreamWriter sw; if (File.Exists(fileName)) { //如果文件存在,那么就向文件繼續(xù)附加(為了下次寫內(nèi)容不會(huì)覆蓋上次的內(nèi)容) sw = File.AppendText(fileName); Debug.Log("appendText"); } else { //如果文件不存在則創(chuàng)建文件 sw = File.CreateText(fileName); Debug.Log("createText"); } sw.WriteLine(strB); sw.Close(); #if UNITY_EDITOR AssetDatabase.Refresh(); #endif }. 為了大家可以更形象理解一下。我用GUI了 void OnGUI() { GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height)); GUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); GUILayout.BeginVertical(); GUILayout.FlexibleSpace(); if (GUILayout.Button("ReadJson")) { ReadJson(); } if (GUILayout.Button("ReadJsonFromTXT")) { ReadJsonFromTXT(); } if (GUILayout.Button("WriteJsonAndPrint")) { WriteJsonAndPrint(); } if (GUILayout.Button("WriteJsonToFile")) { WriteJsonToFile(filePath,fileName); } GUILayout.FlexibleSpace(); GUILayout.EndVertical(); GUILayout.FlexibleSpace(); GUILayout.EndHorizontal(); GUILayout.EndArea(); } 首先我的工程如下: 接著我們把TestJson腳本賦給攝像機(jī) 我們點(diǎn)擊run一下 你便會(huì)在游戲面板里看到有4個(gè)按鈕了 我們點(diǎn)擊ReadJson按鈕一下 在相印的控制臺(tái)里打印如紅色框內(nèi) 我們clear一下控制臺(tái),clear玩了如下所示,紅色區(qū)域里什么也沒有了 我們點(diǎn)擊一下ReadJsonFromTXT按鈕,點(diǎn)完后發(fā)現(xiàn)打印的結(jié)果和txt里的內(nèi)容一模一樣,呵呵成功了 我們再clear一下控制臺(tái),之后點(diǎn)擊一下WriteJsonAndPrint按鈕 打印結(jié)果如下: 我們接著clear一下控制臺(tái),點(diǎn)擊一下WriteJsonToFile按鈕 我們發(fā)現(xiàn)在工程面板里多出來了一個(gè)文件夾和文本文件,而且控制臺(tái)打印的結(jié)果和文本里的內(nèi)容一樣喲!o(∩_∩)o 好了,這節(jié)講完了。不知道大家清楚沒,歡迎留言。一起學(xué)習(xí),一起進(jìn)步。 請(qǐng)保證工程里面已經(jīng)加入了LitJson.dll 服務(wù)器上JSON的內(nèi)容
LoadControl_c代碼:
|
|