Json反序列化问题c#
本文关键字:问题 反序列化 Json | 更新日期: 2023-09-27 18:26:50
我想尝试反序列化以下Facebook帖子响应:
"data": [
{
"id": "...",
"from": {
"category": "Local business",
"name": "...",
"id": "..."
},
"message": "...",
"picture": "...",
"likes": {
"data": [
{
"id": "...",
"name": "..."
},
{
"id": "...",
"name": "..."
]
}
}
]
后模型类为:
public class Post
{
public string Id { get; set; }
public From From { get; set; }
public string Message { get; set; }
public string Picture { get; set; }
[JsonProperty("likes.data")] <===== why this is not working??
public List<Like> Likes { get; set; }
}
类似型号类
public class Like
{
public string Id { get; set; }
public string Name { get; set; }
}
在反序列化json时,我想将likes.data条目映射到likes列表。我该怎么做??
您可以使用Newtonsoft.Json来反序列化Json。
Newtonsoft.Json.JsonConvert.DeserializeObject("{'"data'": [...]")
您可以使用DataContractJsonSerializer
(System.Runtime.Serialization)来反序列化这个:
使用以下内容反序列化:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
var jsonString = "{'"data'":[{'"id'":'"...'",'"from'":{'"category'":'"Local business'",'"name'":'"...'",'"id'":'"...'"},'"message'":'"...'",'"picture'":'"...'",'"likes'":{'"data'":[{'"id'":'"...'",'"name'":'"...'"},{'"id'":'"...'",'"name'":'"...'"}]}}]}";
var jsonSerializer = new DataContractJsonSerializer(typeof(JsonRoot));
JsonRoot json = null;
using(var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
stream.Position = 0;
json = (JsonRoot)jsonSerializer.ReadObject(stream);
}
使用以下类型:
[DataContract]
public class JsonRoot
{
[DataMember(Name="data")]
public List<Post> Data { get; set; }
}
[DataContract]
public class Post
{
[DataMember(Name="id")]
public string Id { get; set; }
[DataMember(Name="from")]
public From From { get; set; }
[DataMember(Name="message")]
public string Message { get; set; }
[DataMember(Name="picture")]
public string Picture { get; set; }
[DataMember(Name="likes")]
public DataContainer Likes { get; set; }
}
[DataContract]
public class DataContainer
{
[DataMember(Name="data")]
public List<Like> Data { get; set; }
}
[DataContract]
public class From
{
[DataMember(Name="category")]
public string Category { get; set; }
[DataMember(Name="name")]
public string Name { get; set; }
[DataMember(Name="id")]
public string Id { get; set; }
}
[DataContract]
public class Like
{
[DataMember(Name="id")]
public string Id { get; set; }
[DataMember(Name="name")]
public string Name { get; set; }
}
同样的基本结构也应该适用于NewtonsoftJson库,但您需要切换出具有相应Newtonsoft替代项的属性。
或者,感谢:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json; // Repo: https://github.com/ysharplanguage/FastJsonParser
你也可以写:
public class From
{
public string id { get; set; }
public string name { get; set; }
public string category { get; set; }
}
public class Post
{
public string id { get; set; }
public From from { get; set; }
public string message { get; set; }
public string picture { get; set; }
public Dictionary<string, Like[]> likes { get; set; }
}
public class Like
{
public string id { get; set; }
public string name { get; set; }
}
和:
var SO_26426594_input = @"{ ""data"": [
{
""id"": ""post 1"",
""from"": {
""category"": ""Local business"",
""name"": ""..."",
""id"": ""...""
},
""message"": ""..."",
""picture"": ""..."",
""likes"": {
""data"": [
{
""id"": ""like 1"",
""name"": ""text 1...""
},
{
""id"": ""like 2"",
""name"": ""text 2...""
}
]
}
} ] }";
var posts = new JsonParser().Parse<Dictionary<string, Post[]>>(SO_26426594_input);
测试/验证:
System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].id == "post 1");
System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].from.category == "Local business");
System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].likes["data"][0].id == "like 1");
System.Diagnostics.Debug.Assert(posts != null && posts["data"][0].likes["data"][1].id == "like 2");
使用:
System.Text.Json(https://www.nuget.org/packages/System.Text.Json)
'HTH,