unity中从txt文件中读取解析json数据&&unity中json数据写入txt文件
2015-05-11 11:27
856 查看
using UnityEngine;
using System.Collections;
using LitJson;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class NewBehaviourScript : MonoBehaviour {
public TextAsset txt;
public string filePath;
public string fileName;
// Use this for initialization
void Start () {
filePath = Application.dataPath + "/TextFile";
fileName = filePath + "/File.txt";
}
// Update is called once per frame
void Update () {
}
void ReadJson()
{
//string str = "{'name':'taotao','id':10,'items':[{'itemid':1001,'itemname':'dtao'},{'itemid':1002,'itemname':'test_2'}]}";
//string str = "{'name':'taotao','id':'10','items':[{'itemid':'1001','itemname':'dtao'},{'itemid':'1000','itemname:test_2'}]}";
string str=@"
{
""album"" : {
""name"" : ""The Dark Side of the Moon"",
""artist"" : ""Pink Floyd"",
""year"" : 1973,
""tracks"" : [
""Speak To Me"",
""Breathe"",
""On The Run""
]
}
}
";
//这里是json解析了
LitJson.JsonData jd = LitJson.JsonMapper.ToObject(str);
for(int ii=0;ii<jd["album"]["tracks"].Count;ii++)
{
Debug.Log(jd["album"]["tracks"][ii]);
}
}
void ReadJsonFromTXT()
{
//解析json
JsonData jd = JsonMapper.ToObject(txt.text);
JsonData jditem = jd["results"];
for (int i = 0; i < jditem.Count; i++)
{
Debug.Log(jditem[i]["id"]);
//Debug.Log(i);
//Debug.Log(jd["results"]);
//Debug.Log("id="+jditem["id"]);
//Debug.Log("color="+weapon["color"]);
//Debug.Log("durability="+weapon["durability"]);
}
}
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"]);
}
}
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
}
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();
}
}
using System.Collections;
using LitJson;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class NewBehaviourScript : MonoBehaviour {
public TextAsset txt;
public string filePath;
public string fileName;
// Use this for initialization
void Start () {
filePath = Application.dataPath + "/TextFile";
fileName = filePath + "/File.txt";
}
// Update is called once per frame
void Update () {
}
void ReadJson()
{
//string str = "{'name':'taotao','id':10,'items':[{'itemid':1001,'itemname':'dtao'},{'itemid':1002,'itemname':'test_2'}]}";
//string str = "{'name':'taotao','id':'10','items':[{'itemid':'1001','itemname':'dtao'},{'itemid':'1000','itemname:test_2'}]}";
string str=@"
{
""album"" : {
""name"" : ""The Dark Side of the Moon"",
""artist"" : ""Pink Floyd"",
""year"" : 1973,
""tracks"" : [
""Speak To Me"",
""Breathe"",
""On The Run""
]
}
}
";
//这里是json解析了
LitJson.JsonData jd = LitJson.JsonMapper.ToObject(str);
for(int ii=0;ii<jd["album"]["tracks"].Count;ii++)
{
Debug.Log(jd["album"]["tracks"][ii]);
}
}
void ReadJsonFromTXT()
{
//解析json
JsonData jd = JsonMapper.ToObject(txt.text);
JsonData jditem = jd["results"];
for (int i = 0; i < jditem.Count; i++)
{
Debug.Log(jditem[i]["id"]);
//Debug.Log(i);
//Debug.Log(jd["results"]);
//Debug.Log("id="+jditem["id"]);
//Debug.Log("color="+weapon["color"]);
//Debug.Log("durability="+weapon["durability"]);
}
}
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"]);
}
}
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
}
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();
}
}
相关文章推荐
- silverlight动态读取txt文件/解析json数据/调用wcf示例
- nodejs 读取JSON文件写入txt中 fs.readFile&fs.writeFile
- Unity中从Txt文件读取Json数据
- Unity的Json解析<一>--读取Json文件
- [Unity&JSON]LitJSON 读取数组文件,多个数据,玩家得分表数据
- [C++]读写txt文件和从txt文件中读取数据写入数组中
- Unity3d-XML文件数据解析&JSON数据解析
- 【python图像处理】txt文件数据的读取与写入
- Unity的Json解析--读取Json文件
- js读取本地txt文件中的json数据
- Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器
- java读取数据写入txt文件并将读取txt文件写入另外一个表
- Newtonsoft.Json读取txt文件中json数据并存到SQL service 数据库!
- IIS7.5支持解析读取.json文件数据 -- 问题
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- c语言创建写入和读取TXT文件数据
- Python 获取接口数据,解析JSON,写入文件
- 【万里征程——Windows App开发】文件&数据——写入与读取
- java读取Excel数据,然后写入到txt文件,并批量保存到oracle数据库中
- 解析驾考json数据,将其写入word文件中