GENERATE JSON STRING WITH C# SAMPLE CODE

   

JSON (JavaScript Object Notation) adalah format pertukaran data yang ringan, mudah dibaca dan ditulis oleh manusia, serta mudah diterjemahkan dan dibuat (generate) oleh komputer.
Berikut ini saya berikan contoh sample code generate string JSON menggunakan bahasa C#

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
 
namespace VERSI02
{
    public class LogEntry
    {
        public string Details { get; set; }
        public DateTime LogDate { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            LogEntry entry = new LogEntry
            {
                LogDate = new DateTime(2009, 2, 15, 0, 0, 0, DateTimeKind.Utc),
                Details = "Application started."
            };
            string isoJson = JsonConvert.SerializeObject(entry);
            JsonSerializerSettings microsoftDateFormatSettings = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
            };
            string microsoftJson = JsonConvert.SerializeObject(entry, microsoftDateFormatSettings);
            string javascriptJson = JsonConvert.SerializeObject(entry, new JavaScriptDateTimeConverter());
            Console.WriteLine(javascriptJson);
            Console.Read();
        }
    }
}
   

Wahyu Amaldi, M.Kom has written 112 articles

Hi Reader, thank you for read this article, i hope this article can be useful.
If you will share knowladge to me, dont forget contact to me.

Email 1 : wahyu.amaldi@outlook.com
Email 2 : wahyu.amaldi@ilmuprogram.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>