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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //author mdsaputra.wordpress.com | EtaYuy | Meihta Dwiguna Saputrausing 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 :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //author mdsaputra.wordpress.com | EtaYuy | Meihta Dwiguna Saputrausing 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,


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.