美文网首页
5.22Json生成与解析

5.22Json生成与解析

作者: 胤醚貔貅 | 来源:发表于2017-05-22 17:46 被阅读39次

    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;

    }

    }

    相关文章

      网友评论

          本文标题:5.22Json生成与解析

          本文链接:https://www.haomeiwen.com/subject/yvcixxtx.html