C # with Unity Data Storage

Use Json to store data

1. Create a Json file, create a txt file directly, and change the suffix to xx.json

2. The data format of the Json file , The square brackets indicate the list, that is, the content separated by each comma under the square brackets is each element of the list, the curly brackets indicate the object, the front of the colon indicates the attribute name (that is, the identifier), and the back of the colon is the attribute value

share picture

Of course, you can save an object directly without the brackets, that is, json can save the object, or save the list, or in other words, json automatically adjusts the format according to the saved content, that is, if there is a list in the object, it will also be saved in the format of the list

Share pictures

3. Program to operate Json files, Here you need to use the using Newtonsoft.Json; namespace, here use NuGet to add

Share pictures

The code is as follows

using< /span> System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Read objects directly from Json
//string s = File.ReadAllText("TextFile1.json" );
//Per p = JsonConvert.DeserializeObject(s) ;

//Console.Write(p);


Per p2
= new Per("lidong", "< span style="color: #800000;">45
");
//Store objects in Json
string os = JsonConvert.SerializeObject(p2);


List
list = new List();
//list.Add(p);
list.Add(p2);
//store the linked list to Json
//string os = JsonConvert.SerializeObject(list);

//Rewriting will empty the file and fill in the content again
File.WriteAllText("TextFile1.json", os);
Console.Write(os);
Console.ReadKey();
}
}

public class Per
{
public string name {get; set;}
public string age {get; set;}

public Per(string _name,string _age)
{
name
= _name;
age
= _age;
}

public override string ToString()
{
return name + "," + age;
}
}
}

1111111

using

span> System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;

namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//Read objects directly from Json
//string s = File.ReadAllText("TextFile1.json" );
//Per p = JsonConvert.DeserializeObject(s) ;

//Console.Write(p);


Per p2
= new Per("lidong", "< span style="color: #800000;">45
");
//Store objects in Json
string os = JsonConvert.SerializeObject(p2);


List
list = new List();
//list.Add(p);
list.Add(p2);
//store the linked list to Json
//string os = JsonConvert.SerializeObject(list);

//Rewriting will empty the file and fill in the content again
File.WriteAllText("TextFile1.json", os);
Console.Write(os);
Console.ReadKey();
}
}

public class Per
{
public string name {get; set;}
public string age {get; set;}

public Per(string _name,string _age)
{
name
= _name;
age
= _age;
}

public override string ToString()
{
return name + "," + age;
}
}
}

Leave a Comment

Your email address will not be published.