gpt4 book ai didi

c# - JSONConvert.DeserializeObject 不处理具有未命名数组项的子数组

转载 作者:太空宇宙 更新时间:2023-11-03 21:38:46 25 4
gpt4 key购买 nike

我有以下来自 Web 服务的 JSON 对象

{
"room":{
"name":"Thunderdome",
"created_at":"2012/04/15 00:36:27 +0000",
"id":xxxxxxx,
"users":[
{
"type":"Member",
"avatar_url":"url",
"created_at":"2012/02/27 14:11:57 +0000",
"id":1139474,
"email_address":"xxx@xxxxxxx.com",
"admin":false,
"name":"xxxx xxxxx"
},
{
"type":"Member",

等等

我正在使用以下行进行反序列化:

var room = JsonConvert.DeserializeObject<SingleRoom>(text);

以及下面的映射类

public class SingleRoom
{
public Room Room { get; set; }
}

[DataContract]
public class Room
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }
public string Image {
get { return "Assets/campfire.png"; }
}
[DataMember]
public string Topic { get; set; }
[DataMember(Name ="membership_limit")]
public int MembershipLimit { get; set; }
[DataMember]
public bool Full { get; set; }
[DataMember]
public bool Locked { get; set; }
[DataMember(Name = "open_to_guests")]
public bool OpenToGuests { get; set; }
[DataMember(Name = "updated_at")]
public DateTime UpdatedAt { get; set; }
[DataMember(Name = "created_at")]
public DateTime CreatedAt { get; set; }

[DataMember(Name = "active_token_value")]
public string ActiveTokenValue { get; set; }

[DataMember(Name = "Users")]
public List<User> Users { get; set; }
}

[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string Name { get; set; }

[DataMember(Name = "email_address")]
public string EmailAddress { get; set; }
[DataMember]
public bool Admin { get; set; }
[DataMember]
public DateTime CreatedAt { get; set; }
[DataMember]
public string Type { get; set; }

[DataMember(Name = "avatar_url")]
public string AvatarUrl { get; set; }
}

“Room”对象反序列化正确,但 Room 上的 Users 属性始终为 null。我知道 null 结果是 JSON.NET 序列化找不到匹配属性的结果。但是,我认为我做对了吗?列表应与用户匹配。我知道 JSON 中的数组对象用户没有命名的 child ,这是问题所在吗?如果是,我该如何解决?

谢谢!

最佳答案

这有效....您可以重命名它们(或使用 JsonProperty 属性)以使用 c# 样式属性名称

(顺便说一句:Json.Net 不需要 DataMemberDataContract 属性)

var obj = JsonConvert.DeserializeObject<SingleRoom>(json)

public class User
{
public string type { get; set; }
public string avatar_url { get; set; }
public string email_address { get; set; }
public bool admin { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}

public class Room
{
public string topic { get; set; }
public int membership_limit { get; set; }
public bool locked { get; set; }
public string name { get; set; }
public List<User> users { get; set; }
public bool full { get; set; }
public bool open_to_guests { get; set; }
public string updated_at { get; set; }
public string created_at { get; set; }
public int id { get; set; }
}

public class SingleRoom
{
public Room room { get; set; }
}

PS:您可能会发现该网站很有用 http://json2csharp.com/ .

关于c# - JSONConvert.DeserializeObject 不处理具有未命名数组项的子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20499701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com