赞
踩
1、JsonUtility读写Josn文件
(1)读取Json文件
JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath))
JsonUtility.FromJson<Json结构类>(File.ReadAllText(Json文件地址))
(2) 写入Josn文件
File.WriteAllText(JsonPath, JsonUtility.ToJson(TJ))
JsonPath Json路径
JsonUtility.ToJson(TJ)将修改后的Josn数据转为字符串
2、使用LitJson.dll文件读写Json文件(需要单独下载LitJson.dll文件)
如果使用LitJson解析文件需要引用引用LitJson.dll
(1)读取Json文件
JsonMapper.ToObject<TestJson>(File.ReadAllText(JsonPath));
(2)写入Json文件
File.WriteAllText(JsonPath, JsonMapper.ToJson(TJ));
使用 JsonUtility API读取Json文件
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- //using LitJson;
-
- public class ReadJson : MonoBehaviour
- {
- private string JsonPath=Application.streamingAssetsPath+ "/TestJson.Json";
- public static TestJson TJ=new TestJson ();
- // Start is called before the first frame update
- void Start()
- {
- JsonRead();
-
- }
-
- // Update is called once per frame
- void Update()
- {
- if (Input.GetKeyDown(KeyCode.R))
- {
- Debug.Log("读取Json文件");
- JsonRead();
-
- }if (Input.GetKeyDown(KeyCode.W))
- {
- Debug.Log("写入json");
- JsonWrite();
- }
- }
- /// <summary>
- /// 读取Json文件
- /// </summary>
- public void JsonRead()
- {
- if (!File.Exists(JsonPath))//判断地址是否存在,如果不存在则提示并中直以下操作
- {
- Debug.LogError("地址不存在"+JsonPath);
- return;
- }
- TJ =JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath));//使用File读取字符串内容,并使用JsonUtility读取到的文件解析为json数据
- // TJ = JsonMapper.ToObject<TestJson>(File.ReadAllText(JsonPath));//使用LitJson读取字符内容,并将读取到的文件解析为json数据
- Debug.Log("ID:"+TJ.ID);//打印读取对应的Id属性
- Debug.Log("Name:"+TJ.Name);//打印读取对应的Name属性
- Debug.Log("装备数量:"+TJ.Equips.Count);//打印读取对应的装备数量
- foreach (var item in TJ.Equips)//打印装备数组中的每个装备的数据
- {
- Debug.Log("装备名称:"+item.Name+"\n"+
- "装备类型:"+item.Label+"\n"+
- "装备伤害:"+item.Damage+"\n"+
- "装备防御:"+item.DefenseValue+"\n"+
- "装备描述:"+item.Describe);
- }
- }
- /// <summary>
- /// 写入json文件
- /// </summary>
- public void JsonWrite()
- {
- if (!File.Exists(JsonPath))//判断地址是否存在,如果不存在则提示并中直以下操作
- {
- Debug.LogError("地址不存在" + JsonPath);
- return;
- }
- TJ.Equips[1].Name = "双手剑";
- TJ.Equips[1].Label = "武器";
- TJ.Equips[1].Damage = "888";
- TJ.Equips[1].DefenseValue = "25";
- TJ.Equips[1].Describe = "这把双手剑的造型非常漂亮,从上面散发的光芒就可以感觉到此剑的非凡。";
- foreach (var item in TJ.Equips)//打印装备数组中的每个装备的数据
- {
- Debug.Log("装备名称:" + item.Name + "\n" +
- "装备类型:" + item.Label + "\n" +
- "装备伤害:" + item.Damage + "\n" +
- "装备防御:" + item.DefenseValue + "\n" +
- "装备描述:" + item.Describe);
- }
- File.WriteAllText(JsonPath, JsonUtility.ToJson(TJ));//使用JsonUtility写入Json内容
- //File.WriteAllText(JsonPath, JsonMapper.ToJson(TJ));//使用LitJson写入Json内容
- TJ = JsonUtility.FromJson<TestJson>(File.ReadAllText(JsonPath));//使用File读取Json内容,并将读取到的文件解析为json数据
-
- }
- }
-
- [Serializable]
- public class TestJson
- {
- public string ID;
- public string Name;
- public List<Equips> Equips;
- }
- [Serializable]
- public class Equips
- {
- /// <summary>
- /// 名称
- /// </summary>
- public string Name;
- /// <summary>
- /// 标签
- /// </summary>
- public string Label;
- /// <summary>
- /// 伤害
- /// </summary>
- public string Damage;
- /// <summary>
- /// 防御值
- /// </summary>
- public string DefenseValue;
- /// <summary>
- /// 装备描述
- /// </summary>
- public string Describe;
- }

与之应Json文件
-
- {
- "ID": 6963,
- "Name": "测试用",
- "Equips": [
- {
- "Name": "头盔",
- "Label": "防具",
- "Damage": 12,
- "DefenseValue": 123,
- "Describe": "这是一个坚固的头盔"
- },
- {
- "Name": "大剑",
- "Label": "武器",
- "Damage": 666,
- "DefenseValue": 13,
- "Describe": "这把大剑锈上面有一些历史的痕迹"
- }
- ]
- }
-
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。