//写入json文档
注意事项:
1、在Asset下要有一个StreamingAssets文件夹
2、在文件夹内,有一个已创建好的json空文档
3、引入命名空间 using Litjson; using System; using System.IO;
4、创建英雄和技能类
Demo所需要的命名空间:
[Serializable]
class Hero
{
public string HeroName;
public string Hp;
public string Attack;
public List
}
[Serializable]
class Skill
{
public string keyCode;
public string Name;
public string CD;
public string MP;
}
void CreatJson ()
{
JsonData nuoke = new JsonData ();
nuoke ["HeroName"] = "诺克";
nuoke ["HP"] = ;
nuoke ["Attack"] = ;
nuoke ["Skills"] = new JsonData ();
//一技能:大杀四方
JsonData skill1 = new JsonData ();
skill1 \["keyCode"\] = "Q";
skill1 \["Name"\] = "大杀四方";
skill1 \["CD"\] = "9/8/7/6/5";
skill1 \["MP"\] = "";
//二技能:致残打击
JsonData skill2 = new JsonData ();
skill2 \["keyCode"\] = "W";
skill2 \["Name"\] = "致残打击";
skill2 \["CD"\] = "9/8/7/6/5";
skill2 \["MP"\] = "";
//三技能:无情铁手
JsonData skill3 = new JsonData ();
skill2 \["keyCode"\] = "E";
skill2 \["Name"\] = "无情铁手";
skill2 \["CD"\] = "24/21/18/15/12";
skill2 \["MP"\] = "";
//大招:诺克萨斯断头台
JsonData skill4 = new JsonData ();
skill2 \["keyCode"\] = "R";
skill2 \["Name"\] = "诺克萨斯断头台";
skill2 \["CD"\] = "120/100/80";
skill2 \["MP"\] = "100/100/0";
//将生成的技能对象放到诺克的技能中
nuoke \["Skills"\].Add (skill1);
nuoke \["Skills"\].Add (skill2);
nuoke \["Skills"\].Add (skill3);
nuoke \["Skills"\].Add (skill4);
//将生成的对象转化成json文档
string json = JsonMapper.ToJson (nuoke);
//将json写入到文件
string path = Application.streamingAssetsPath + "/Nuoke.json";
StreamWriter sw = new StreamWriter (path);
sw.Write (json);
sw.Close ();
}
//解析json
void PraseJson ()
{
FileInfo file = new FileInfo (Application.dataPath + "/StreamingAssets/Nuoke.json");
StreamReader reader = new StreamReader (file.OpenRead (), Encoding.UTF8);
string content = reader.ReadToEnd ();
reader.Close ();
reader.Dispose ();
JsonData nuokeData = JsonMapper.ToObject (content);
m_text.text = "英雄:" + nuokeData ["HeroName"] + "\n";
//通过JsonData对象访问数据
for (int i = ; i < nuokeData ["Skills"].Count; i++) {
m_text.text += "技能:" + nuokeData ["Skills"] [i] ["Name"] + "\n技能键:\t" +
nuokeData ["Skills"] [i] ["keyCode"] + "\nCD:\t" +
nuokeData ["Skills"] [i] ["CD"] + "\nMP:\t" + nuokeData ["Skills"] [i] ["MP"] + "\n";
}
}
写入Json的中文会默认转化为UTF-8编码格式,在解析时,只需转换格式即可
unity3d使用litjson中文显示的问题 ,一下代码选至(https://www.cnblogs.com/fyluyg/p/5963052.html)
我们在使用litjson时它的编码方式是unicode的,所以我将json转成string输出时显示的是unicode的编码。这样我们显示或者保存中文时不是很方便。我们可以将中文的unicode转成能识别的GBK编码。
using UnityEngine;
using System.Collections.Generic;
using Utils;
using LitJson;
using System;
using System.Text.RegularExpressions;
public class Script1 : MonoBehaviour
{
public void OnTestJson()
{
JsonData sData = new JsonData();
JsonData data1 = new JsonData();
JsonData data2 = new JsonData();
data1\["等级"\] = "";
data1\["位置"\] = "m=1000,x=33,y=21"; //新手村
data2\["等级"\] = "";
data2\["位置"\] = "m=1001,x=58,y=97"; //桃园镇
sData\["张三"\] = data1;
sData\["李四"\] = data2;
string jsonStr = sData.ToJson();
Regex reg = new Regex(@"(?i)\\\\\[uU\](\[0-9a-f\]{4})");
var ss = reg.Replace(jsonStr, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups\[\].Value, )).ToString(); });
print(ss);
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章