0

I am trying to deserialize JSON to C# object but not able to get rid of this compiler error. Any help would be much appreciated.

JSON

{
  AX:{BX:1777} 
}

Here are my deserializer classes:

Response.cs

{
    public class Response
    {
        public AX Ax { get; set; }
    }
}

AX.cs

{
    public class AX
    {
        public long Bx { get; set; }
    }
}

Here is the line that is problematic:

IRestResponse<Response> response = client.Execute<Response>(request);

response.Content is just as fine and returns the raw JSON but I want it to be an instance of the Response class. I want to access Bx like this:

var price = response.Ax.Bx; // should return 1777

But this line produces the following compiler error:

Error: IRestResponse does not contain definition for 'Ax'
9
  • Can you try Response response = client.Execute<Response>(request); ? By saying IRestResponse<Response>, you are casting to a type of IRestResponse and you might not have Ax field defined in the IRestResponse interface. Commented Oct 1, 2018 at 5:51
  • Yes but I was getting a different error. Commented Oct 1, 2018 at 5:52
  • Well I have Ax field in the Response Class. I thought that's where it was supposed to be at. Commented Oct 1, 2018 at 5:56
  • Yes that's where it needs to be. Just going through their documentation (restsharp.org), looks like you need to use RestResponse<Response> response = client.Execute<Response>(request); and not IRestResponse interface. Please check if this helps Commented Oct 1, 2018 at 6:04
  • I already tried this as well. Initially cast explicitly but in vain. the same error persists. Commented Oct 1, 2018 at 6:07

2 Answers 2

1

For what it's worth, and having spent some time searching around this, I would have used Newtonsoft.Json and solved it like this:

IRestResponse response = client.Execute(request);
string ResponseStr = response.Content.ToString();
dynamic Response = JsonConvert.DeserializeObject<dynamic>(ResponseStr);

you can then call off the elements as you need them:

var price = Response.AX.BX;
string Description = Response.AX.Desc.ToString();     etc

Hope this helps someone

Sign up to request clarification or add additional context in comments.

Comments

0

Problem is with case sensitive. RestSharp serializer expects following json structure

{
  Ax:{Bx:1777} 
}

You have 3 ways to deal with it:

1) add DataContract and DataMember to your classes

[DataContract]
public class Response
{
    [DataMember(Name = "AX")]
    public AX Ax { get; set; }
}

[DataContract]
public class AX
{
    [DataMember(Name = "BX")]
    public long Bx { get; set; }
}

2) write your own serializer that ignores case sensitive and use it with restsharp

3) change your json structure

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.