gpt4 book ai didi

c# - Json DeserializeObject 返回 null - 我做错了什么?

转载 作者:行者123 更新时间:2023-11-30 21:51:48 24 4
gpt4 key购买 nike

下面是对 DeserializeObject 的调用:

listrootobject obj = JsonConvert.DeserializeObject<listrootobject>(json);

这是杰森。

{
"so:MemberProfileDataSet": {
"@xmlns:moso": "http://schema.socloud.com/1/MemberProfileDataSet.xsd",
"Member": {
"@xmlns": "http://schema.socloud.com/1/MemberProfileDataSet.xsd",
"PartyId": "63",
"PartyTypeName": "Employee",
"RoleId": "1310",
"BusinessUnitCode": "95",
"CardId": null,
"PostalContact": {
"PartyId": "63",
"TypeName": "Mailing Address",
"Address1": "100 Main Ave",
"Address2": null,
"Address3": null,
"City": "Arlington",
"State": "VA",
"PostalCode": "22206"
},
"PhoneContact": [{
"PartyId": "63",
"TypeName": "Cell",
"CountryCode": "1",
"Telephone": "(555) 555-5555",
"AdditionalData": null,
"TextMessageOK": "false"
}, {
"PartyId": "63",
"TypeName": "Home",
"CountryCode": "1",
"Telephone": null,
"AdditionalData": null,
"TextMessageOK": "false"
}],
"Property": [{
"PartyId": "63",
"Name": "First Name",
"Value": "Katie"
}, {
"PartyId": "63",
"Name": "Last Name",
"Value": "Rodriguez"
}, {
"PartyId": "63",
"Name": "Payroll ID",
"Value": null
}, {
"PartyId": "63",
"Name": "Lookup Initials",
"Value": null
}, {
"PartyId": "63",
"Name": "Date of Birth",
"Value": null
}, {
"PartyId": "63",
"Name": "Hire Date",
"Value": null
}, {
"PartyId": "63",
"Name": "Termination Date",
"Value": null
}, {
"PartyId": "63",
"Name": "Gender",
"Value": null
}]
}
}
}

这是C#类

public class PostalContact
{
public string PartyId { get; set; }
public string TypeName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}

public class EmailContact
{
public string PartyId { get; set; }
public string TypeName { get; set; }
public string EmailAddress { get; set; }
public string EmailOptOut { get; set; }
}

public class PhoneContact
{
public string PartyId { get; set; }
public string TypeName { get; set; }
public string CountryCode { get; set; }
public string Telephone { get; set; }
public string AdditionalData { get; set; }
public string TextMessageOK { get; set; }
}

public class Property
{
public string PartyId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}

//public class rootobject
//{
// public List<Member> Members { get; set; }
//}
public class Member
{
public string PartyId { get; set; }
public string PartyTypeName { get; set; }
public string RoleId { get; set; }
public string BusinessUnitCode { get; set; }
public string CardId { get; set; }
public PostalContact PostalContact { get; set; }
public EmailContact EmailContact { get; set; }
public List<PhoneContact> PhoneContact { get; set; }
public List<Property> Property { get; set; }
public string _xmlns { get; set; }
}

public class MemberProfileDataSet
{
public Member Member { get; set; }
public string __invalid_name___xmlns_moso { get; set; }
public string __prefix { get; set; }
}


public class listrootobject
{
public List<MemberProfileDataSet> rootobjects { get; set; }
}
public class rootobject
{
public MemberProfileDataSet MemberProfileDataSet { get; set; }
}

最佳答案

使用http://json2csharp.com/生成和编写您的 C# 类作为从网站生成的类。最后,您按如下方式解析 rootObject。

但是您遇到了一个问题,因为它生成的属性名称在 C# 中无效,您无法使用这些名称创建 C# 类。但是名称必须匹配才能反序列化。因此,您需要删除无效字符或重新命名它们以满足 C# CLR 命名要求。然后创建格式正确的字符串的 C# 类。之后您可以反序列化。

例如,我从原始字符串生成了 C# 对象,并且我有一个属性名称
public SoMemberProfileDataSet __invalid_name__so:MemberProfileDataSet { get;放; }

这在 C# 中是无效的,但您需要在字符串中将其格式化为有效的内容,例如

public SoMemberProfileDataSet MemberProfileDataSet { get;放; }

通过重命名 json 字符串或从字符串中获取所需的属性并在反序列化之前正确地重新构造 json。

显示代码

例如,我在冒号之间组合了字符串并删除了 @ 符号,这样我就创建了一个有效的 C# 对象。它去序列化。请查看下面 json 字符串中的更改。因此,请格式化您的 json 字符串,您将拥有它。

string json = "{\"soMemberProfileDataSet\": {\"xmlnsmoso\": \"http://schema.socloud.com/1/MemberProfileDataSet.xsd\",\"Member\": {\"xmlns\": \"http://schema.socloud.com/1/MemberProfileDataSet.xsd\",\"PartyId\": \"63\",\"PartyTypeName\": \"Employee\",\"RoleId\": \"1310\",\"BusinessUnitCode\": \"95\",\"CardId\": null,\"PostalContact\": {\"PartyId\": \"63\",\"TypeName\": \"Mailing Address\",\"Address1\": \"100 Main Ave\",\"Address2\": null,\"Address3\": null,\"City\": \"Arlington\",\"State\": \"VA\",\"PostalCode\": \"22206\"},\"PhoneContact\": [{\"PartyId\": \"63\",\"TypeName\": \"Cell\",\"CountryCode\": \"1\",\"Telephone\": \"(555) 555-5555\",\"AdditionalData\": null,\"TextMessageOK\": \"false\"}, {\"PartyId\": \"63\",\"TypeName\": \"Home\",\"CountryCode\": \"1\",\"Telephone\": null,\"AdditionalData\": null,\"TextMessageOK\": \"false\"}],\"Property\": [{\"PartyId\": \"63\",\"Name\": \"First Name\",\"Value\": \"Katie\"}, {\"PartyId\": \"63\",\"Name\": \"Last Name\",\"Value\": \"Rodriguez\"}, {\"PartyId\": \"63\",\"Name\": \"Payroll ID\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Lookup Initials\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Date of Birth\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Hire Date\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Termination Date\",\"Value\": null}, {\"PartyId\": \"63\",\"Name\": \"Gender\",\"Value\": null}]}}}";

var listrootobject = JsonConvert.DeserializeObject<RootObject>(json);

你的类现在应该如下所示,格式化后不再有无效的属性名

public class PostalContact
{
public string PartyId { get; set; }
public string TypeName { get; set; }
public string Address1 { get; set; }
public object Address2 { get; set; }
public object Address3 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
}

public class PhoneContact
{
public string PartyId { get; set; }
public string TypeName { get; set; }
public string CountryCode { get; set; }
public string Telephone { get; set; }
public object AdditionalData { get; set; }
public string TextMessageOK { get; set; }
}

public class Property
{
public string PartyId { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}

public class Member
{
public string xmlns { get; set; }
public string PartyId { get; set; }
public string PartyTypeName { get; set; }
public string RoleId { get; set; }
public string BusinessUnitCode { get; set; }
public object CardId { get; set; }
public PostalContact PostalContact { get; set; }
public List<PhoneContact> PhoneContact { get; set; }
public List<Property> Property { get; set; }
}

public class SoMemberProfileDataSet
{
public string xmlnsmoso { get; set; }
public Member Member { get; set; }
}

public class RootObject
{
public SoMemberProfileDataSet soMemberProfileDataSet { get; set; }
}

关于c# - Json DeserializeObject 返回 null - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35214036/

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