如何使用类名将json反序列化为动态值

时间:2013-12-11 07:02:41

标签: c# json

如何对json进行反序列化?

{
    "data": {
        "11396": {
            "description": "Timer project",
            "status": "ACTIVE",
            "customer": {
                "locations": {},
                "id": 96626
            },
            "tasks": [
                {
                    "description": "Timer Task",
                    "unit": "h",
                    "vatPct": 0.2,
                    "unitPrice": 12,
                    "billable": true,
                    "id": 19660
                }
            ],
            "price": 0,
            "pricing": "UNIT",
            "allowProducts": true,
            "hasUninvoicedItems": false,
            "id": 11396
        },
        "11397": {
            "description": "Timer Project 2",
            "status": "ACTIVE",
            "customer": {
                "locations": {},
                "id": 96626
            },
            "tasks": [
                {
                    "description": "Timer Task2",
                    "unit": "h",
                    "vatPct": 0.05,
                    "unitPrice": 20,
                    "billable": true,
                    "id": 19655
                }
            ],
            "price": 0,
            "pricing": "UNIT",
            "allowProducts": true,
            "hasUninvoicedItems": false,
            "id": 11397
        }
    },
    "ok": true
}

问题是值11396,11397作为类名(如果转换为c#),它们实际上是该特定记录的ID。所以当使用http://json2csharp.com将此json转换为c#时。它显示为

public class Locations
{
}

public class Customer
{
    public Locations locations { get; set; }
    public int id { get; set; }
}

public class Task
{
    public string description { get; set; }
    public string unit { get; set; }
    public double vatPct { get; set; }
    public double unitPrice { get; set; }
    public bool billable { get; set; }
    public int id { get; set; }
}

public class __invalid_type__11397
{
    public string description { get; set; }
    public string status { get; set; }
    public Customer customer { get; set; }
    public List<Task> tasks { get; set; }
    public double price { get; set; }
    public string pricing { get; set; }
    public bool allowProducts { get; set; }
    public bool hasUninvoicedItems { get; set; }
    public int id { get; set; }
}

public class Locations2
{
}

public class Customer2
{
    public Locations2 locations { get; set; }
    public int id { get; set; }
}

public class Task2
{
    public string description { get; set; }
    public string unit { get; set; }
    public double vatPct { get; set; }
    public double unitPrice { get; set; }
    public bool billable { get; set; }
    public int id { get; set; }
}

public class __invalid_type__11396
{
    public string description { get; set; }
    public string status { get; set; }
    public Customer2 customer { get; set; }
    public List<Task2> tasks { get; set; }
    public double price { get; set; }
    public string pricing { get; set; }
    public bool allowProducts { get; set; }
    public bool hasUninvoicedItems { get; set; }
    public int id { get; set; }
}

public class Data
{
    public __invalid_type__11397 __invalid_name__11397 { get; set; }
    public __invalid_type__11396 __invalid_name__11396 { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public bool ok { get; set; }
}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我通过将json字符串解析为JTOKEN并查询所需数据来解决此问题。 这是可能的,因为我在json中的数据是静态的

JToken token = JObject.Parse(response);
        var justDaily = token["data"];
        ProjectList = new List<Project>();
        foreach (JToken child in justDaily.Children())
        {
            foreach (JToken grandChild in child)
            {
                Project temp = JsonConvert.DeserializeObject<Project>(grandChild.ToString().Replace("\r\n", ""));

                    ProjectList.Add(temp);
            }

        }

希望这也有助于其他人