关于Newtonsoft Json中的集合数据转换
1.嵌套属性的类序列化时会报错.
如:
class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public string Remark { get; set; } public List<Person> Group { get; set; } } | PersonA中有一个List<Person>属性,这将导致序列化时,如果这个属性中有对象自身,那么这个树就会无限循环,所以抛异常. |
- 还是以上边的示例,在用JsonConvert.SerializeObject()方法时,如果序列化Person.Group这样的集合属性,其Json文本是如下这样的:
{ "Name": "Park", "Age": 52, "Gender": "male", "Group": [ { "Name": "Jack", "Age": 32, "Gender": "male" }, { "Name": "Lily", "Age": 23, "Gender": "female" }, { "Name": "Ted", "Age": 13, "Gender": "male", "Remark": "his is child" } ] } |
但如果序列化的是一个List<Person>集合,其文本是如下这样的: |
[{"Name":"Jack","Age":32,"Gender":"male","Remark":null},{"Name":"Lily","Age":23,"Gender":"female","Remark":null},{"Name":"Ted","Age":13,"Gender":"male","Remark":"his is child"}] |
而我们持续向某一个Json文本中添加Serialized后的JsonText,结果是向下边这样的:
{ "Name": "Jack", "Age": 32, "Gender": "male" } { "Name": "Ted", "Age": 13, "Gender": "male", "Remark": "his is child" } { "Name": "Park", "Age": 52, "Gender": "male" }
// 注意,这种文件不符合Json文档格式,在Deserialize时会抛出异常: Newtonsoft.Json.JsonReaderException:"Additional text encountered after finished reading JSON content: {. Path '', line 5, position 1."
|
- 使用JsonConvert.DeserializeObject()方法时,非泛型方法然后再通过as TargetType的方法是不管用的,返回的还是一个null.
Person result1 = JsonConvert.DeserializeObject(jackJson) as Person; |
附录:全部测试代码放在了下边
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
namespace TestConsoleApp { class Program { static void Main(string[] args) { var jack = new Person() { Name = "Jack", Age = 32, Gender = "male" }; var lily = new Person() { Name = "Lily", Age = 23, Gender = "female" }; var park = new Person() { Name = "Park", Age = 52, Gender = "male" }; var ted = new Person() { Name = "Ted", Age = 13, Gender = "male", Remark = "his is child" }; List<Person> group1 = new List<Person>() { jack, lily, ted }; //park.Group = group1; //jack.Group = group1; //lily.Group = group1; //ted.Group = group1;
JsonSerializerSettings jsSetting = new JsonSerializerSettings(); jsSetting.NullValueHandling = NullValueHandling.Ignore; string jackJson = JsonConvert.SerializeObject(jack, Formatting.Indented, jsSetting); string tedJson = JsonConvert.SerializeObject(ted, Formatting.Indented, jsSetting); string groupJson = JsonConvert.SerializeObject(group1); string parkJson = JsonConvert.SerializeObject(park, Formatting.Indented, jsSetting); Console.WriteLine(jackJson); Console.WriteLine(tedJson); Console.WriteLine(parkJson); string jsonTextColl = jackJson + tedJson + parkJson; Person result1 = JsonConvert.DeserializeObject(jackJson) as Person; Person result2 = JsonConvert.DeserializeObject<Person>(jackJson); List<Person> result3 = JsonConvert.DeserializeObject<List<Person>>(groupJson); List<Person> result4 = JsonConvert.DeserializeObject(groupJson) as List<Person>; List<Person> result5 = JsonConvert.DeserializeObject(jsonTextColl) as List<Person>; Console.ReadKey(); } }
class Person { public string Name { get; set; } public int Age { get; set; } public string Gender { get; set; } public string Remark { get; set; } //public List<Person> Group { get; set; } } } |