简体   繁体   English

用于Json到C#映射的Restsharp类成员注释

[英]Restsharp Classmember Annotations for Json to C# mapping

I got this json: 我得到了这个json:

 "history": {
        "-Kr6ZcI0_SSeO0tRYGRe": {
            "created_at": 1502291854572,
            "id": "1307a006-d8a0-2f72-30bf-3a04410b8d1a",
            "last_status": "",
            "method_create": "createFlowjob",
            "new_status": "Queued"
        },
        "-Kr6Zci2Ts3ncYnfP2Cq": {
            "created_at": 1502291856297,
            "gateway_id": 127,
            "id": "1307a006-d8a0-2f72-30bf-3a04410b8d1a",
            "last_status": "Queued",
            "method_create": "HTTP_POST - /flowjob/receive",
            "new_status": "Pending"
        },
        "-Kr6Zd11nYnMu2uLK7-1": {
            "created_at": 1502291857577,
            "gateway_id": 127,
            "id": "1307a006-d8a0-2f72-30bf-3a04410b8d1a",
            "last_status": "Pending",
            "method_create": "HTTP_POST - /gateway/processor",
            "new_status": "Running"
        },
        "-Kr6ZlV7eD16nLI4FuGe": {
            "created_at": 1502291892274,
            "gateway_id": 127,
            "id": "1307a006-d8a0-2f72-30bf-3a04410b8d1a",
            "last_status": "Running",
            "method_create": "HTTP_DELETE - /gateway/processor/:job_id",
            "new_status": "Completed"
        }
    }

The thing is i wanted to map this to ac# class like this 事情是我想这样映射到ac#类

public class History
    {
        [JsonProperty("somerandomnumberidontknowbefore")]
        [SerializeAs(Name = "somerandomnumberidontknowbefore")]
        [DeserializeAs(Name = "somerandomnumberidontknowbefore")]
        public Dictionary<string,HistoryItem> Items{ get; set; }           
    }

public class HistoryItem
    {

        [JsonProperty("created_at")]
        [SerializeAs(Name = "created_at")]
        [DeserializeAs(Name = "created_at")]
        public int CreatedAt { get; set; }

        [JsonProperty("id")]
        [SerializeAs(Name = "id")]
        [DeserializeAs(Name = "id")]
        public string Id { get; set; }

        [JsonProperty("last_status")]
        [SerializeAs(Name = "last_status")]
        [DeserializeAs(Name = "last_status")]
        public string  LastSTatus { get; set; }

        [JsonProperty("method_create")]
        [SerializeAs(Name = "method_create")]
        [DeserializeAs(Name = "method_create")]
        public string MethodCreate { get; set; }

        [JsonProperty("new_status")]
        [SerializeAs(Name = "new_status")]
        [DeserializeAs(Name = "new_status")]
        public string NewStatus { get; set; }
    }

My Dictionary never gets filled and i don't know how to map this json to the class properly. 我的字典永远不会被填充,我也不知道如何正确地将此json映射到该类。 Json to c# converter gave me very weird results which didn't make sense. Json到C#转换器给我非常奇怪的结果,这没有任何意义。

Does anybody here know how to achieve a mapping of aa json key that's always random and not know before runtime? 这里有人知道如何实现始终为随机且在运行时不知道的json密钥的映射吗?

I don't know about restsharp, but with NewtonSoft you can do using a dynamic such as the following; 我不了解restsharp,但是通过NewtonSoft,您可以使用如下所示的动态方式:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

dynamic result = JsonConvert.DeserializeObject<dynamic>(json);

foreach (JProperty x in (JToken)result)
{                
    Console.WriteLine("{0}:{1}", x.Name, x.Value.ToString());            
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM