0

Error found in JsonConvert.DeserializeObject:

actions = JsonConvert.DeserializeObject<List<Action>>(response.Content);
    public class Input
    {
        public string name { get; set; }
        public string value { get; set; }
        public string label { get; set; }
    }

here the returned Json from the request

[
    {
        "name": "firstPageNumber",
        "value": "1",
        "label": "Start page numbering at"
    },
    {
        "name": "engine",
        "value": "localhost1",
        "label": "InDesign Server"
    },
    {
        "name": "documentstart",
        "value": [
          ""
        ],
        "label": "Document start"
    },
    {
        "name": "sectionstart",
        "value": [
          ""
        ],
        "label": "Section start"
    }
]

As I thought error caused by value property. It have both String and String Array returned. How should I change my Input class to map both of them.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
  • 3
    Sometimes `value` is a `string`, sometimes its an `array`, *side note* : this is a fine example of a web developer who needs to rethink their life choices. However, all that aside, you can generally use a converter to solve this type of problem – TheGeneral Feb 28 '20 at 05:54
  • try object with a custom setter, or just make it an array with a method that either gets the array or string as output – Vince Feb 28 '20 at 05:55
  • 2
    If the JSON property `"value"` is sometimes a single string, and sometimes an array of strings, you can deserialize it to a `List` by applying `[JsonConverter(typeof(SingleOrArrayConverter))]` from [How to handle both a single item and an array for the same property using JSON.net](https://stackoverflow.com/q/18994685). In fact I think this is a duplicate, agree? – dbc Feb 28 '20 at 06:05
  • According to https://jsonlint.com/ this is not valid JSON! – jason.kaisersmith Feb 28 '20 at 06:18
  • @jason.kaisersmith Indeed it's not valid JSON, but not because of the single item vs. array thing, but because the brackets of the JSON array are missing – Paul Kertscher Feb 28 '20 at 06:22
  • @RameshPrerera I've added the brackets to your JSON. Could you please check that it's not been the actual issue and you've only missed them in this question? – Paul Kertscher Feb 28 '20 at 06:23

1 Answers1

0

you can read you json like this

    static void Main(string[] args)
    {
        string json = @"[{name: 'firstPageNumber',
            value: '1',
            label: 'Start page numbering at'},
            {name: 'documentstart',
            value: [1,2,3],
            label: 'Document start'}]";
        var obj = JsonConvert.DeserializeObject<List<Input>>(json);
        Console.WriteLine(obj[0].value is JArray);
        Console.WriteLine(obj[2].value is JArray);
        Console.WriteLine(JsonConvert.SerializeObject(obj));
        Console.ReadKey();
    }
}

public class Input
{
    public string name { get; set; }
    public object value { get; set; }
    public string label { get; set; }
}
Mehrdad
  • 1,523
  • 9
  • 23