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
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Comprehensive Guide To Working With JSON in JavaScript
  • 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

Trending

  • From Open SQL to CDS Views: Rewriting SAP Data Access for Performance at Scale
  • Introducing RAI Audit Kit: Evidence-Grade Responsible AI Audits in Python
  • Testing AI-Infused Apps: A Dual-Layer Framework for AI Quality Assurance
  • Devs Don't Want More Dashboards; They Want Self-Healing Systems
  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

By 
Toni Petrina user avatar
Toni Petrina
·
Oct. 12, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
26.5K 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. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Comprehensive Guide To Working With JSON in JavaScript
  • 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

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook