10 Json(unity3D)

 //寫入json文檔
注意事項:html

一、在Asset下要有一個StreamingAssets文件夾
二、在文件夾內,有一個已建立好的json空文檔
三、引入命名空間 using Litjson; using System; using System.IO;
四、建立英雄和技能類json

 

Demo所須要的命名空間:app

[Serializable]
class Hero
{
    public string HeroName;
    public string Hp;
    public string Attack;
    public List<Skill> Skills = new List<Skill> ();
}

[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"] = 1000;
        nuoke ["Attack"] = 550;
        nuoke ["Skills"] = new JsonData ();

        //一技能:大殺四方
        JsonData skill1 = new JsonData ();
        skill1 ["keyCode"] = "Q";
        skill1 ["Name"] = "大殺四方";
        skill1 ["CD"] = "9/8/7/6/5";
        skill1 ["MP"] = "30";

        //二技能:致殘打擊
        JsonData skill2 = new JsonData ();
        skill2 ["keyCode"] = "W";
        skill2 ["Name"] = "致殘打擊";
        skill2 ["CD"] = "9/8/7/6/5";
        skill2 ["MP"] = "30";

        //三技能:無情鐵手
        JsonData skill3 = new JsonData ();
        skill2 ["keyCode"] = "E";
        skill2 ["Name"] = "無情鐵手";
        skill2 ["CD"] = "24/21/18/15/12";
        skill2 ["MP"] = "30";

        //大招:諾克薩斯斷頭臺
        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 = 0; 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編碼。

 1 using UnityEngine;
 2 using System.Collections.Generic;
 3 using Utils;
 4 using LitJson;
 5 using System;
 6 using System.Text.RegularExpressions;
 7 
 8 public class Script1 : MonoBehaviour
 9 {
10     public void OnTestJson()
11     {
12         JsonData sData = new JsonData();
13         JsonData data1 = new JsonData();
14         JsonData data2 = new JsonData();
15 
16         data1["等級"] = "54";
17         data1["位置"] = "m=1000,x=33,y=21";   //新手村
18 
19         data2["等級"] = "56";
20         data2["位置"] = "m=1001,x=58,y=97";   //桃園鎮
21 
22         sData["張三"] = data1;
23         sData["李四"] = data2;
24 
25         string jsonStr = sData.ToJson();
26 
27         Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
28         var ss = reg.Replace(jsonStr, delegate(Match m) { return ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString(); });
29 
30 
31         print(ss);
32     }
33 }
相關文章
相關標籤/搜索