The Target field is of string type in your json, hence the serialiser will attempt to read it as a string.
You can use a converter to overrule that, for example using Newtonsoft Json.
Let's assume your data structure to be defined as follows:
public class Data {
public string Description{ get; set; }
public ICollection<int> Target{ get; set; }
public int Category { get; set; }
}
Then you may create your own JsonConverter as follows:
public class DataConverter : JsonConverter {
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) {
var jObject = JObject.Load(reader);
var data = new Data();
if (jObject.TryGetValue("Description", out JToken jDescription)) {
data.Description = jDescription.Value<string>();
}
if (jObject.TryGetValue("Target", out JToken jTarget)) {
data.Target = ToTarget(jTarget, serializer);
}
if (jObject.TryGetValue("Category", out JToken jCategory)) {
data.Category = jCategory.Value<int>();
}
return req;
}
private static ICollection<int> ToTarget( JToken jTarget, JsonSerializer serializer ) {
int defaultValue = -1;
var target = new List<int>();
var collection = jTarget.Value<string>().Split(',');
foreach (string str in collection)
if (int.TryParse(str, out int number))
target.Add(number);
else
target.Add(defaultValue);
return target;
}
public override bool CanConvert(Type objectType) => objectType == typeof(Data);
}
You can then deserialise using the following code:
var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new DataConverter);
Data data = JsonConvert.DeserializeObject<Data>(yourJsonString, jsonSettings);
Further considerations: this approach provides clear separation between your data definition and the data parsing logic, hence keeping your data structure and definition clean from any json specific information and parsing logic.