There is the following JSON

{ "items": [ { "name": "Book", "description": "", } ] } class ActionCollection { [JsonProperty("items")] public string Content { get; set; } } 

need to convert an array to a string during deserialization. Now it crashes with the Unexpected character error encountered while parsing value: [. Path 'items''

  • And why do you need such a mockery? Why not create a complete structure for working with this json? - EvgeniyZ
  • Everything else needs to be displayed as is - Oleg

1 answer 1

Custom converter create:

 public class ListToStringConverter : JsonConverter { 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) { JArray data = serializer.Deserialize<JArray>(reader); // тут что-то делаем, преобразуем в строку, например return data.ToString(); } public override bool CanConvert(Type objectType) { throw new NotImplementedException(); } } 

And assign him to the class:

 internal class ActionCollection { [JsonProperty("items")] [JsonConverter(typeof (ListToStringConverter))] public string Content { get; set; } } 
  • Those. is it anyway easier to deserialize, and then bring to a string? Those. Is there no such thing to decriminalize only the first level? - Oleg
  • @Oleg from the box there is no such thing, just write. - Suvitruf