当前位置:网站首页>C # basic 6-file IO and JSON
C # basic 6-file IO and JSON
2022-07-28 20:55:00 【W.C.Zeng】
Read and write files asynchronously
Next to the previous chapter , Learn simple file reading and writing from the official website example
public static string readData;
public static async Task SimpleWriteAsync(string text)
{
string filePath = "simple.txt";
await File.WriteAllTextAsync(filePath, text);
}
public static async Task SimpleReadAsync()
{
string filePath = "simple.txt";
readData = await File.ReadAllTextAsync(filePath);
}
JSON Serialization and deserialization
Need to use System.Text.Json
JsonSerializer Class
Import Json package
Using the command line dotnet add package System.Text.Json --version 7.0.0-preview.5.22301.12
quote
using System.Text.Json;
using System.Text.Json.Serialization;
Write a player class
class Player
{
// Must be public And there are getter Can be JsonSerializer serialize
public string name {
get; set; }
public int age {
get; set; }
public int money {
get; }
public void log(string s)
{
Console.WriteLine(s + " " + this.name + " " + this.age + " " + this.money);
}
}
Copy the example of the official website
static async Task Main()
{
// serialize Turn players into JSON
Player p = new Player();
p.name = " Xiao Ming ";
p.age = 24;
p.log("p:");
string data = JsonSerializer.Serialize(p);
// Write files asynchronously
await SimpleWriteAsync(data);
Console.WriteLine(" Write data ");
// Asynchronous read file
await SimpleReadAsync();
Console.WriteLine(" Read the data " + readData);
// Deserialization Convert string to player object
Player p2 = JsonSerializer.Deserialize<Player>(readData);
p2.log("p2:");
}
Print the results
p: Xiao Ming 24 0
Write data
Read the data {"name":"\u5C0F\u660E","age":24,"money":0}
p2: Xiao Ming 24 0
边栏推荐
- Algorithm interview high frequency problem solving guide [1]
- 不懂就问,快速成为容器服务进阶玩家!
- 7/27 训练日志(位运算+后缀数组)
- Ask if you don't understand, and quickly become an advanced player of container service!
- Unity performance optimization
- C# 委托 delegate 的理解
- Easynlp Chinese text and image generation model takes you to become an artist in seconds
- 【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例
- 全链路灰度在数据库上我们是怎么做的?
- Cartoon JS shooting game source code
猜你喜欢

Talking about canvas and three rendering modes in unity

Unity performance optimization

Learn about the native application management platform of rainbow cloud

Confusing knowledge points of software designer examination

The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution

《软件设计师考试》易混淆知识点

Three deletion strategies and eviction algorithm of redis

Unity package exe to read and write excel table files

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

Space shooting Lesson 10: score (painting and writing)
随机推荐
微信小程序的分包加载
Teach you unity scene switching progress bar production hand in hand
js可拖拽alert弹窗插件
C foundation 2-encapsulation, inheritance, polymorphism
企业如何成功完成云迁移?
Space shooting Lesson 16: props (Part 2)
User and group and authority management
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
Unity performance optimization scheme arrangement
How can enterprises successfully complete cloud migration?
Leetcode:2141. The longest time to run n computers at the same time [the maximum value is two points]
js图表散点图例子
7/27 training log (bit operation + suffix array)
Network layer performance test
Random talk on GIS data (VI) - projection coordinate system
Explain several mobile ways of unity in detail
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
How to use redis to realize things and locks?
Talking about canvas and three rendering modes in unity
Record an error in runtime. Getruntime().Exec ("command")