5.22Json生成与解析

using UnityEngine;

using System.Collections;

using LitJson;

using System.IO;

using System.Text;

using UnityEditor;

using System.Collections.Generic;

public class  JsonDcript:MonoBehaviour{

public  TextAsset    txt;//读取Asset文件

//创建文件夹路径

string   dirpath;

//创建文件路径

string   filePath;

voidStart(  ){

dirpath=Application.dataPath+"/JsonFiles";

filePath=dirpath+"/MyJson.txt";

//MergerAndSaveJson( );

//对象的所有属性(字段),转化为Json格式字符串

//HreoDataModel model=new HreoDataModel( );

//model.Hero_Name="xiaoli";

//model.Hp=100;

//model.Attack=30;

        //LitJson

//print(JsonMapper.ToJson(model));

//string   modelstring=JsonMapper.ToJson(model);

       //JsonUtiluty

//print(JsonUtility.ToJson(model));

//将得到的json字符串,依次赋值给指定的数据模型对象

//StreamReader   reader=new   StreamReader(filePath);

//string   jsonStr=reader.ReadToEnd( );

//HreoDataModel   model_2=new  HreoDataModel( );

//model_2=JsonMapper.ToObject(jsonStr);

//model_2=JsonUtility.FromJson(jsonStr);

//foreach( HeroSkillDataModel   dm  in  model_2.Skill){

//print(dm.passivity);

}

}

//合成Json并写入文件

public  void   MergerAndSaveJson( ){

StringBuilder    strBuilder=new  StringBuilder( );

JsonWriter  writer=new   JsonWriter(strBuilder);

writer.WriteObjectStart( );//{


writer.WritePropertyName("Hero_Name");

writer.Write("hero");


writer.WritePropertyName("Hp");

writer.Write(100);


writer.WritePropertyName("Attack");

writer.Write(50);


writer.WritePropertyName("Skill");//数组

writer.WriteArrayStart( );//[


writer.WriteObjectStart( );//{

writer.WritePropertyName("被动技能");

writer.Write("出血");

writer.WriteObjectEnd();//}


writer.WriteObjectStart( );//{

writer.WritePropertyName("Q技能");

writer.Write("大杀四方");

writer.WritePropertyName("冷却时间");

writer.Write("7/6/5/4/3");

writer.WritePropertyName("消耗");

writer.Write(30);

writer.WriteObjectEnd( );//}


writer.WriteObjectStart( );

writer.WritePropertyName("W技能");

writer.Write("残忍打击");

writer.WritePropertyName("冷却时间");

writer.Write("8/7/6/2/1");

writer.WritePropertyName("消耗");

writer.Write(50);

writer.WriteObjectEnd( );


writer.WriteObjectStart( );

writer.WritePropertyName("E技能");

writer.Write("无情铁手");

writer.WritePropertyName("冷却时间");

writer.Write("9/5/3/1");

writer.WritePropertyName("消耗");

writer.Write(60);

writer.WriteObjectEnd( );


writer.WriteArrayEnd( );//]

writer.WriteObjectEnd( );//}

Debug.Log(strBuilder);

DirectoryInfo  dir=new  DirectoryInfo(dirpath);  //创建文件的目录

if(!dir.Exists){  //检查文件夹是否存在

Directory.CreateDirectory(dirpath);

//如果当前程序运行在Unity软件中才会执行下面语句

#ifUNITY_EDITOR

AssetDatabase.Refresh( );//刷新

#endif

}

//把Json字符串写入Txt

StreamWriter  sw;

if(File.Exists(filePath)){

//如果文件存在,就继续往文件中添加内容

sw=File.AppendText(filePath);

}else{

//如果文件不存在,则创建文件

sw=File.CreateText(filePath);

}

sw.WriteLine(strBuilder);

sw.Close( );//关闭输入流

#ifUNITY_EDITOR

AssetDatabase.Refresh( );

#endif

}


public void  ReadJson( ){  //新版本

//从文件读取Json字符串

if(File.Exists(filePath)){

StreamReader  reader=new  StreamReader(filePath);

string  jsonStr=reader.ReadToEnd( );//从头读到尾

//将json格式的字符串转换为json数据,然后操作json数据

//LitJson

JsonData  jd=JsonMapper.ToObject(jsonStr);

foreach(JsonData  josnData  in   jd["Skill"]){

foreach(string  key  in   josnData.Keys){

Debug.Log(key+":"+josnData[key]);

}

}

}

}

public   T   JsonToModel <T>(string  path)where T:class,new( ){//泛型方法

if(File.Exists(path)){

StreamReader  sr=new  StreamReader(path);

string   jsonStr=sr.ReadToEnd( );

T   model=JsonMapper.ToObject (jsonStr) as  T;

return   model;

}

return   null;

}

}


usingUnityEngine;

usingSystem.Collections;

usingSystem.Collections.Generic;

public class   HreoDataModel{

public   string   Hero_Name;

public    int    Hp;

public    int     Attack;

public   List <HeroSkillDataModel>Skill;

public  override   string    ToString( )

{

return  Hero_Name+", "+Hp.ToString( );

}

}


usingUnityEngine;

usingSystem.Collections;

public  class  HeroSkillDataModel{

public  string  passivity="";

public   string  Q;

public   string  W;

public    string  E;

public    string CoolTime;

public  string    consume;

}

MVC

usingUnityEngine; 

usingSystem.Collections;

usingSystem.IO;

usingLitJson;

public class UIModel{

public delegate void Data UpdateDelegate( );

public event  DataUpdateDelegate   dataUpdateEvent;

public string  mPlayer_name;

public  string  mPlayer_money;

public  string  mPlayer_exp;

public  string  mPlayer_race;

public  string  mPlayer_hp;

public  string  mPlayer_attack;

public  string  mPlayer_defense;

//自定义初始化

//重写toString( )

public  void   JsonToModel(string  path){

if(File.Exists(path)){

StreamReader  sr=new  StreamReader(path);

string  jsonStr=sr.ReadToEnd( );

UIModel  model=JsonMapper.ToObject<UIModel>(jsonStr);

this.mPlayer_name=model.mPlayer_name;

this.mPlayer_money=model.mPlayer_money;

this.mPlayer_exp=model.mPlayer_exp;

this.mPlayer_race=model.mPlayer_race;

this.mPlayer_hp=model.mPlayer_hp;

this.mPlayer_attack=model.mPlayer_attack;

this.mPlayer_defense=model.mPlayer_defense;

dataUpdateEvent( );

}

}

}


usingUnityEngine;

usingSystem.Collections;

usingUnityEngine.UI;

publicclassUIManager:MonoBehaviour{

public Text nameText;

public Text moneyText;

public Text expText;

public Text raceText;

public Text hpText;

public Text attackText;

public Text  defenseText;


}


usingUnityEngine;

usingSystem.Collections;

publicclassGameController:MonoBehaviour{

UIManager   manager;

UIModel    model;

void Start( ){

manager=GameObject.Find("UIManager").GetComponent<UIManager>( );

model=new UIModel( );

model.dataUpdateEvent+=UpdataView;

}

void  Update( ){

if(Input.GetKeyDown(KeyCode.U)){

model.JsonToModel(Application.dataPath+"/JsonFiles/MyJson.txt");

}

}

void   UpdataView( ){

manager.nameText.text=model.mPlayer_name;

manager.moneyText.text=model.mPlayer_money;

manager.expText.text=model.mPlayer_exp;

manager.raceText.text=model.mPlayer_race;

manager.hpText.text=model.mPlayer_hp;

manager.attackText.text=model.mPlayer_attack;

manager.defenseText.text=model.mPlayer_defense;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,719评论 19 139
  • 将Json.txt的天气信息获取并显示在UI usingUnityEngine; usingSystem.Coll...
    胤醚貔貅阅读 3,122评论 0 0
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,482评论 0 23
  • 疼惜自己就是当你端了一盘刚出炉的铜锣烧出来,有人一箭步上来要抢第一块的时候,你投给他一种凶狠的眼神,你要疼惜那个投...
    与姝会友阅读 2,669评论 0 0
  • 以前我是不宅的,至少是我这么认为。 最近的生活都是从窗外看天,剩下的眼光都是分散在各种屏幕上,觉得自己要进化出第三...
    厌世者orz阅读 2,372评论 0 0