سؤال

I am using RESTSharp to receive and deserialize result from an API call. Response is JSON. I've created a class for the repsonse that looks like this:

public class JsonResponseClass
{
    public class Selector
    {
        public static string verb { get; set; }
    }

    public class Points
    {
        public int definition { get; set; }
    }
 }

I do following to get the response:

var response = client.Execute<JsonResponseClass>(request);
var resData = response.Data;

How do I read/print values received from above? For example how do I print values verb and definition from the above deserialized response?

هل كانت مفيدة؟

المحلول

You're not supposed to nest the classes. Instead, add a property of each type to the root object's class.

public class JsonResponseClass
{
    public Selector selector { get; set; }

    public Points points { get; set; }
}

public class Selector
{
    public static string verb { get; set; }
}

public class Points
{
    public int definition { get; set; }
}

With that in place, the code works as expected:

var response = client.Execute<JsonResponseClass>(request);
var resData = response.Data;
var verb = resData.selector.verb;
var definition = resData.points.definition;

نصائح أخرى

It's not clear what are you asking.

resData variable contains data from request stored in JsonResponseClass so you need to access it's fields like:

string verb = resData.verb;
Console.WriteLine(verb);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top