Saturday, 2 March 2019

Generate Class From JSON or XML in Visual Studio

JSON

{"employees":[
   {"firstName":"John", "lastName":"Doe"},
   {"firstName":"Anna", "lastName":"Smith"},
   {"firstName":"Peter", "lastName":"Jones"}
]}

XML 

<employees>  
    <employee>  
        <firstName>John</firstName>  
        <lastName>Doe</lastName>  
    </employee>  
    <employee>  
        <firstName>Anna</firstName>  
        <lastName>Smith</lastName>  
    </employee>  
    <employee>  
        <firstName>Peter</firstName>  
        <lastName>Jones</lastName>  
    </employee>  

</employees>  

Automated using Visual Studio


Generate Class From JSON or XML in Visual Studio


This approach uses Visual Studio to generate a class just by copying and pasting the JSON or XML string.

The following is the procedure to generate the class:
  1. Copy JSON or XML string
    JSON

    JSON


    XML

    XML
  2. Go to Edit > Paste Sepcial > Paste JSON As Classes or Paste XML As Classes.

    past spacial
Visual Studio generates a class structure for the developer as in the following:

The following is an example of the class structure created by copying and pasting a JSON string.

public class EmployeeList {  
    public Employee[] employees {  
        get;  
        set;  
    }  
}  
  
public class Employee {  
    public string firstName {  
        get;  
        set;  
    }  
    public string lastName {  
        get;  
        set;  
    }  
}  

No comments:

Post a Comment

React Hooks - custom Hook

  v CustomHook Ø React allows us to create our own hook which is known as custom hook. Example – 1 localStorage Demo Step-1 Create ...