C# and JSON – Hello,World Example

I haven’t really played around with JSON data streams, but the need arose when consuming some data from another system recently. In order to do a quick and dirty solution I simply read it in as a String and then split the string where I needed to, and found the data I required. I’ve now revisited this example to do it properly!

My raw data string looks very similar to this:

{"patron":
{"FIRSTNAME" : "Gregor",
"MIDDLENAME": "",
"LASTNAME" : "Bowie",
"ERROR" : ""
},

"HasPaidRecently" :false,
"patronComments" :"You have no books on loan",
"potentialChargeAfterRenewal":12.6,
"patronAccountStatus" :"A",
"totalFineFee" :0,
"calculatedOverdueFine" :0,
"totalReplacementCharges" :0,
"totalBritishLibraryCharges" :0,
"returnedItemFines" :12.6,
"oldOutstandingFines" :0,
"numberOfItemsBorrowed" :0,
"caseId" :151,
"thereWillBeRowsToDisplay" :true,
"widgetLogId" :48721,
"sessionId" :"B80B9913C513EC8C4B81F68B3FD0A8AD"
}

In order to process this data using .NET Serializer a couple of classes representing the object structure need to be created (due to the inner Patron syntax above).

The parent class takes this form:


public class Patron
{
   public NameDetails patron { get; set; }
   public string HasPaidRecently { get; set; }
   public string patronComments { get; set; }
   public string potentialChargeAfterRenewal { get; set; }
   public string patronAccountStatus { get; set; }
   public double totalFineFee { get; set; }
   public double calculatedOverdueFine { get; set; }
   public double totalReplacementCharges { get; set; }
   public double totalBritishLibraryCharges { get; set; }
   public double returnedItemFines { get; set; }
   public double oldOutstandingFines { get; set; }
   public int numberOfItemsBorrowed { get; set; }
   public int caseId { get; set; }
   public bool thereWillBeRowsToDisplay { get; set; }
   public int widgetLogId { get; set; }
   public string sessionId { get; set; }
}

With the inner NameDetails class looking as:


public class NameDetails
{
   public string FIRSTNAME { get; set; }
   public string MIDDLENAME { get; set; }
   public string LASTNAME { get; set; }
   public string ERROR { get; set; }
}

Note, I’ve made my variable names match exactly the format they are given in the JSON. I would imagine, although haven’t tried, adding a DataMember attribute would override the naming if required.

Now for the source code that takes the raw string and converts it into the object.


Patron pat1 = new Patron();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(pat1.GetType());
MemoryStream sr = new MemoryStream(Encoding.Unicode.GetBytes(rawData));
Patron pat = serializer.ReadObject(sr) as Patron;
sr.Close();

Console.WriteLine("Person: " + pat.patron.FIRSTNAME + " " + pat.patron.LASTNAME);
Console.WriteLine("Has Paid Recently? " + pat.HasPaidRecently);
Console.WriteLine("Fines: " + pat.potentialChargeAfterRenewal);

Note – this isn’t production code, merely a spike to prove things.

Advertisements

One thought on “C# and JSON – Hello,World Example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s