Asp.net JSON反序列化:内部有多个对象

Asp.net JSON反序列化:内部有多个对象,asp.net,json,Asp.net,Json,我有示例代码,工作正常 public class Employee { public int ID { get; set; } public string Name { get; set; } public string Address { get; set; } } private void JSONDeserilaize() { string json = @"{ 'ID': '1',

我有示例代码,工作正常

public class Employee  
{  
    public int ID { get; set; }  
    public string Name { get; set; }  
    public string Address { get; set; }  
}  


   private void JSONDeserilaize()  
{  
    string json = @"{  
        'ID': '1',  
        'Name': 'Manas',  
        'Address': 'India'  
    }";  

    Employee empObj = JsonConvert.DeserializeObject<Employee>(json);  

    Response.Write(empObj.Name);  
}  

如何获取EmpDeptId以及Id、名称和地址。

为要反序列化的对象声明另一个类,然后将其添加为原始类的成员:

public class Employee  
{  
    public int ID { get; set; }  
    public string Name { get; set; }  
    public string Address { get; set; } 
    public EmployeeData Data { get; set; } 
}

public class EmployeeData 
{
    public string EmpDeptId {get; set; }
}

然后,它应该相应地反序列化为数据。

如果需要Employee.EmpDeptId,则添加行公共字符串EmpDeptId{get{return this.data.EmpDeptId;set{this.data.emptedeptid=value;}}给雇员class@IamSilviu为什么要这样做呢?它已经是数据的一个属性,您只需在对象图中重复自己,它也可以序列化回JSON。OP只需执行employee.Data.emptedeptid即可获得值。
public class Employee  
{  
    public int ID { get; set; }  
    public string Name { get; set; }  
    public string Address { get; set; } 
    public EmployeeData Data { get; set; } 
}

public class EmployeeData 
{
    public string EmpDeptId {get; set; }
}