当前位置:网站首页>LitJson解析 生成json文件 读取json文件中的字典
LitJson解析 生成json文件 读取json文件中的字典
2022-06-30 03:22:00 【卿屿-轻尘】
直接打印结果 打印1 代码详情在 GlableManger 中
字典打印结果 打印2 代码详情在 GlableManger 中
//存数据的类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//数据保存用到的类
public class server
{
public int index;//存了多少数据 从1开始
public Dictionary<string , string> Dic_JsonTF = new Dictionary<string , string>();//字典存数据
}
//数据类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class data
{
private static data instanes;
public static data Instanes
{
get
{
if (instanes == null)
{
instanes = new data();
}
return instanes;
}
}
public int index;
public int idname;//json文件保存的名字 暂用
public Dictionary<string , string> Dic_JsonTF = new Dictionary<string, string>();//题目id存
public void Init()//测试用的 后续存数据时用下面的方法添加数据即可
{
OnDic_JsonTF(1,1,true ,1);
OnDic_JsonTF(2, 1, true,2);
OnDic_JsonTF(3, 1, true,1);
OnDic_JsonTF(4, 1, true, 1);
OnDic_JsonTF(5, 1, true, 1);
OnDic_JsonTF(6, 1, true, 1);
}
public void OnDic_JsonTF(int idname, int info, bool tf,int daan)
{
this.idname = idname;//后续json文件保存的名字
index++;//存了多少条数据
// 第几题 第几题 题库id 选择情况 答案 正确情况 分数
// 第几题 第几题 选择情况 正确情况
Dic_JsonTF.Add(idname.ToString (), string.Format("{0}_{1}_{2}_{3}", idname, info, tf, daan));
}
}
//保存本地json文件 读取json文件 单一数据 字典数据
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using LitJson;
public class GlableManger : MonoBehaviour
{
public static GlableManger Instanes;
public InfoUIView_TF infoUIView_TF;
public SaveList savelist = new SaveList();
private void Awake()
{
Instanes = this;
}
Dictionary<int , string> dic = new Dictionary<int, string>();//读取json后存入字典 后续使用
void Start ()//测试用的 保存方法 读取方法
{
data.Instanes.Init();//数据添加 测试用的
_Save();//保存生成本地json文件的方法 如果已存在则修改
OnReadJson();//读取方法
}
private void OnReadJson()
{
//这里是写死了 读取文件的名字 4 后续可根据需要用参数代替
string txt = File.ReadAllText(Application.dataPath + "/JsonData/4.json");
JsonData geojson_data = JsonMapper.ToObject(txt);
JsonData features = geojson_data["Dic_JsonTF"];
JsonData indes = geojson_data["index"];
for (int i = 1; i < int.Parse(indes.ToString()) + 1; i++)
{
features = geojson_data["Dic_JsonTF"][i.ToString()];
string str = JsonMapper.ToJson(features);
//Debug.Log(str);//打印1 顶部图面
dic.Add(i, str);
}
foreach (var item in dic)
{
Debug.Log(item);//打印2 顶部图面
}
}
public void _Save()
{
server save = SaveGame();
string sevejsonstr = JsonMapper.ToJson(save);
//string filename = data.Instanes.idname + "4.json";
string filename = "4.json";//目前写死 测试用的
string filepath = Application.dataPath + "/JsonData/" + filename;
bool b_Save = savelist.savelist.Contains(data .Instanes.idname.ToString());
if (!b_Save)
{
savelist.savelist.Add(data .Instanes.idname.ToString());
string json = JsonUtility.ToJson(savelist);
StreamWriter sw = new StreamWriter(filepath);
sw.Write(sevejsonstr);
sw.Close();
}
else
{
FileStream fs = File.Open(filepath, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
server save1 = SaveGame();
string sevejsonstr1 = JsonMapper.ToJson(save);
sw.Write(sevejsonstr);
sw.Flush();
sw.Close();
fs.Close();
}
}
public server SaveGame()
{
server save = new server();
save.index = data.Instanes.index;
save.Dic_JsonTF = data.Instanes.Dic_JsonTF;
return save;
}
}
[System.Serializable]
public class SaveList
{
public List<string> savelist = new List<string>();
}
边栏推荐
猜你喜欢

简单自定义MVC优化

Prompt learning a blood case caused by a space

OP-diode-限制摆幅

1151_ Makefile learning_ Static matching pattern rules in makefile

General paging (2)

Use of Arthas

O & M (20) make and start USB flash disk and install win10

F1C100S自制开发板调试过程

Principle, advantages and disadvantages of three operating modes of dc/dc converter under light load

Reasons for MySQL master-slave database synchronization failure
随机推荐
What is the metauniverse: where are we, where are we going
1150_ Makefile learning_ Duplicate name target processing in makefile
Auto.js学习笔记15:autojs的UI界面基础篇2
The broadcast module code runs normally in autojs4.1.1, but an error is reported in pro7.0 (not resolved)
If you can tell whether the external stock index futures trading platform I am trading is formal and safe?
浅谈IDEA的优化和使用
X Book 6.97 shield unidbg calling method
Neo4j--- performance optimization
WPF Initialized事件在.cs中绑定不被触发的原因
O & M (20) make and start USB flash disk and install win10
Data set preparation and arrangement
hudi记录
Use of foreach in QT
AppData文件夹下Local,Locallow和Roaming
Global and Chinese market of medical mass notification system 2022-2028: Research Report on technology, participants, trends, market size and share
【微信小程序】条件渲染 列表渲染 原来这样用?
Mysqldump principle
What are the defaults for Binding. Mode=Default for WPF controls?
Auto. JS learning notes 16: save to the mobile phone by project, instead of saving a single JS file every time, which is convenient for debugging and packaging
[live broadcast notes 0629] Concurrent Programming II: lock