C#實(shí)現(xiàn)讀取寫(xiě)入Json文件
更新時(shí)間:2023年01月25日 16:12:11 作者:暗夜__
這篇文章主要介紹了C#實(shí)現(xiàn)讀取寫(xiě)入Json文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
C#讀取寫(xiě)入Json文件
/// <summary>
/// 將序列化的json字符串內(nèi)容寫(xiě)入Json文件,并且保存
/// </summary>
/// <param name="path">路徑</param>
/// <param name="jsonConents">Json內(nèi)容</param>
private void WriteJsonFile(string path, string jsonConents)
{
File.WriteAllText(path, jsonConents, System.Text.Encoding.UTF8);
}
/// <summary>
/// 獲取到本地的Json文件并且解析返回對(duì)應(yīng)的json字符串
/// </summary>
/// <param name="filepath">文件路徑</param>
/// <returns></returns>
private string GetJsonFile(string filepath)
{
string json = string.Empty;
using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
{
json = sr.ReadToEnd().ToString();
}
}
return json;
}
/// <summary>
/// 對(duì)象 轉(zhuǎn)換為Json字符串
/// </summary>
/// <param name="tablelList"></param>
/// <returns></returns>
public string toJson(object tablelList)
{
DataContractJsonSerializer json = new DataContractJsonSerializer(tablelList.GetType());
string finJson = "";
//序列化
using (MemoryStream stream = new MemoryStream())
{
json.WriteObject(stream, tablelList);
finJson = Encoding.UTF8.GetString(stream.ToArray());
}
Debug.Log(tablelList + "JSON數(shù)據(jù)為:" + finJson);
return finJson;
}
將數(shù)據(jù)寫(xiě)入Json文件中
部分邏輯代碼
? //需要寫(xiě)入Json文件中的數(shù)據(jù)集合 ? List<MonsterSpawnPoint> MonsterPos = new List<MonsterSpawnPoint>(); ? //轉(zhuǎn)為string 字符串 ? ? string text = toJson(MonsterPos); ? ? //寫(xiě)入Json文件 ?filepath文件路徑? ? ? string filepath=Path.Combine(Application.dataPath, "../", "Config_NPC.json"); ? ? ? ?WriteJsonFile(filepath, text);
Json文件
[
? {
? ? "Direction": 0,
? ? "Index": 9,
? ? "PositionX": 28,
? ? "PositionY": 10,
? ? "PositionZ": 20
? },
? {
? ? "Direction": 0,
? ? "Index": 9,
? ? "PositionX": 40,
? ? "PositionY": 10,
? ? "PositionZ": 18
? },
? {
? ? "Direction": 0,
? ? "Index": 4,
? ? "PositionX": 21,
? ? "PositionY": 10,
? ? "PositionZ": -8
? },
? {
? ? "Direction": 0,
? ? "Index": 4,
? ? "PositionX": 40,
? ? "PositionY": 10,
? ? "PositionZ": -13
? }
]讀取Json文件
Json對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)
public class MonsterSpawnPoint
{
public long Index;//怪物索引
public int PositionX;//怪物X坐標(biāo)
public int PositionY;//怪物Y坐標(biāo)
public int PositionZ;//怪物Z坐標(biāo)
public int Direction;
}
//讀取指定數(shù)據(jù)集合Json
MonsterSpawnPoint[] monsterPoints = LitJson.JsonMapper.ToObject<MonsterSpawnPoint[]>(GetJsonFile(filepath));
//讀取Json 通過(guò)字符串 索引取得Json表中的值
LitJson.JsonData jsonData = LitJson.JsonMapper.ToObject(GetJsonFile(fillePath));
foreach (JsonData item in jsonData)
{
JsonData id_data = item["Id"];//通過(guò)字符串 索引取得鍵值對(duì)的值
JsonData name_data = type == InitType.Monster ? item["Name"] : item["NPCName"];//通過(guò)字符串 索引取得鍵值對(duì)的值
tempData = new Dropdown.OptionData();
tempData.text = $"[{id_data}]{name_data}";
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Python讀取和寫(xiě)入txt、Excel文件和JSON文件的方法
- C++利用jsoncpp庫(kù)實(shí)現(xiàn)寫(xiě)入和讀取json文件
- Python如何讀取、寫(xiě)入JSON數(shù)據(jù)
- python3 循環(huán)讀取excel文件并寫(xiě)入json操作
- Node.js fs模塊(文件模塊)創(chuàng)建、刪除目錄(文件)讀取寫(xiě)入文件流的方法
- Node.js readline 逐行讀取、寫(xiě)入文件內(nèi)容的示例
- Python讀取Json字典寫(xiě)入Excel表格的方法
- Javascript寫(xiě)入txt和讀取txt文件示例
- JavaScript?讀取及寫(xiě)入本地文件的操作方法
相關(guān)文章
使用C#的正則表達(dá)式驗(yàn)證中文字符(實(shí)例代碼)
本文通過(guò)實(shí)例代碼給大家介紹了使用C#的正則表達(dá)式驗(yàn)證中文字符的方法,需要的的朋友參考下吧2017-07-07
使用C#實(shí)現(xiàn)在屏幕上畫(huà)圖效果的代碼實(shí)例
本篇文章是對(duì)使用C#在屏幕上畫(huà)圖效果的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#實(shí)現(xiàn)軟件開(kāi)機(jī)自啟動(dòng)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)軟件開(kāi)機(jī)自啟動(dòng),且不需要管理員權(quán)限,文中的示例代碼講解詳細(xì),需要的小伙伴可以參考一下2023-07-07
Datagridview使用技巧(9)Datagridview的右鍵菜單
這篇文章主要為大家詳細(xì)介紹了Datagridview使用技巧,Datagridview的右鍵菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
淺談C# StringBuilder內(nèi)存碎片對(duì)性能的影響
這篇文章主要介紹了淺談StringBuilder內(nèi)存碎片對(duì)性能的影響,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
c# 線(xiàn)性回歸和多項(xiàng)式擬合示例詳解
線(xiàn)性回歸與多項(xiàng)式擬合是兩種常用的回歸分析方法,線(xiàn)性回歸模型簡(jiǎn)單,易于計(jì)算,但只適用于線(xiàn)性關(guān)系的數(shù)據(jù),多項(xiàng)式擬合能處理非線(xiàn)性數(shù)據(jù),模型更復(fù)雜,擬合度更高,但容易產(chǎn)生過(guò)擬合問(wèn)題,計(jì)算成本較高,適用場(chǎng)景不同,線(xiàn)性回歸適合線(xiàn)性數(shù)據(jù),多項(xiàng)式擬合適合非線(xiàn)性數(shù)據(jù)2024-10-10

