嗨,我有以下JSON,希望对ICollection<Location>
" u0001 [{" id ":1," name ":" a-01 "," address ":" aa ", " children ":[{" id ":2," name ":" a-01-01 "," address ":" bb ", "儿童":[{" 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 "}]}]}]"
可读的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)
错误很明显,字符串不是有效的JSON,问题是第一个字符u0001
。
"可读" JSON字符串和内饰测试不包含该字符,因此他们甚至无法重现问题。
trim()也无法独自工作。它的工作是删除空格,但u0001
是SOH(起始人)角色。那是不是被认为是空格。
为了实际解决该问题,应修复产生此特征的代码,以便它不会发出。如果JSON行来自使用SOH用作记录分配器的流json文件,则应将其修复,以便它在JSON字符串中包含记录定界符。
如果不可能,则应使用String.trim(char [])超载删除SHO字符:
var locations = JsonConvert.DeserializeObject(jsonString.Trim('u0001'));
一旦删除了SOH字符,避免序列化就可以了:
var jsonString = "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, "Name": "B-01-01", "Address": "ff"}]}]";
var cleaned=jsonString.Trim('u0001');
var locations = (JArray)JsonConvert.DeserializeObject(cleaned);
Debug.Assert(locations.Count==2);
var address=(JValue)locations[1]["Children"][0]["Address"];
Debug.Assert(String.Equals(address.Value,"ff"));
dai所说,有" u0001"字符(请参阅描述)。我认为您从Somwehere复制它,而不需要它,因此您可以将其删除:
var str =
"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, "Name": "B-01-01", "Address": "ff"}]}]";
var unescapeStr = str.Replace("u0001", String.Empty);
var result = JsonConvert.DeserializeObject<ICollection<Location>>(unescapeStr);
字符" u0001"根本不应该在您的字符串中,不仅在开始时,因此您必须使用替换not trim
将其从所有字符串中删除测试代码中的第二个问题。当您复制一些Scpecial字符的Visual Studio问您时,您是否想逃脱它,如果您说"是",您可以逃脱两次字符
因此,尝试使用上面的示例测试字符串