Unity中使用Newtonsoft.Json-强大的JSON转换工具

Links

官网地址:https://www.newtonsoft.com/json
下载地址:https://github.com/JamesNK/Newtonsoft.Json/releases
源码地址:https://github.com/JamesNK/Newtonsoft.Json

Install

  1. Install with NuGet (recommended) *Unity中需要借助 nuget 插件

在这里插入图片描述

  1. 直接复制 dll 文件

Quick Use

  1. 序列化对象
// 序列化对象
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product);
// {
//   "Name": "Apple",
//   "Expiry": "2008-12-28T00:00:00",
//   "Sizes": [
//     "Small"
//   ]
// }
  1. 反序列化
// 反序列化
string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;
// Bad Boys

Attributes

  1. JsonContainer
    *所有Attribute类的父类,抽象类
  2. JsonRequired
    *标记该特性的属性必须存在且不能为null,否则报错
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'name': null
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(player.name);
    }
    public class Player
    {
        [JsonRequired]
        public string name;
    }
}
// JsonSerializationException: 
// Required property 'name' expects a value but got null. Path '', line 1, position 27.
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'age': 12
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(player.name);
    }
    public class Player
    {
        [JsonRequired]
        public string name;
    }
}

// JsonSerializationException: 
// Required property 'name' not found in JSON. Path '', line 3, position 13.
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'name': 'Steve'
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(player.name);
    }
    public class Player
    {
        [JsonRequired]
        public string name;
    }
}
// Steve
// 序列化时也会报错
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        Player player = new Player();
        print(JsonConvert.SerializeObject(player));
    }
    public class Player
    {
        [JsonRequired]
        public string name;
    }
}
// JsonSerializationException: 
// Cannot write a null value for property 'name'. Property requires a value. Path ''.
  1. JsonProperty
    *标记该特性的属性总是以特定的字段名序列化
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'name': 'Jack',
                'xingming': 'Steve'
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(player.name);
    }
    public class Player
    {
        [JsonProperty("xingming")]
        public string name;
    }
}
// Steve
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        Player player = new Player();
        print(JsonConvert.SerializeObject(player));
    }
    public class Player
    {
        [JsonProperty("xingming")]
        public string name;
    }
}
// {"xingming":null}
  1. JsonIgnore
    *标记该特性的属性将被忽略,不进行序列化和反序列化。
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'name': 'Steve'
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(player.name);
    }
    public class Player
    {
        [JsonIgnore]
        public string name;
    }
}
// Null
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        Player player = new Player();
        player.name = "Steve";
        print(JsonConvert.SerializeObject(player));
    }
    public class Player
    {
        [JsonIgnore]
        public string name;
    }
}
// {}
  1. JsonExtensionData
    *标记该特性的属性存放未匹配到的额外属性,属性必须是字典形式
public class NewtonsoftJsonTest : MonoBehaviour
{
    void Start()
    {
        string json = @"{
                'name': 'Steve'
            }";
        Player player = JsonConvert.DeserializeObject<Player>(json);
        print(JsonConvert.SerializeObject(player.ext));
    }
    public class Player
    {
        [JsonExtensionData]
        public Dictionary<string, object> ext;
    }
}
// {"name":"Steve"}
  1. 其他特性更多用法未测试,详细见官方文档

你可能感兴趣的:(#,插件)