复杂的Json响应未在c#类中正确映射

时间:2018-07-25 17:35:36

标签: c# .net json

我有这个JSON响应,并且创建了一个等效的C#类。 但是当我尝试将此JSON转换为c#结果未完成时。

JSON响应

{
    "result": "success",
    "totalresults": 1,
    "startnumber": 0,
    "numreturned": 1,
    "orders": {
        "order": [
            {
                "id": 26,
                "ordernum": 9658502986,
                "userid": 18,
                "frauddata": "",
                "lineitems": {
                    "lineitem": [
                        {
                            "type": "domain",
                            "relid": 9,
                            "dnsmanagement": 0,
                            "emailforwarding": 0,
                            "idprotection": 0
                        }
                    ]
                }
            }
        ]
    }
}

C#类

public class RootObject
    {
        int totalresults { get; set; }
        int startnumber { get; set; }
        int numreturned { get; set; }
        orders orders { get; set; }
    }
    public class lineitem
    {
        public string type { get; set; }
        public int idprotection { get; set; }
    }

    public class lineitems
    {
        public List<lineitem> lineitem { get; set; }
    }

    public class order
    {
        public int id { get; set; }
        public long ordernum { get; set; }
        public int userid { get; set; }
        public string fraudmodule { get; set; }
        public string fraudoutput { get; set; }
        public string frauddata { get; set; }
        public lineitems lineitems { get; set; }
    }

    public class orders
    {
        public List<order> order { get; set; }
    }

我使用了 JsonConvert.DeserializeObject ,但结果订单属性始终为空!

enter image description here

我需要订购财产。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您应该尝试这样;

JsonConvert.DeserializeObject<RootObject>(yourJsonString)