[英]Invalid class property declaration for json mapping
I have below json
received from mailgun API
. 我下面是从
mailgun API
接收的json
。
{
"items": [{
"delivery-status": {
"message": null,
"code": 605,
"description": "Not delivering to previously bounced address",
"session-seconds": 0
},
"event": "failed",
"log-level": "error",
"recipient": "test@test.com"
},
{
//some other properties of above types
}]
}
Now I was trying to create a class structure for above json
to auto-map the properties after deserializing
. 现在,我试图为上述
json
创建一个类结构,以在deserializing
后自动映射属性。
public class test
{
public List<Item> items { get; set; }
}
public class Item
{
public string recipient { get; set; }
public string @event { get; set; }
public DeliveryStatus delivery_status { get; set; }
}
public class DeliveryStatus
{
public string description { get; set; }
}
This is how I deserialize
and try to map the properties. 这就是我
deserialize
并尝试映射属性的方式。
var resp = client.Execute(request);
var json = new JavaScriptSerializer();
var content = json.Deserialize<Dictionary<string, object>>(resp.Content);
test testContent = (test)json.Deserialize(resp.Content, typeof(test));
var eventType = testContent.items[0].@event;
var desc = testContent.items[0].delivery_status.description; //stays null
Now in the above class Item
, recipient
and @event
gets mapped properly and since it was a keyword
I was suppose to use preceding @
character and it works well. 现在在上面的类
Item
, recipient
和@event
得到了正确的映射,由于这是一个keyword
我想使用前面的@
字符,并且效果很好。 But the delivery-status
property from json
, does not get mapped with delevery_status
property in class DeliveryStatus
. 但是
json
的delivery-status
属性未与delevery_status
class DeliveryStatus
delevery_status
属性映射。 I have tried creating it as deliveryStatus
or @deliver-status
. 我尝试将其创建为
deliveryStatus
或@deliver-status
。 The earlier on doesn't map again and the later one throws compile time exception. 较早的版本不会再次映射,而较晚的版本会抛出编译时异常。 Is there anyway these things can be handled, like declaring a property with
-
in between? 无论如何,这些事情是否可以处理,例如在
-
之间声明属性? I cannot change response json
as it is not getting generated from my end. 我无法更改
response json
因为它不是从末端生成的。 Hoping for some help. 希望能有所帮助。
Update 更新
Changed the class as below referring this answer
, but did not help. 参照
this answer
将类更改为以下内容,但没有帮助。 Its null
again. 它再次为
null
。
public class Item
{
public string @event { get; set; }
[JsonProperty(PropertyName = "delivery-status")]
public DeliveryStatus deliveryStatus { get; set; }
}
I am not sure what the issue is at your end, but at least it works if you use this code. 我不确定问题出在哪里,但是如果您使用此代码,至少起了作用。 Make sure to include a recent version of Newtonsoft.Json in your project and you should be fine.
确保在您的项目中包括Newtonsoft.Json的最新版本,并且应该没问题。
public class DeliveryStatus
{
public object message { get; set; }
public int code { get; set; }
public string description { get; set; }
[JsonProperty("session-seconds")]
public int session_seconds { get; set; }
}
public class Item
{
[JsonProperty("delivery-status")]
public DeliveryStatus delivery_status { get; set; }
public string @event { get; set; }
[JsonProperty("log-level")]
public string log_level { get; set; }
public string recipient { get; set; }
}
public class RootObject
{
public List<Item> items { get; set; }
}
public static void Main(string[] args)
{
string json = @"{
""items"": [{
""delivery-status"": {
""message"": null,
""code"": 605,
""description"": ""Not delivering to previously bounced address"",
""session-seconds"": 0
},
""event"": ""failed"",
""log-level"": ""error"",
""recipient"": ""test@test.com""
}]
}";
RootObject r = JsonConvert.DeserializeObject<RootObject>(json);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.