Привет! У меня есть следующий JSON, и я хочу десериализовать в ICollection<Location>
"\ u0001 [{\" Id \ ": 1, \" Name \ ": \" A-01\ ", \" Address \ ": \" aa \ ", \" Children \ ": [{\" Id \ ": 2, \" Name \ ": \" A-01-01 \ ", \" Address\ ": \" bb \ ", \" Children \ ": [{\" Id \ ": 5, \" Name \ ": \" A-01-02-01 \ ", \" Address \ ": \"cc \"}]}, {\ "Id \": 3, \ "Name \": \ "A-01-02 \", \ "Address \": \ "dd \"}]}}, {\"Id \": 4, \ "Name \": \ "B-01 \", \ "Address \": \ "ee \", \ "Children \": [{\ "Id \": 6, \"Имя \": \ "B-01-01 \", \ "Адрес \": \ "ff \"}]}] "
для чтения JSON
[
{
"Id": 1,
"Name": "A-01",
"Address": "aa",
"Children": [
{
"Id": 2,
"Name": "A-01-01",
"Address": "bb",
"Children": [
{
"Id": 5,
"Name": "A-01-02-01",
"Address": "cc"
}
]
},
{
"Id": 3,
"Name": "A-01-02",
"Address": "dd"
}
]
},
{
"Id": 4,
"Name": "B-01",
"Address": "ee",
"Children": [
{
"Id": 6,
"Name": "B-01-01",
"Address": "ff"
}
]
}
]
Местоположение объекта
public class Location
{
[JsonProperty("Id")]
public int Id { get; set; }
[JsonProperty("Name")]
[Required]
public string Name { get; set; }
[JsonProperty("Address")]
public string Address { get; set; }
public ICollection<Asset> Assets { get; set; }
public Location ParentLocation { get; set; }
[JsonProperty("Children")]
public virtual ICollection<Location> ChildrenLocation { get; set; }
}
Я пытаюсь проверить строку очистки, но без надежды ..
var jsonString = @"[{""Id"":1,""Name"":""A-01"",""Address"":""aa"",""Children"":[{""Id"":2,""Name"":""A-01-01"",""Address"":""bb",""Children"":[{""Id"":5,""Name"":""A-01-02-01"",""Address"":""cc""}]},{""Id"":3,""Name"":""A-01-02"",""Address"":""dd""}]},{""Id"":4,""Name"":""B-01"",""Address"":""ee"",""Children"":[{""Id"":6,""Name"":""B-01-01"",""Address"":""ff""}]}]";
var locations = JsonConvert.DeserializeObject<ICollection<Location>>(jsonString.Trim());
ОШИБКА
Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: . Path '', line 0, position 0.
at Newtonsoft.Json.JsonTextReader.ParseValue()
at Newtonsoft.Json.JsonTextReader.Read()
at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)