Application Programming Interfaces C#

Constructor Usage for JSON with Newtonsoft

This C# program demonstrates how to create and modify a JSON document via element constructors with the Newtonsoft Json package. Newtonsoft.json is a Nuget package that must be installed in order to run this program.

Program.cs

using System;
// Requires Nuget Newtonsoft.Json package
using Newtonsoft.Json.Linq;

namespace JsonConstructorUsage
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var qJson = new JObject(
                            new JProperty("Proverbs6",
                                new JObject(
                                    new JProperty("Seven Things",
                                        new JArray(
                                            new JValue("haughty eyes"),
                                            new JValue("a lying tongue"),
                                            new JValue("hands that shed innocent blood"),
                                            new JValue("a heart that devises wicked plans"),
                                            new JValue("feet that make haste to run to evil"),
                                            new JValue("a false witness who breathes out lies"),
                                            new JValue("a man who sows discord among brothers")
                                        )
                                    )
                                )
                            ),
                            new JProperty("Wisdom2",
                                new JArray(
                                    new JValue("Short and sorrowful is our life,"),
                                    new JValue("and there is no remedy when a man comes to his end,"),
                                    new JValue("and no one has been known to return from Hades."),
                                    new JValue("Because we were born by mere chance,"),
                                    new JValue("and hereafter we shall be as though we had never been;"),
                                    new JValue("because the breath in our nostrils is smoke,"),
                                    new JValue("and reason is a spark kindled by the beating of our hearts."),
                                    new JValue("When it is extinguished, the body will turn to ashes,"),
                                    new JValue("and the spirit will dissolve like empty air."),
                                    new JValue("Thus they reasoned, but they were led astray,"),
                                    new JValue("for their wickedness blinded them,"),
                                    new JValue("and they did not know the secret purposes of God,"),
                                    new JValue("nor hope for the wages of holiness,"),
                                    new JValue("nor discern the prize for blameless souls;"),
                                    new JValue("for God created man for incorruption,"),
                                    new JValue("and made him in the image of his own eternity,"),
                                    new JValue("and those who belong to his party experience it.")
                                )
                            )
                        );
            Console.WriteLine(qJson.ToString());
        }
    }
}
 

Output

{
  "Proverbs6": {
    "Seven Things": [
      "haughty eyes",
      "a lying tongue",
      "hands that shed innocent blood",
      "a heart that devises wicked plans",
      "feet that make haste to run to evil",
      "a false witness who breathes out lies",
      "a man who sows discord among brothers"
    ]
  },
  "Wisdom2": [
    "Short and sorrowful is our life,",
    "and there is no remedy when a man comes to his end,",
    "and no one has been known to return from Hades.",
    "Because we were born by mere chance,",
    "and hereafter we shall be as though we had never been;",
    "because the breath in our nostrils is smoke,",
    "and reason is a spark kindled by the beating of our hearts.",
    "When it is extinguished, the body will turn to ashes,",
    "and the spirit will dissolve like empty air.",
    "Thus they reasoned, but they were led astray,",
    "for their wickedness blinded them,",
    "and they did not know the secret purposes of God,",
    "nor hope for the wages of holiness,",
    "nor discern the prize for blameless souls;",
    "for God created man for incorruption,",
    "and made him in the image of his own eternity,",
    "but through the devil's envy death entered the world,",
    "and those who belong to his party experience it."
  ]
}
Press any key to continue . . .
 
 

© 2007–2025 XoaX.net LLC. All rights reserved.