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
Newsletter
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

  • Should Developers Switch From SQLite to DuckDB?
  • SSRF Prevention in C#: Securing File Imports From User-Supplied URLs
  • Searching Kafka Without an Index: Lessons From Building Message Search for Kafdrop
  • Delta Lake UniForm to Snowflake: Cross-Platform Table Access Without a Second Copy

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Oct. 12, 13 · Interview
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
26.7K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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