41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using Newtonsoft.Json;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class JsonTest : MonoBehaviour
|
|
{
|
|
|
|
public class TestPerson
|
|
{
|
|
public string Name { get; set; }
|
|
public int Age { get; set; }
|
|
public string Email { get; set; }
|
|
}
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
// 创建一个对象
|
|
TestPerson person = new TestPerson
|
|
{
|
|
Name = "John Doe",
|
|
Age = 30,
|
|
Email = "john.doe@example.com"
|
|
};
|
|
|
|
// 序列化对象为JSON字符串
|
|
string jsonString = JsonConvert.SerializeObject(person, Formatting.Indented);
|
|
Debug.Log("Serialized JSON string:");
|
|
Debug.Log(jsonString);
|
|
|
|
// 反序列化JSON字符串为对象
|
|
TestPerson deserializedPerson = JsonConvert.DeserializeObject<TestPerson>(jsonString);
|
|
Debug.Log("Deserialized Person object:");
|
|
Debug.Log($"Name: {deserializedPerson.Name}");
|
|
Debug.Log($"Age: {deserializedPerson.Age}");
|
|
Debug.Log($"Email: {deserializedPerson.Email}");
|
|
}
|
|
|
|
|
|
}
|