当前位置:网站首页>Operation of JSON file
Operation of JSON file
2022-06-30 05:08:00 【zjh_ three hundred and sixty-eight】
【JSON file 】
1、 Save the file ( serialize ), Save program objects locally
// Program object class
public class Paths
{
public string Name;
public List<Vector3> position;
public List<Vector3> rotation;
}
Paths paths;
/// <summary>
/// Initialize the path class
/// </summary>
void InitPaths()
{
paths = new Paths();
paths.Name = " This is a test ";
paths.position = InputData.listPosition;
paths.rotation = InputData.listRotation;
}
/// <summary>
/// preservation json
/// </summary>
/// <param name="pathName"></param>
//private void SaveJson(string pathName)
private void SaveJson()
{
//string savePath = @"C:\Users\T01015\Desktop\Test01.json";
//string savePath = @"D:\AutoPath";
if (!Directory.Exists(savePath))
{
// Automatically create a folder when it does not exist
Directory.CreateDirectory(savePath);
}
InitPaths();
DirectoryInfo dir = new DirectoryInfo(savePath);
string json = JsonUtility.ToJson(paths);
File.WriteAllText(savePath + @"\" + " programme " + dir.GetFiles().Length + ".json", json, Encoding.UTF8);
print(" Saved address :" + savePath);
}2、 Read the file ( Deserialization ), Convert a file to an object for program use
void LoadJson(string txtName)
{
// Clear the scene roadmap
for (int i = 0; i < pointPoll.childCount; i++)
{
Destroy(pointPoll.GetChild(i).gameObject);
}
string str = File.ReadAllText(savePath + @"\" + txtName + ".json" , Encoding.Default);
paths = JsonUtility.FromJson<Paths>(str);// analysis json
print(paths.Name);
foreach (Vector3 item in paths.position)
{
print(item.x+","+item.y+","+item.z);
}
}Pit point : If you have created a collection object class , Then the object class should be marked [Serializable], Otherwise, the content cannot be parsed . for example :
// Json Resolve to this object
public class Response
{
public List<Student> list;
}
[Serializable]
public class Student
{
public int id;
public string name;
}
边栏推荐
- Untiy3d controls scene screenshots through external JSON files
- Force buckle 977 Square of ordered array
- Procedural animation -- inverse kinematics of tentacles
- z-index属性在什么情况下会失效?
- Chapter 10 of OpenGL super classic (7th Edition) calculation shader
- 力扣349. 两个数组的交集
- Unity enables simple music visualization
- Pytorch的安装以及入门使用
- JPA composite primary key usage
- Unity realizes rotation and Revolution
猜你喜欢

PBR material: basic principle and simple fabrication

Unreal 4 unavigationsystemv1 compilation error

Force buckle 349 Intersection of two arrays

虚析构和纯虚析构

Deeply understand the function calling process of C language

Harbor API 2.0 query

Unity + hololens common basic functions

Redis cluster concept

Records of problems encountered in unity + hololens development

UE4 method of embedding web pages
随机推荐
Nestjs configures static resources, template engine, and post examples
Force buckle 59 Spiral matrix II
Untiy3d controls scene screenshots through external JSON files
Unity Logitech steering wheel access
Passing values between classes using delegates and events
One command to run rancher
Database base (Study & review for self use)
Preorder traversal of Li Kou 589:n fork tree
SCM learning notes: interrupt learning
Unity/ue reads OPC UA and OPC Da data (UE4)
Unit screenshot saved on the phone
Read and save txt files
Modbus protocol register
Unity enables simple music visualization
Unity project hosting platform plasticscm (learn to use 1)
UnityEngine. JsonUtility. The pit of fromjason()
Parkour demo
Unity + hololens2 performance test
HTC vive cosmos development - handle button event
MinGW-w64下载文件失败the file has been downloaded incorrectly!