Json.NET & Oxygene – How to Deserialize an Object ?

Published on March 13, 2016 by abundantcode

Filed under Json.NET

Last modified February 13, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 5394 times

One of the ways to Deserialize an object to JSON string in Oxygene in the Json.NET is using the DeSerializeObject method defined in the JsonConvert method.

How to Deserialize an Object in Oxygene using Json.NET ?

Below is a sample code snippet demonstrating how you can deserialize an object from Json string to Oxygene object using Json.NET. It takes the json string that contains the employee information and deserializes it to the Employee class.

namespace ACConsoleOxygene;

interface

uses
  System,
  System.Collections.Generic,
  Newtonsoft.Json;

type
  Program = class
  private
    class method Main(args: array of String);
  end;

  Employee = public class
  public
    property Name: String;
    property IsPermanent: Boolean;
    property Departments: List<String>;
  end;

implementation

class method Program.Main(args: array of String);
begin
  var jsonTxt: String := '{''Name'': ''Abundantcode'',  ''IsPermanent'': true, ''Departments'': [    ''Technology'',    ''Product Engineering'' ]    }';
  
  //  Deserialize an Json string to Employee object 
  var emp: Employee := JsonConvert.DeserializeObject<Employee>(jsonTxt);
  
  Console.WriteLine(emp.Name);
  
  Console.ReadLine();
end;

end.

The Deserialized object contains the following data.

image

Leave a Comment


x

You might be Interested in these Posts

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford http://www.youtube.com/watch?v=sfV0ApyN5fM ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...

Video - Getting started with Angular 2

In this screencast , Alex Eagle , developer on the Angular team talks about Angular 2 developer prev...