//本文主要介绍在unity中如何处理json数据
//LitJson是处理JSON的第三方库,需要下载导入
//可直接右键Assets,Import New Asset导入LitJson
using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
using System.Text;
public class JsonDemo : MonoBehaviour
{
public TextAsset json;
public string filePath;
public string fileName;
void Awake()
{
filePath = Application.dataPath + "/Resources/TextFile";
fileName = filePath + "/File.json";
//StringToJson();
//ObjectToJson();
//WriteJsonAndLog();
//writeJsonToFile(filePath,fileName);
//ReadJsonFromJson();
}
public void StringToJson()
{
//@ 换行
string str = @"{'name':'hello','age':16,'items':
[{'height':100,'width':'20'},{'height':'30','width':'40'}]}";
JsonData jd = JsonMapper.ToObject(str);
Debug.Log("name:" + (string)jd["name"]);
Debug.Log("age:" + (int)jd["age"]);
JsonData jdItems = jd["items"];
for (int i = 0; i < jdItems.Count; i++)
{
Debug.Log("height" + jdItems[i]["height"]);
Debug.Log("width" + jdItems[i]["width"]);
}
}
public void ObjectToJson()
{
Person person = new Person();
person.age = 10;
person.name = "caicai";
string strjson = JsonMapper.ToJson(person);
Debug.Log(strjson);
JsonData jd = JsonMapper.ToObject(strjson);
Debug.Log("name" + jd["name"]);
Debug.Log("age" + jd["age"]);
}
public void WriteJsonAndLog()
{
StringBuilder strB = new StringBuilder();
JsonWriter jsWrite = new JsonWriter(strB);
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("caicai");
jsWrite.WritePropertyName("list");
jsWrite.WriteArrayStart();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("huahua");
jsWrite.WriteObjectEnd();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("huanghuang");
jsWrite.WriteObjectEnd();
jsWrite.WriteArrayEnd();
jsWrite.WriteObjectEnd();
Debug.Log(strB);
JsonData jd = JsonMapper.ToObject(strB.ToString());
Debug.Log("name"+jd["name"]);
JsonData items = jd["list"];
for (int i = 0; i < items.Count; i++)
{
Debug.Log(items[i]["name"]);
}
}
public void writeJsonToFile(string path, string fileName)
{
StringBuilder strB = new StringBuilder();
JsonWriter jsWrite = new JsonWriter(strB);
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("caicai");
jsWrite.WritePropertyName("list");
jsWrite.WriteArrayStart();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("huahua");
jsWrite.WriteObjectEnd();
jsWrite.WriteObjectStart();
jsWrite.WritePropertyName("name");
jsWrite.Write("huanghuang");
jsWrite.WriteObjectEnd();
jsWrite.WriteArrayEnd();
jsWrite.WriteObjectEnd();
DirectoryInfo dir = new DirectoryInfo(path);
if (dir.Exists)
{
Debug.Log("dir is exit");
}
else
{
Directory.CreateDirectory(path);
Debug.Log("createdir");
}
StreamWriter sw;
if (File.Exists(fileName))
{
//sw = File.AppendText(fileName);
sw = File.CreateText(fileName);
Debug.Log("appendText");
}
else
{
sw = File.CreateText(fileName);
Debug.Log("createText");
}
sw.WriteLine(strB);
sw.Close();
//AssetDatabase.Refresh();
}
public void ReadJsonFromJson()
{
json = Resources.Load("TextFile/File") as TextAsset;
JsonData jd = JsonMapper.ToObject(json.text);
Debug.Log(jd["name"]);
}
}
class Person
{
public string name { get; set; }
public int age { get; set; }
}