赞
踩
{ “name”: “Player”, “health”: 100, “position”: {
“x”: 10,
“y”: 5,
“z”: 0 }, “inventory”: [“sword”, “potion”, “shield”] }
string json = "{\"name\":\"santy\",\"age\":27}"; // Deserialize the JSON string into a C# object var person = JsonConvert.DeserializeObject<Person>(json); // Now you can access the properties of the deserialized object string name = person.Name; int age = person.Age; // Use the retrieved data as needed Debug.Log("Name: " + name); Debug.Log("Age: " + age); // Define a class to represent the structure of the JSON data public class Person { public string Name { get; set; } public int Age { get; set; } }
var data = new { name = "santy", age = 27};
string json = JsonConvert.SerializeObject(data);
Dictionary<string, object> data = new Dictionary<string, object>();
data["name"] = "santy";
data["age"] = 27;
string json = JsonConvert.SerializeObject(data);
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Person person = new Person { Name = "santy", Age = 27};
string json = JsonConvert.SerializeObject(person);
[Serializable]
public class Person
{
public string Name;
public int Age;
}
Person person = new Person { Name = "santy", Age = 27};
string json = JsonUtility.ToJson(person);
JObject data = new JObject(); data["name"] = "Santy"; data["age"] = 27; data["config"] = new JObject { { "phoneNum", 12245 }, { "isDeveloper", true }, }; string json = data.ToString(); // Parse the JSON string into a JObject JObject player = JObject.Parse(json); // Access the properties of the JObject string name = (string)player["name"]; int phoneNum = (int)player["config"]["phoneNum"]; Debug.Log("json name =" + name + " phoneNum=" + phoneNum);
using UnityEngine; public class PlayerData { public string name; public int health; public Vector3 position; public string[] inventory; } public class JSONExample : MonoBehaviour { void Start() { // Deserializing JSON string to C# object string json = "{\"name\":\"Player\",\"health\":100,\"position\":{\"x\":10,\"y\":5,\"z\":0},\"inventory\":[\"sword\",\"potion\",\"shield\"]}"; PlayerData player = JsonUtility.FromJson<PlayerData>(json); // Accessing values Debug.Log("Player Name: " + player.name); Debug.Log("Player Health: " + player.health); // Modifying values player.health -= 10; // Serializing C# object to JSON string string updatedJson = JsonUtility.ToJson(player); Debug.Log("Updated JSON: " + updatedJson); } }
using Newtonsoft.Json; public class PlayerData { public string playerName; public int playerScore; } public class Example : MonoBehaviour { void Start() { PlayerData player = new PlayerData(); player.playerName = "Player1"; player.playerScore = 100; // Serializing C# object to JSON string string json = JsonConvert.SerializeObject(player); Debug.Log(json); // Deserializing JSON string to C# object string json = "{\"playerName\":\"Player1\",\"playerScore\":100}"; PlayerData player = JsonConvert.DeserializeObject<PlayerData>(json); Debug.Log("Player Name: " + player.playerName); Debug.Log("Player Score: " + player.playerScore); } }
// level1.json { “name”: “Level 1”, “background”:
“level1_background.png”, “enemies”: [
{ “type”: “zombie”, “position”: { “x”: 5, “y”: 2 } },
{ “type”: “skeleton”, “position”: { “x”: -3, “y”: 0 } } ] }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。