JSON Array with RootObject and SubObjects

Skythrust

Member
Jul 9, 2019
133
7
Hi All,

I have these two kind of libraries;

C#:
    public class Root
    {
        public List<Liveweer> liveweer { get; } = new List<Liveweer>();
        public List<WkVerw> wk_verw { get; } = new List<WkVerw>();
        public List<UurVerw> uur_verw { get; } = new List<UurVerw>();
        public List<Api> api { get; } = new List<Api>();
    }

    public class Api
    {
        public string source { get; set; }
        public int max_verz { get; set; }
        public int rest_verz { get; set; }
    }

I was wondering how I can call source, max_verz and rest_verz inside the Api class, I was thinking I could call them via a foreach to use them in a WPF application.
Whatever I try I will get the messages that the JSON is incorrect. but the JSON is from a third party integrator which works with other systems.
 

Foxer

New Member
Oct 7, 2015
3
0
U can access these properties directly within the methods or properties of the API class.

C#:
public class Api{
    public string source { get; set; }
    public int max_verz { get; set; }
    public int rest_verz { get; set; }

    // Here we create the constructor
    public Api(string source, int max_verz, int rest_verz){
        this.source = source;
        this.max_verz = max_verz;
        this.rest_verz = rest_verz;
    }

    // Example
    public void ShowSourceAPI(){
        Console.WriteLine("Source: " + source);
    }
}
 
Last edited:

Skythrust

Member
Jul 9, 2019
133
7
U can access these properties directly within the methods or properties of the API class.

Code:
public class Api{
    public string source { get; set; }
    public int max_verz { get; set; }
    public int rest_verz { get; set; }

    // Here we create the constructor
    public Api(string source, int max_verz, int rest_verz){
        this.source = source;
        this.max_verz = max_verz;
        this.rest_verz = rest_verz;
    }

    // Example
    public void ShowSourceAPI(){
        Console.WriteLine("Source: " + source);
    }
}

But still have to do a RestRequest?
 

Skythrust

Member
Jul 9, 2019
133
7
I have already solve this case above.

Now I am calling another RESTfulAPI request, but this is a weird one. In Postman I am recieving the following output;

JSON:
"{\r\n   \"status\": \"Active\",\r\n   \"activatedOn\": \"2021-02-09T00:00:00\",\r\n  \"licenseType\": \"Production\",\r\n}"

How can I use this one to deserialize in C#?

C#:
public class Root
    {
        public string status { get; set; }
        public DateTime activatedOn { get; set; }
        public string licenseType { get; set; }
    }
 

Foxer

New Member
Oct 7, 2015
3
0
U can use the Newtonsoft lib to deserialize:

C#:
using System;
using Newtonsoft.Json; //JSON Lib

public class Program {
    public static void Main(string[] args)
    {
        string jsonString = "{\"status\": \"Active\",\"activatedOn\": \"2021-02-09T00:00:00\",\"licenseType\": \"Production\"}";

        Root root = JsonConvert.DeserializeObject<Root>(jsonString);

        Console.WriteLine("Status: " + root.status);
        Console.WriteLine("Activated On: " + root.activatedOn);
        Console.WriteLine("License Type: " + root.licenseType);
    }
}

public class Root {
    public string status { get; set; }
    public DateTime activatedOn { get; set; }
    public string licenseType { get; set; }
}
 

Users who are viewing this thread

Top