首先在Unity的Assets目錄下新建一個(gè)Plugins文件夾,引入LitJson.dll類(lèi)庫(kù)?。?!
編寫(xiě)讀寫(xiě)腳本時(shí),還需要再導(dǎo)入命名空間 using LitJson
關(guān)于Litjson文件:
可以去官網(wǎng)下載一個(gè)文件包;.dll文件存在于 ?litjson-0.5.0/bin目錄。
?Litjson官網(wǎng)下載鏈接:LitJSON download | SourceForge.net
具體操作:
?先定義一個(gè)類(lèi),假如現(xiàn)在是要查看手機(jī)里APP的信息,把信息先放進(jìn)去(這里分為簡(jiǎn)單和復(fù)雜讀取時(shí)的情況,測(cè)試的時(shí)候要先把另一個(gè)注釋掉?。?/p>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class APPofMine
{
public int appNum;
public bool phoneState;
//簡(jiǎn)單寫(xiě)入和讀取的時(shí)候直接用List來(lái)保存數(shù)據(jù)
//public List<string> appList;
//復(fù)雜寫(xiě)入和讀取的時(shí)候定義一個(gè)List存放另一個(gè)類(lèi)里的數(shù)據(jù)
public List<AppProperty> appPropertiesList;
}
另一個(gè)類(lèi)里的數(shù)據(jù):?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AppProperty
{
public string appName;
public string ErenID;
public bool ErenFavour;
public List<int> useTimeList;
}
寫(xiě)入和讀取的方法及調(diào)用測(cè)試代碼:
代碼比較長(zhǎng),有詳細(xì)的注釋?zhuān)】梢灾苯尤螐?fù)制~
PS:測(cè)試時(shí)要先在Unity創(chuàng)建一個(gè)物體,然后把此腳本掛在物體下,另外還需要在Assets目錄下創(chuàng)建一個(gè) Resources 文件夾,再在Resources 文件夾下創(chuàng)建一個(gè)Json文件夾?。?!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class JosnTest : MonoBehaviour
{
private APPofMine appOfMine;
// Start is called before the first frame update
void Start()
{
#region 簡(jiǎn)單Json信息的存儲(chǔ)和讀取
//簡(jiǎn)單Json信息的存儲(chǔ)和讀取
/*
寫(xiě)入
//appOfMine = new APPofMine
//{
// appNum = 3,
// phoneState = true,
// appList = new List<string>()
// {
// "抖音","BiliBili","絕地求生"
// }
//};
//SaveByJson();
//讀取
appOfMine = new APPofMine();
appOfMine = LoadByJson();
print(appOfMine.appNum);
print(appOfMine.phoneState);
foreach (var item in appOfMine.appList)
{
print(item);
}
*/
#endregion
#region 復(fù)雜Json信息寫(xiě)入和讀取
寫(xiě)入
//appOfMine = new APPofMine
//{
// appNum = 3,
// phoneState = true,
// appPropertiesList = new List<AppProperty>()
//};
//AppProperty appProperty = new AppProperty
//{
// appName = "抖音",
// ErenID = "冬瓜大浪",
// ErenFavour = true,
// useTimeList = new List<int> { 6, 7, 8 }
//};
//appOfMine.appPropertiesList.Add(appProperty);
//SaveByJson();
//讀取
appOfMine = LoadByJson();
print(appOfMine.appNum);
print(appOfMine.phoneState);
foreach (var item in appOfMine.appPropertiesList)
{
print(item);
print(item.appName);
print(item.ErenFavour);
print(item.ErenID);
foreach (var itemGo in item.useTimeList)
{
print(itemGo);
}
}
#endregion
}
#region 簡(jiǎn)單存儲(chǔ)Json信息文件的方法
//存儲(chǔ)Json信息文件
private void SaveByJson()
{
//找到文件要存儲(chǔ)的路徑
string filePath = Application.dataPath/*Assets根目錄*/ + "/Resources"/*后續(xù)路徑*/ + "/APPofMine.json"/*文件名*/;
//利用JsonMapper將 信息類(lèi)對(duì)象(字段名) 轉(zhuǎn)化成 json格式 的 字符串
string saveJsonStr = JsonMapper.ToJson(appOfMine);
//創(chuàng)建一個(gè)文件流將字符串寫(xiě)入一個(gè)文件中
StreamWriter sw = new StreamWriter(filePath);
//開(kāi)始寫(xiě)入
sw.Write(saveJsonStr);
//關(guān)閉文件流
sw.Close();
}
#endregion
#region 簡(jiǎn)單讀取Json的信息文件
//讀取Json的信息文件
private APPofMine LoadByJson()
{
APPofMine appGo = new APPofMine();
//找到需要讀取文件的路徑
string filePath = Application.dataPath/*Assets根目錄*/ + "/Resources"/*后續(xù)路徑*/ + "/APPofMine.json"/*文件名*/;
//先判斷如果文件夾存在
if (File.Exists(filePath))
{
//創(chuàng)建一個(gè)新的變量 sr ,用來(lái)存儲(chǔ)讀取到的數(shù)據(jù),開(kāi)始讀取
StreamReader sr = new StreamReader(filePath);
//把讀取到的信息轉(zhuǎn)換成字符串的形式存儲(chǔ)下來(lái)
string loadJsonStr = sr.ReadToEnd();
//關(guān)閉文件流
sr.Close();
//存儲(chǔ)讀取到的數(shù)據(jù)
appGo = JsonMapper.ToObject<APPofMine>(loadJsonStr);
}
//判空
if (appGo == null)
{
Debug.Log("讀取Json文件失敗");
}
return appGo;
}
#endregion
}
最近工作中遇到一個(gè)需求,后端的一個(gè)接口的參數(shù)是List,List中的元素又是數(shù)組,相當(dāng)于二維數(shù)組。但是二維數(shù)組無(wú)法直接轉(zhuǎn)成Json格式傳遞。?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-404416.html
? ? ? 搞了好一會(huì)兒,記錄一下方法:文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-404416.html
JsonData addressInfo = new JsonData();//創(chuàng)建一個(gè)addressInfo對(duì)象
foreach (var item in companyInfo.address)
{
JsonData dataArray = new JsonData();//在addressInfo中再創(chuàng)建一個(gè)對(duì)象
dataArray["id"] = item.id;
dataArray["address"] = item.address;
addressInfo.Add(dataArray);//依次添加進(jìn)addressInfo
}
jd["address"] = addressInfo;//賦值
到了這里,關(guān)于Unity——寫(xiě)入和讀取Json信息的方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!