当前位置:网站首页>The serialization class in unity is in JSON format
The serialization class in unity is in JSON format
2022-07-26 13:25:00 【wodownload2】
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class JsonTest : MonoBehaviour
{
//[Serializable]
public class Person
{
[SerializeField]
public string name;
public int age;
}
[Serializable]
public class Student : Person
{
public string code;
}
[Serializable]
public class SkillData
{
public List<Person> persons;
public List<Student> students;
}
void Start()
{
//Write();
Read();
}
private static void Read()
{
string json = File.ReadAllText("a.json");
SkillData skillData = new SkillData();
skillData = JsonUtility.FromJson<SkillData>(json);
for (int i = 0; skillData.persons != null && i < skillData.persons.Count; ++i)
{
Debug.Log(skillData.persons[i].name + " " + skillData.persons[i].age);
}
for (int i = 0; skillData.students != null && i < skillData.students.Count; ++i)
{
Debug.Log(skillData.students[i].name + " " + skillData.students[i].age + " " + skillData.students[i].code);
}
}
private static void Write()
{
Person person = new Person(); person.name = "abc"; person.age = 10;
Student student = new Student(); student.name = "abc"; student.age = 123; student.code = "java";
Student student2 = new Student(); student2.name = "abc2"; student2.age = 1234; student2.code = "C#";
SkillData skillData = new SkillData();
skillData.persons = new List<Person>();
skillData.students = new List<Student>();
skillData.persons.Add(person);
skillData.students.Add(student);
skillData.students.Add(student2);
string json = JsonUtility.ToJson(skillData);
List<string> list = new List<string>();
list.Add(json);
File.WriteAllLines("a.json", list);
}
}
json Serialization , Use unity Their own JsonUtility that will do .
Note the serialized class , To add :Serializable Can be serialized to json In the middle :
边栏推荐
- Kubernetes Flannel:HOST-GW模式
- JSON数据传递参数&日期型参数传递
- El table implements editable table
- Photoshop (cc2020) unfinished
- Click El dropdown item/@click.native
- Mysql数据目录(1)---数据库结构(二十四)
- Activity. Onstop() delay 10 seconds? Wonderful investigation process
- Oom caused by improper use of multithreading
- 冒泡排序的时间复杂度分析
- Why do you want to make "secret comments"?
猜你喜欢

1312_适用7z命令进行压缩与解压

【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯

Chat system based on webrtc and websocket

JSON数据传递参数&日期型参数传递
![[5gc] what is 5g slice? How does 5g slice work?](/img/8c/52ba57d6a18133e97fa00b6a7cf8bc.png)
[5gc] what is 5g slice? How does 5g slice work?

Activity.onStop() 延迟10秒?精彩绝伦的排查历程

Unicode file parsing methods and existing problems

同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

One stroke problem (Chinese postman problem)
随机推荐
One stroke problem (Chinese postman problem)
MVVM architecture encapsulation of kotlin series (kotlin+mvvm)
Emotion analysis model based on Bert
基于C#实现的学生考试系统
【上位机教程】CANopen通信下一体化步进电机与台达PLC(AS228T)的应用
Codeforces Round #810 (Div. 2)【比赛记录】
一笔画问题(中国邮递员问题)
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
B+树(3)聚簇索引,二级索引 --mysql从入门到精通(十五)
Leetcode 217. there are duplicate elements
Unity中序列化类为json格式
Kubernetes flannel: host-gw mode
基于Locust框架进行文件上传下载性能测试
JUC总结
[5gc] what is 5g slice? How does 5g slice work?
JSON format execution plan (6) - MySQL execution plan (52)
父组件访问子组件的方法或参数 (子组件暴漏出方法defineExpose)
LeetCode 217. 存在重复元素
深度学习3D人体姿态估计国内外研究现状及痛点
MySQL data directory (2) -- table data structure (XXV)