Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将类名作为动态值的json反序列化_C#_Json - Fatal编程技术网

C# 如何将类名作为动态值的json反序列化

C# 如何将类名作为动态值的json反序列化,c#,json,C#,Json,如何反序列化以下json { "data": { "11396": { "description": "Timer project", "status": "ACTIVE", "customer": { "locations": {}, "id": 96626 }, "tasks": [

如何反序列化以下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。因此,在将此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; }
}
公共类位置
{
}
公共类客户
{
公共位置位置{get;set;}
公共int id{get;set;}
}
公开课任务
{
公共字符串说明{get;set;}
公共字符串单元{get;set;}
公共双vatPct{get;set;}
公共双单价{get;set;}
公共布尔可计费{get;set;}
公共int id{get;set;}
}
公共类\无效\类型\ 11397
{
公共字符串说明{get;set;}
公共字符串状态{get;set;}
公共客户客户{get;set;}
公共列表任务{get;set;}
公共双价{get;set;}
公共字符串定价{get;set;}
公共bool allowProducts{get;set;}
公共bool hasUninvoicedItems{get;set;}
公共int id{get;set;}
}
公共舱位置2
{
}
公共类客户2
{
公共位置2位置{get;set;}
公共int id{get;set;}
}
公共课任务2
{
公共字符串说明{get;set;}
公共字符串单元{get;set;}
公共双vatPct{get;set;}
公共双单价{get;set;}
公共布尔可计费{get;set;}
公共int id{get;set;}
}
公共类\无效\类型\ 11396
{
公共字符串说明{get;set;}
公共字符串状态{get;set;}
公共客户2客户{get;set;}
公共列表任务{get;set;}
公共双价{get;set;}
公共字符串定价{get;set;}
公共bool allowProducts{get;set;}
公共bool hasUninvoicedItems{get;set;}
公共int id{get;set;}
}
公共类数据
{
公共uuu无效uu类型uuu11397 uuuu无效uuu11397{get;set;}
公共uuu无效uu类型uuu11396 uuuu无效uuu11396{get;set;}
}
公共类根对象
{
公共数据数据{get;set;}
公共bool ok{get;set;}
}

非常感谢您的帮助。

我通过将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);
            }

        }
JToken-token=JObject.Parse(响应);
var justDaily=token[“数据”];
ProjectList=新列表();
foreach(justDaily.Children()中的JToken child)
{
foreach(JToken孙子女)
{
Project temp=JsonConvert.DeserializeObject(孙子.ToString().Replace(“\r\n”和“”);
项目列表。添加(临时);
}
}
希望这对其他人也有帮助

希望这对你有帮助。。