2 Replies - 366 Views - Last Post: 28 October 2020 - 01:33 PM Rate Topic: -----

#1 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 342
  • Joined: 25-May 16

API Controller to take two different types for Object Property

Posted 28 October 2020 - 09:38 AM

Hello everyone,

I am trying to figure something out.

I have an API and I accept a complex object. The object has a particular json property that can sent to us like this
"DeclineReason": {
 "@nil": "true"
 }
==================
OR
==================
"DeclineReason": "Testing"


I can either have my C# code do this or the other but can't find it to do both
public string DeclineReason { get; set; }


public DeclineReason DeclineReasion { get; set; }
    public class DeclineReason
    {
    }



The issue is that I get an object that can give me a list of varying different DeclineReason statuses and they could have a combination of only giving me the string or the "@nil": "true" object.

Is there a way for me to have my API look accept both for the same name?

Is This A Good Question/Topic? 0
  • +

Replies To: API Controller to take two different types for Object Property

#2 fearfulsc2   User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 342
  • Joined: 25-May 16

Re: API Controller to take two different types for Object Property

Posted 28 October 2020 - 01:01 PM

I found a work-around/resolution

The service that sends us the information converts the nil value into a null and my API can accept that.
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings   User is offline

  • D.I.C Lover
  • member icon

Reputation: 244
  • View blog
  • Posts: 1,307
  • Joined: 05-December 13

Re: API Controller to take two different types for Object Property

Posted 28 October 2020 - 01:33 PM

Your question lacks clarity and raises more questions than answers, because you are not showing us your model logic which your controller is utilising. The response you receive is partially due to how you wrote your controllers to both get/put/send data. You are also responsible for the construction of your json data. So it's your job to make sure the items in your response being returned are easy to work with, and easy to deserialize to a class object such as : DeclineReason.

Only then you can determine what logic should be used. Have you looked at the Json serialize deserialise documentation? If you can provide more information, it might be easier to give you a more definitive and complete answer. Where is your json data being constructed?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1