-1

I would like to return this json response

{
    "Type": "testing",
    "Errors": {
        "InputOne": [
            "The input one is accepted"
        ],
        "InputTwo": [
            "The input two is accepted"
        ]
    }
}

But after trying a few rounds all I got was this

{
    "Type": "testing",
    "Errors": {
        "InputOne": "The input one is accepted",
        "InputTwo": "The input two is accepted"
    }
}

May I know what am I missing here?

These are my code

public string Type { get; set; }
public ErrorClass Errors { get; set; }

public class ErrorClass
{
  public object InputOne { get; set; }
  public object InputTwo { get; set; }
}

Thanks in advance!

Zorev Gnoz
  • 221
  • 4
  • 20
  • I'm guessing they are both strings instead of arrays/lists/collections of strings! – phuzi Aug 12 '21 at 09:57
  • 2
    you need an array/list in ErrorClass like public List InputOne { get; set; } – Imran Ahmad Shahid Aug 12 '21 at 09:57
  • 3
    You can create classes directly from demo json -f.e. https://codewithoutcomplaint.com/how-to-generate-a-c-class-from-json-in-visual-studio-2019/ - no need to "try a few rounds" or [how-to-auto-generate-a-c-sharp-class-file-from-a-json-string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Patrick Artner Aug 12 '21 at 09:59
  • Does this answer your question? [How to auto-generate a C# class file from a JSON string](https://stackoverflow.com/questions/21611674/how-to-auto-generate-a-c-sharp-class-file-from-a-json-string) – Patrick Artner Aug 12 '21 at 10:01
  • @PatrickArtner thank you for your suggestion to look at the auto generate a c sharp file from json! It helps alot! – Zorev Gnoz Aug 12 '21 at 10:08

2 Answers2

4

Try this one:

public string Type { get; set; }
public ErrorClass Errors { get; set; }

public class ErrorClass
{
  public string[] InputOne { get; set; }
  public string[] InputTwo { get; set; }
}
Nicola Biada
  • 2,325
  • 1
  • 8
  • 22
  • Thank your for your help nicola I also eventually found out after @patrickartner comment of generating a c sharp class file from json Thank you both very much! – Zorev Gnoz Aug 12 '21 at 10:05
1

Try this: You need to return a list/collection/array of InputOne and InputTwo according to your response.

public string Type { get; set; }
public ErrorClass Errors { get; set; }

public class ErrorClass
{
   public List<object> InputOne { get; set; }
 public List<object> InputTwo { get; set; }
}