DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Pasting JSON/XML as Classes in Visual Studio

Pasting JSON/XML as Classes in Visual Studio

Visual Studio provides a shortcut to generate classes from XML or JSON data.

Daniel D'agostino user avatar by
Daniel D'agostino
·
Nov. 23, 15 · Tutorial
Like (3)
Save
Tweet
Share
2.61K Views

Join the DZone community and get the full member experience.

Join For Free

rest and json together have revolutionized the way we expose and consume data in recent years. for this reason it has become increasingly common to need to integrate with rest services, and data itself is provided in either xml or json format. this typically involves creating a set of classes that match the data provided by the service, so that the application may take care of serialisation and deserialisation, and work with structured data.

although you can create these data classes manually, visual studio provides a shortcut to generate classes from xml or json data. for instance, let’s say that we want to work with the data from the github api :

github-api

we can easily generate a class for this by copying that data, and in visual studio going into edit -> paste special -> parse json as classes :

paste-json-as-classes

and as if by magic, the following class gets generated:


        public class rootobject
        {
            public string current_user_url { get; set; }
            public string current_user_authorizations_html_url { get; set; }
            public string authorizations_url { get; set; }
            public string code_search_url { get; set; }
            public string emails_url { get; set; }
            public string emojis_url { get; set; }
            public string events_url { get; set; }
            public string feeds_url { get; set; }
            public string followers_url { get; set; }
            public string following_url { get; set; }
            public string gists_url { get; set; }
            public string hub_url { get; set; }
            public string issue_search_url { get; set; }
            public string issues_url { get; set; }
            public string keys_url { get; set; }
            public string notifications_url { get; set; }
            public string organization_repositories_url { get; set; }
            public string organization_url { get; set; }
            public string public_gists_url { get; set; }
            public string rate_limit_url { get; set; }
            public string repository_url { get; set; }
            public string repository_search_url { get; set; }
            public string current_user_repositories_url { get; set; }
            public string starred_url { get; set; }
            public string starred_gists_url { get; set; }
            public string team_url { get; set; }
            public string user_url { get; set; }
            public string user_organizations_url { get; set; }
            public string user_repositories_url { get; set; }
            public string user_search_url { get; set; }
        }

now, the class and property names might not be exactly the way you want them, and you might need to change some things, but this will still save you a lot of typing.

if you’re still not convinced, try it with a more complex example that involves nested data structures. for instance, let’s say we have this json data:


{
"name" : "john smith",
"summary" : "wannabe superhero",
"workexperience" : [{
"title" : "insurance guy",
"company" : "abc ltd.",
"start" : "feb 2015",
"description" : "current job, very bad"
}, {
"title" : "cleaning guy",
"company" : "super clean ltd.",
"start" : "jan 2013",
"end" : "jan 2015",
"description" : "dirty work",
"recommendations" : [{
"name" : "boss guy",
"position" : "boss"
}, {
"name" : "colleague girl",
"position" : "cleaning girl"
}
]
}
]
}

with no effort whatsoever, you can get these classes generated for you:


        public class rootobject
        {
            public string name { get; set; }
            public string summary { get; set; }
            public workexperience[] workexperience { get; set; }
        }

        public class workexperience
        {
            public string title { get; set; }
            public string company { get; set; }
            public string start { get; set; }
            public string description { get; set; }
            public string end { get; set; }
            public recommendation[] recommendations { get; set; }
        }

        public class recommendation
        {
            public string name { get; set; }
            public string position { get; set; }
        }

and there’s a similar function for xml.

you're welcome.

Data (computing)

Published at DZone with permission of Daniel D'agostino, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Bye Bye, Regular Dev [Comic]
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Deploying Java Serverless Functions as AWS Lambda
  • How to Create a Real-Time Scalable Streaming App Using Apache NiFi, Apache Pulsar, and Apache Flink SQL

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: