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. Windows 怎么启动 apache

    在可执行目录下找到httpd.exe命令,然后运行cmd,执行类似以下命令:C:\"Program Files"\"Apache Software Foundation& ...

  2. 算法竞赛进阶指南--hamilton路径

    // hamilton路径 int f[1 << 20][20]; int hamilton(int n, int weight[20][20]) { memset(f, 0x3f, si ...

  3. 数学--数论--Alice and Bob (CodeForces - 346A )推导

    It is so boring in the summer holiday, isn't it? So Alice and Bob have invented a new game to play. ...

  4. Linux下swap到底有没有必要使用

    周五看到QQ群里在讨论Linux主机上到底需不需要开启swap空间,而且目前公有云主机默认都是把swap关了的,很多公司也是没有开启swap,那到底需不需要开启呢? 我之前在看<鸟哥的Linux ...

  5. andorid jar/库源码解析之Butterknife

    目录:andorid jar/库源码解析 Butterknife: 作用: 用于初始化界面控件,控件方法,通过注释进行绑定控件和控件方法 栗子: public class MainActivity e ...

  6. pytest文档38-allure.setp添加测试用例步骤

    前言 一般流程性的测试用例,写成自动化用例时,步骤较多写起来会比较长.在测试用例里面添加详细的步骤有助于更好的阅读,也方便报错后快速的定位到问题. 举个常见的测试场景用例:从登陆开始,到浏览商品添加购 ...

  7. CC2530入门

    一.简介 单片机(MCU)就是一个将微型计算机系统制作到里面的集成电路芯片. 微控制器的基本结构:内核+外设.内核通过寄存器控制外设:外设通过中断系统通知内核:内核与外设之间通过总线传输数据.地址及控 ...

  8. BufferedInputStream:字节缓冲输入流

    package com.itheima.demo01.BufferedStream; import java.io.BufferedInputStream; import java.io.FileIn ...

  9. [hdu5439 Aggregated Counting]公式化简,预处理

    题意:按下列规则生成一组序列,令f(n)为n这个数在序列中出现的最后一个位置,求f(f(n))的值. 1. First, write down 1, 2 on a paper.2. The 2nd n ...

  10. [hdu4801]搜索

    http://acm.hdu.edu.cn/showproblem.php?pid=4801 状态和生成状态的过程处理好了,这个题就是简单的搜索题了== #include <iostream&g ...