Remember my post How to JSON with Google Json in Java ? This time am gonna show you how easy using JSON in C# using Json.NET reference. Landed by this post, I assuming you knew already what JSON is, and when to use em so I’ll straight to the point how to dealing JSON in C#. First create POCO or classes that gonna describe the JSON like below.

//author mdsaputra.wordpress.com | EtaYuy | Meihta Dwiguna Saputra

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JsonTutorial
{
    [Serializable]
    public class PosRequest
    {
        public int ammount { get; set; }
        public String requestType { get; set; }
    }
}

And below is how to serialize (convert to JSON) and deserialize (convert to POCO) em :

//author mdsaputra.wordpress.com | EtaYuy | Meihta Dwiguna Saputra

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace JsonTutorial
{
    class JsonMessageHandler
    {
        //Serialize
        public static String parseObjectToJson(Object objectToJson)
        {
            return JsonConvert.SerializeObject(objectToJson);
        }

        //Deserialize
        public static PosRequest parseJsonToTransactionMessage(String json)
        {
            return JsonConvert.DeserializeObject<PosRequest>(json);
        }
    }
}

Easy isn’t it? Hope the post useful, thanks for readingšŸ˜‰ .
Twitter Subscribe :