Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating JSON on the fly with JObject

Tags:

json

c#

json.net

For some of my unit tests I want the ability to build up particular JSON values (record albums in this case) that can be used as input for the system under test.

I have the following code:

var jsonObject = new JObject(); jsonObject.Add("Date", DateTime.Now); jsonObject.Add("Album", "Me Against The World"); jsonObject.Add("Year", 1995); jsonObject.Add("Artist", "2Pac"); 

This works fine, but I have never really like the "magic string" syntax and would prefer something closer to the expando-property syntax in JavaScript like this:

jsonObject.Date = DateTime.Now; jsonObject.Album = "Me Against The World"; jsonObject.Year = 1995; jsonObject.Artist = "2Pac"; 
like image 461
Karl Anderson Avatar asked Aug 15 '13 05:08

Karl Anderson


People also ask

What is the difference between JToken and JObject?

So you see, a JObject is a JContainer , which is a JToken . Here's the basic rule of thumb: If you know you have an object (denoted by curly braces { and } in JSON), use JObject. If you know you have an array or list (denoted by square brackets [ and ] ), use JArray.

What is the use of JObject in C#?

The JObject type exposes the following members. Initializes a new instance of the JObject class. Initializes a new instance of the JObject class with the specified content. Initializes a new instance of the JObject class with the specified content.

Can JSON be dynamic?

A dynamic JSON file will be created to store the array of JSON objects. Consider, we have a database named gfg, a table named userdata. Now, here is the PHP code to fetch data from database and store them into JSON file named gfgfuserdetails. json by converting them into an array of JSON objects.


2 Answers

Well, how about:

dynamic jsonObject = new JObject(); jsonObject.Date = DateTime.Now; jsonObject.Album = "Me Against the world"; jsonObject.Year = 1995; jsonObject.Artist = "2Pac"; 
like image 115
Dimitar Dimitrov Avatar answered Oct 25 '22 01:10

Dimitar Dimitrov


You can use the JObject.Parse operation and simply supply single quote delimited JSON text.

JObject  o = JObject.Parse(@"{   'CPU': 'Intel',   'Drives': [     'DVD read/writer',     '500 gigabyte hard drive'   ] }"); 

This has the nice benefit of actually being JSON and so it reads as JSON.

Or you have test data that is dynamic you can use JObject.FromObject operation and supply a inline object.

JObject o = JObject.FromObject(new {     channel = new     {         title = "James Newton-King",         link = "http://james.newtonking.com",         description = "James Newton-King's blog.",         item =             from p in posts             orderby p.Title             select new             {                 title = p.Title,                 description = p.Description,                 link = p.Link,                 category = p.Categories             }     } }); 

Json.net documentation for serialization

like image 38
Lee Jensen Avatar answered Oct 25 '22 00:10

Lee Jensen