-1

I am struggling to deserialize the following JSON string that I am receiving from an API:

{
  "appointment_types": [
    {
      "id": 279148,
      "max_attendees": 1,
      "name": "Follow up",
      "billable_item": {
        "links": {
          "self": "https://api.cliniko.com/v1/billable_items/485545"
        }
      },
      "practitioners": {
        "links": {
          "self": "https://api.cliniko.com/v1/appointment_types/279148/practitioners"
        }
      }
    },
    {
      "id": 279149,
      "max_attendees": 1,
      "name": "Assessment",
      "billable_item": {
        "links": {
          "self": "https://api.cliniko.com/v1/billable_items/490437"
        }
      },
      "practitioners": {
        "links": {
          "self": "https://api.cliniko.com/v1/appointment_types/279149/practitioners"
        }
      }
    }
  ],
  "total_entries": 17,
  "links": {
    "self": "https://api.cliniko.com/v1/appointment_types?page=1"
  }
}

I have searched but I couldn't find anything that would work for the above.

Any pointers that may get me on the right track would be greatly appreciated.

dbc
  • 104,963
  • 20
  • 228
  • 340
Ash
  • 11
  • 2
  • 1
    What language are you using? If c#, did you try any of the tools mentioned in [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674)? – dbc Aug 07 '18 at 09:48
  • That's a good question - if this is javascript, for example, that is not serialised. – Nathan Stockton Aug 07 '18 at 09:50
  • Sorry, i'm using C# and I have used json2csharp.com to create some classes but after many hours of trial and error I got absolutely nowhere – Ash Aug 07 '18 at 09:58
  • 1
    @Ash show a [mcve] of what is not working – Nkosi Aug 07 '18 at 10:03
  • 1
    @Ash the above parses just fine in this example https://app.quicktype.io?share=SHlY5JV5011TJxROvCFd – Nkosi Aug 07 '18 at 10:04

2 Answers2

0

This seems to work fine for me just using dynamic...

dynamic d = JObject.Parse(json);
var totalNumber = d.total_entries.ToString();
var theId = d.appointment_types[0].id.ToString();

What have you tried?

Milney
  • 6,253
  • 2
  • 19
  • 33
0

I would create c# classes for the structure and then use Newtonsoft Json.NET for deserializion. (It is fast and already in c# but you have to add the reference.)

Here is my code:

class Program
{
    static void Main(string[] args)
    {
        string json = File.ReadAllText("demo.json"); //Your json here
        RequestResult requestResult = Newtonsoft.Json.JsonConvert.DeserializeObject<RequestResult>(json); //There is your result
        Console.WriteLine("Done!");
        Console.ReadLine();
    }
}

class RequestResult
{
    public AppointmentType[] appointment_types;
    public int total_entries;
    public Link links;
}

class Practitioners
{
    public Link links;
}

class BillableItem
{
    public Link links;
}

class Link
{
    public string self;
}

class AppointmentType
{
    public int id;
    public int max_attendees;
    public string name;
    public BillableItem billable_item;
    public Practitioners practitioners;
}

Then you have the result as a c# object and things like intellisense and code completion do work.

Felix Mayer
  • 1
  • 1
  • 2