【发布时间】:2023-03-04 19:57:01
【问题描述】:
我有一个类SubscriptionType,它的属性是一个复杂的类,称为InfoText、LongInfoText 和Name,它们都有en 和ur 属性。
当我使用 JsonApiNet 3.0.0 库从 wep api 反序列化我的 json 时。检索所有对象,除了属性中存在的复杂类型。显示空值。
我曾尝试使用我自己的解析器,但它不起作用。有人可以帮助我吗?谢谢
我的解析器
public class NamingThingsIsHardResolver : JsonApiPropertyResolver
{
public override PropertyInfo ResolveJsonApiAttribute(Type type, string attributeName)
{
if (type == typeof(SubscriptionType) && attributeName == "name")
{
return type.GetProperty("Name");
}
return base.ResolveJsonApiAttribute(type, attributeName);
}
}
我的类型
public class Subscription
{
public string Id { get; set; }
public string ContractId { get; set; }
public string PaymentType { get; set; }
public string Status { get; set; }
public SubscriptionType SubscriptionType { get; set; }
}
public class SubscriptionType
{
public string Id { get; set; }
public string BusinessModelType { get; set; }
public InfoText InfoText;
public string LineType { get; set; }
public LongInfoText LongInfoText;
//[JsonApiRelationship("name")] // [JsonApiAttribute("name")] //also not working
public string Name;
}
我的复杂类型
public class Name
{
public string En { get; set; }
public string Ur { get; set; }
}
public class InfoText
{
public string En { get; set; }
public string Ur { get; set; }
}
public class LongInfoText
{
public string En { get; set; }
public string Ur { get; set; }
}
我的 json 这个json被简化了。删除了一些属性和关系
{
"data": [
{
"attributes": {
"contract-id": "103",
"payment-type": "prepaid",
"status": "active",
},
"id": "103",
"links": {
"self": "/api/v1/subscriptions/103"
},
"relationships": {
"subscription-type": {
"data": {
"id": "1",
"type": "subscription-types"
},
"links": {
"related": "/api/v1/subscriptions/103/subscription-type"
}
}
},
"type": "subscriptions"
}
],
"included": [
{
"attributes": {
"business-model-type": "prepaid",
"info-text": {
"en": null,
"ur": null
},
"line-type": "mobile",
"long-info-text": {
"en": null,
"ur": null
},
"name": {
"en": "Start prepaid",
"ur": "Start Prepaid"
}
},
"id": "1",
"links": {
"self": "/api/v1/subscription-types/1"
},
"relationships": {
"billing-rate-plans": {
"links": {
"related": "/api/v1/subscription-types/1/billing-rate-plans"
}
}
},
"type": "subscription-types"
}
]
}
【问题讨论】:
标签: c# json deserialization