using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
using UnityEditor; public class Person
{
public string Name { get; set; }
public double HP { get; set; }
public int Level { get; set; }
public double Exp { get; set; }
public int Attak { get; set; } }
public class PersonList
{
public Dictionary<string, string> dictionary = new Dictionary<string, string>();
} public class Classtext : MonoBehaviour {
/*定义一个Person对象(其属性包括,Name,HP,Level,Exp,Attak等),
将其转会成json格式字符串并且写入到person.json的文本中,
然后将person.json文本中的内容读取出来赋值给新的Person对象。
*/ public PersonList personList = new PersonList(); // Use this for initialization
void Start () {
//初始化人物信息
Person person = new Person();
person.Name = "Czhenya";
person.HP = 100;
person.Level = 30;
person.Exp = 999.99;
person.Attak = 38; //调用保存方法
Save(person); }
/// <summary>
/// 保存JSON数据到本地的方法
/// </summary>
/// <param name="player">要保存的对象</param>
public void Save(Person player)
{
//打包后Resources文件夹不能存储文件,如需打包后使用自行更换目录
string filePath = Application.dataPath + @"/Resources/JsonPerson.json";
Debug.Log(Application.dataPath + @"/Resources/JsonPerson.json"); if (!File.Exists(filePath)) //不存在就创建键值对
{
personList.dictionary.Add("Name", player.Name);
personList.dictionary.Add("HP", player.HP.ToString());
personList.dictionary.Add("Level", player.Level.ToString());
personList.dictionary.Add("Exp", player.Exp.ToString());
personList.dictionary.Add("Attak", player.Attak.ToString()); }
else //若存在就更新值
{
personList.dictionary["Name"] = player.Name;
personList.dictionary["HP"] = player.HP.ToString();
personList.dictionary["Level"] = player.Level.ToString();
personList.dictionary["Exp"] = player.Exp.ToString();
personList.dictionary["Attak"] = player.Attak.ToString();
} //找到当前路径
FileInfo file = new FileInfo(filePath);
//判断有没有文件,有则打开文件,,没有创建后打开文件
StreamWriter sw = file.CreateText();
//ToJson接口将你的列表类传进去,,并自动转换为string类型
string json = JsonMapper.ToJson(personList.dictionary);
//将转换好的字符串存进文件,
sw.WriteLine(json);
//注意释放资源
sw.Close();
sw.Dispose(); AssetDatabase.Refresh(); } /// <summary>
/// 读取保存数据的方法
/// </summary>
public void LoadPerson()
{
//调试用的 //Debug.Log(1); //TextAsset该类是用来读取配置文件的
TextAsset asset = Resources.Load("JsonPerson") as TextAsset;
if (!asset) //读不到就退出此方法
return; string strdata = asset.text;
JsonData jsdata3 = JsonMapper.ToObject(strdata);
//在这里循环输出表示读到了数据,,即此数据可以使用了
for (int i = 0; i < jsdata3.Count; i++)
{
Debug.Log(jsdata3[i]);
}
//使用foreach输出的话会以[键,值],,,
/*foreach (var item in jsdata3)
{
Debug.Log(item);
}*/ } private void OnGUI()
{ //点击读取存储的文件
if (GUILayout.Button("LoadTXT"))
{
LoadPerson();
}
}
}

Unity实现写入json文件的更多相关文章

  1. python 数据写入json文件时中文显示Unicode编码问题

    一.问题描述 import json dir = { '春晓':'asfffa', '春眠不觉晓' : '处处闻啼鸟', '夜来风雨声' : 56789, 'asdga':'asdasda' } fp ...

  2. Json.NET读取和写入Json文件

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. C++简单使用Jsoncpp来读取写入json文件

    一.源码编译 C++操作json字符串最好的库应该就是jsoncpp了,开源并且跨平台.它可以从这里下载. 下载后将其解压到任意目录,它默认提供VS2003和VS2010的工程文件,使用VS2010可 ...

  4. scrapy相关 通过设置 FEED_EXPORT_ENCODING 解决 unicode 中文写入json文件出现`\uXXXX`

    0.问题现象 爬取 item: 2017-10-16 18:17:33 [scrapy.core.scraper] DEBUG: Scraped from <200 https://www.hu ...

  5. 在类文件中创建 写入Json文件

    由于业务需要 今天写了一个方法能够定时更新Json文件 即定时从数据库中查询数据 然后转化为Json对象 如果有数据的话 删掉之前的Json文件 重新创建一个文件 然后写入Json对象 中间走了很多弯 ...

  6. nodejs写入json文件,格式化输出json的方法

    假如我需要把data序列化成json字符串,然后写入data.json文件中,代码如下: let str = JSON.stringify(data) fs.writeFile('data.json' ...

  7. 19.JAVA-从文件中解析json、并写入Json文件(详解)

    1.json介绍 json与xml相比, 对数据的描述性比XML较差,但是数据体积小,传递速度更快. json数据的书写格式是"名称:值对",比如: "Name" ...

  8. scrapy基础知识之将item写入JSON文件:

    pipelines.py import json class xxPipeline(object):     def __init__(self):         self.filename=ope ...

  9. Node.js读取某个目录下的所有文件夹名字并将其写入到json文件

    针对解决的问题是,有些时候我们需要读取某个文件并将其写入到对应的json文件(xml文件也行,不过目前用json很多,json是主流). 源码如下:index.js var fs = require( ...

随机推荐

  1. 数学--数论--随机算法--Pollard Rho 大数分解算法 (带输出版本)

    RhoPollard Rho是一个著名的大数质因数分解算法,它的实现基于一个神奇的算法:MillerRabinMillerRabin素数测试. 操作流程 首先,我们先用MillerRabinMille ...

  2. 日常开发中常用的linux命令

    本文并不将linux的常用命令全部罗列出来,列出一下常用.容易忘记的命令. 更详细的说明见:https://www.cnblogs.com/xuxinstyle/p/9609551.html 文件相关 ...

  3. 2020年ubuntu1804安装nginx最新稳定版1.16详细教程笔记

    第一次使用nginx是2007年,当时主流还是apache.nginx横空出世,在web2.0的推动下,迅速崛起.眼下已是绝对的主流了. 当时,还有一个轻量级的lighttpd,是德国人写,刚开始还并 ...

  4. JMeter Nmon Tool V2.0 插件

    很早之前宝路已将nmon监控功能集成到了JMeter中,自己在使用旧版本时,也有诸多不满意的地方.趁着五一假期(基本都是晚上,白天要陪孩子),对插件底层代码进行了重构,自己还要反复测试调整,最晚的一次 ...

  5. Java——单双引号的区别

    单引号: 单引号包括的是单个字符,表示的是char类型.例如: char  a='1' 双引号: 双引号可以包括0个或者多个字符,表示的是String类型. 例如: String s="ab ...

  6. LeetCode--Jewels and Stones && Range Sum of BST (Easy)

    771. Jewels and Stones (Easy)# You're given strings J representing the types of stones that are jewe ...

  7. 心路历程-安装Docker

    心路历程-安装Docker 本机环境 Windows10 激活HyperV功能 新建CentOS虚拟机 centos docker安装 由于是新的虚拟机,所以没有docker旧版本的问题,不需要卸载旧 ...

  8. 【华为云技术分享】智能诊断和优化,华为云DAS服务云DBA平台让您无忧运维

    摘要:随着时代的发展,传统的“人工”运维方式,已经逐渐跟不上企业业务发展的需要.如何更好的保证数据库系统的稳定性.安全性.完整性和高性能,实现运维工具化.产品化.自助化.自动化,是当前数据管理和运维面 ...

  9. 【FreeRTOS学习03】小白都能懂的Task Management 任务管理基本概念介绍

    在FreeRTOS中,线程的术语又可以被称之为任务,或许这样更加合适,本文将介绍任务的创建/删除,任务参数的使用,以及任务优先级: 1 软实时和硬实时 硬实时系统的任务运行正确性与响应时限是紧密相关的 ...

  10. FOC:在MCU上检验Clark和Park坐标变换是否正确

    文章目录 前言 程序 头文件 clark 变换 C实现 park c 变换实现 仿真 前言 仿真简单,可以参考仿真的结果,但是实际中将代码移植到MCU,会出现一些新的问题,所以需要对坐标变换部分算法进 ...