库文件:LitJson.dll
using LitJson; //对json操作的命名空间
ToJson(只使用了json数据串,未生成json文件)
public void setSpot()
{
string dataJsonStr = "";
JsonData data = new JsonData();
JsonData data01 = new JsonData();
data01["spotName"] = YiZhi01.GetComponent<XuanDian>().Tex_Name.text;
data.Add(data01);
JsonData data02 = new JsonData();
data02["spotName"] = YiZhi02.GetComponent<XuanDian>().Tex_Name.text;
data.Add(data02);
for (int i = 1; i < DaiQiu_Fu.transform.childCount; i++)
{
JsonData datai = new JsonData();
datai["spotName"] = DaiQiu_Fu.transform.GetChild(i).GetComponent<XuanDian>().Tex_Name.text;
data.Add(datai);
}
//传入jsondata数据
dataJsonStr = LitJson.JsonMapper.ToJson(data);
Debug.Log(dataJsonStr);
Application.ExternalCall("setSpot", dataJsonStr); //传网页
}
}
ReadJson
1. 网页调用
Application.ExternalCall("getSpot"); 网页getSpot调用ReadJsonText()
html:
var str
function getSpot(){
u.getUnity().SendMessage("GUI","ReadJsonText","+str+");
}
public void ReadJsonText(string str)
{
string jsonString = str;
JsonData jd = JsonMapper.ToObject(jsonString);
for (int i = 0; i < jd.Count; i++)
{
GameObject temp = Instantiate(PrefabCube, PrefabCube.transform.position, PrefabCube.transform.rotation) as GameObject;
//名字
temp.name = jd[i]["spotName"].ToString();
}
}
2.协程
IEnumerator ReadJsonText()
{
WWW www = new WWW(url);
while (!www.isDone)
{
Debug.Log("Wait");
yield return new WaitForSeconds(0.01f);
}
if (www.error != null)
{
Debug.LogError(www.error);
}
yield return www;
Debug.Log(www.text);
string jsonString = www.text;
jsonString = System.Text.Encoding.UTF8.GetString(www.bytes, 3, www.bytes.Length - 3);
JsonData jd = JsonMapper.ToObject(jsonString);
for (int i = 0; i < jd.Count; i++)
{
GameObject temp = Instantiate(PrefabCube, PrefabCube.transform.position, PrefabCube.transform.rotation) as GameObject;
temp.name = jd[i]["spotName"].ToString();
}
www.Dispose();
}
3.JSON学习练习
using UnityEngine;
using System.Collections;
using LitJson; //对json操作的命名空间
using System.Text;
using System.IO;
using UnityEditor;
public class Data_JSON : MonoBehaviour
{
public TextAsset MyFile;
public string strPath;
public string strName;
void Start ()
{
//文件的目录
strPath = Application.dataPath + "/4.json/Date";
//文件的名称
strName = strPath + "/NewFile.json";
ReadJson();
ReadJsonFromFile();
WriteJsonToFile(strPath, strName);
Hero h = new Hero();
//h.Name = "炎魔之王丶拉格纳罗斯";
//h.ID = "1001";
string jsonH = JsonMapper.ToJson(h);
print(jsonH);
}//end_Start
void Update ()
{
}//end_Update
void ReadJson()
{
string str =
@"{
'name':'UnityClass',
'id':'SnailVR02',
'students':
[
{'stuID':'1001','stuName':'傻屌安超','Score':'100'},
{'stuID':'1002','stuName':'傻吊大个','Score':'100'}
]
}";
//解析:将json的格式化字符串转化为对象
//并将信息保存在jsondata的对象中
JsonData jd = JsonMapper.ToObject(str);
print("name" + jd["name"]);
print("id" + jd["id"]);
//学生的所有信息
JsonData jdStu = jd["students"];
print("一共多少个学生:" + jdStu.Count);
for (int i = 0; i < jdStu.Count; i++)
{
print("stuId:" + jdStu[i]["stuID"]
+ "\tstuName:" + jdStu[i]["stuName"]
+ "\tScore:" + jdStu[i]["Score"]);
}
}
void ReadJsonFromFile()
{
JsonData jd = JsonMapper.ToObject(MyFile.text);
JsonData jdHero = jd["Hero"];
print(jdHero[0]["skill"][1]["name"]);
print(jdHero[0]["skill"][1]["lv_cd"][1]["lv2"]);
JsonData jd2 = JsonMapper.ToObject(MyFile.text);
JsonData jsHero = jd["Hero"];
print(jdHero[0]["skill"][2]["level"][0]["lv1"]);
}
void WriteJsonToFile(string path, string name)
{
StringBuilder sb = new StringBuilder();
//定义一个json的写入器
JsonWriter jw = new JsonWriter(sb);
//开始写入一组数据
jw.WriteObjectStart();//----------------1.头
jw.WritePropertyName("class_name");//-------------1.1
jw.Write("SnailVR02");
jw.WritePropertyName("self_info");//------------1.2
jw.WriteArrayStart();
jw.WriteObjectStart();
#region 杀生丸
jw.WritePropertyName("name");//-----------------1.2.1
jw.Write("杀生丸");
jw.WritePropertyName("height");//-------------1.2.2
jw.Write("180");
#endregion
jw.WriteObjectEnd();
jw.WriteArrayEnd();
//结束写入数据
jw.WriteObjectEnd();//------------------1.尾
//创建目录
DirectoryInfo dir = new DirectoryInfo(path);
//判断目录是否存在
if (dir.Exists)
{
print("目录已存在");
}
else
{
//如果不存在,那么创建目录
dir.Create();
print("创建目录成功");
}
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
//定义一个写入流
StreamWriter sw;
//实例化该写入流(给该写入流赋值)
if (File.Exists(name))
{
print("文件已存在,我们添加数据");
sw = File.AppendText(name);
}
else
{
sw = File.CreateText(name);
print("创建文件成功,开始写入数据");
}
//通过流写入数据
sw.WriteLine(sb);
sw.Close();
sw.Dispose();
#if UNITY_EDITOR
AssetDatabase.Refresh();
#endif
}
}
public class Hero
{
}
网友评论