unity Json
来源:互联网 发布:风力发电机组数据 编辑:程序博客网 时间:2024/05/23 16:04
//本文主要介绍在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; }
}
- unity Json
- Unity json
- Unity Json
- Unity JavaScript 解析json
- Unity的json处理
- unity之Json
- Unity Json解析
- Unity读取 JSon配置文件
- Unity 解析Json字符串
- Unity与JSON
- UNITY之Json
- Unity JSON 序列化
- unity使用json
- Unity读取 JSon配置文件
- Unity -- Json文件读取
- Unity之JSon
- unity读取Json文件
- Unity 与 json
- adb devices : * daemon not running. starting it now on port 5037
- Objective-C Associated Objects 的实现原理
- IntelliJ IDEA 快捷健与eclipse对比
- 动态寻路
- NDK 编译armebai-v7a的非4字节对齐crash Fatal signal 7 (SIGSEGV) 错误解决
- unity Json
- ubuntu安装编译 webrtc
- webview中html页面点击文本框整个页面放大
- BZOJ 3172 [Tjoi2013]单词 AC自动机(fail树)
- 不再需要第三方网络诊断工具VS2015
- QDialog 的一些接口,如透明化
- Android Frame(逐帧动画)的使用
- html中组织内容的元素总结
- 每天一个linux命令(32):gzip命令