Jui Test
Jui Test

Reputation: 2409

JSON Deserialization : Multiple object inside

I have sample code ,that work fine.

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);  
}  

But my json string is in this format.

string json = @"{"ID": "1","Name": "Manas","Address": "India","data":{"EmpDeptId":"20172807"}}";

How to fetch EmpDeptId along with Id,Name and Address.

Upvotes: 0

Views: 42

Answers (1)

Lloyd
Lloyd

Reputation: 29668

Declare another class for the object to deserialize into, then add it as a member of the original class:

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; }
}

It should then deserialize into data accordingly.

Upvotes: 1

Related Questions