json是好東西啊!JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式
JSON簡單易用,我要好好研究一下,和你們共享一下.
想了解更詳細的數據能夠參考一下百科:
http://baike.baidu.com/view/136475.htm
好了,咱們步入正題:unity3d使用json
我寫了4個方法:ReadJson(),ReadJsonFromTXT();WriteJsonAndPrint(),WriteJsonToFile(string,string).
想使用JSon須要dll動態連接庫
還有一些相應的命名空間using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
首先你須要2個字符串,一個爲路徑,一個文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數據
void ReadJson()
{
//注意json格式。個人只能在一行寫入啊,要不就報錯,懂的大牛,不吝賜教啊,這是爲何呢?
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,不然會報錯的,看到網上
//有的朋友用枚舉,可是我測試過,會報錯,我也不太清楚。
//你們注意一下就行了
for (int i = 0; i < jdItems.Count; i++)
{
Debug.Log("itemid=" + jdItems[i]
["itemid"]);
Debug.Log("itemname=" + jdItems[i]
["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"];
//打印一下數組
for (int i = 0; i < weapon.Count; i++)
{
Debug.Log("name="+weapon[i]
["name"]);
Debug.Log("color="+weapon[i]
["color"]);
Debug.Log("durability="+weapon[i]
["durability"]);
}
}
3. //寫json數據而且打印他
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[i]
["name"]);
Debug.Log("MM age=" + jdItems[i]
["age"]);
}
}
4. //把json數據寫到文件裏
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);
//建立文件目錄
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數據寫到txt裏
StreamWriter sw;
if (File.Exists(fileName))
{
//若是文件存在,那麼就向文件繼續附加(爲了下次寫內容不會覆蓋上次的內容)
sw = File.AppendText(fileName);
Debug.Log("appendText");
}
else
{
//若是文件不存在則建立文件
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();
}