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 Video Library
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
View Events Video Library
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • JSON-Based Serialized LOB Pattern
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator

Trending

  • Docker and Kubernetes Transforming Modern Deployment
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  • Navigating the Skies
  • Modular Software Architecture: Advantages and Disadvantages of Using Monolith, Microservices and Modular Monolith
  1. DZone
  2. Coding
  3. Languages
  4. Quickly Create JSON Object from Anonymous Object with Json.NET

Quickly Create JSON Object from Anonymous Object with Json.NET

Toni Petrina user avatar by
Toni Petrina
·
Oct. 12, 13 · Interview
Like (0)
Save
Tweet
Share
24.02K Views

Join the DZone community and get the full member experience.

Join For Free

Recently I needed to quickly create some temporary JSON objects from some preexisting data. So, I turned to Json.NET, a very popular JSON framework for .NET. Since I got my data on the fly (think parsing some other data structures and you have lots of anonymous results), I didn’t want to create classes for such instances.

Luckily, Json.NET supports serializing anonymous objects. First add the library via NuGet – it is called Newtonsoft.Json. For example:

var obj = new
{
	user = new
	{
		name = "John",
		age = 21,
		data = new[] { 1, 2, 3, 4 }
	}
};

var result = JsonConvert.SerializeObject(obj, Formatting.Indented);

If we did that in a console application, we could print out the result. The printout is:

{
  "user": {
    "name": "John",
    "age": 21,
    "data": [
      1,
      2,
      3,
      4
    ]
  }
}

The code above works on Windows 8, Windows Phone 7 and 8. This way it is dead simple to create JSON representation of your anonymous data. Think REST services.

JSON Object (computer science)

Published at DZone with permission of Toni Petrina, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • JSON-Based Serialized LOB Pattern
  • How To Check for JSON Insecure Deserialization (JID) Attacks With Java
  • Introduction to Couchbase for Oracle Developers and Experts: Part 2 - Database Objects
  • Deep Dive Into DataWeave Map, Filter, MapObject, and Filter Object Operator

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: