[英]Exception on deserialization of JSON object with string array
我需要反序列化JSON對象。 但是,看來對象中的字符串數組會導致異常,而我不知道為什么。
我使用了json.parser.online.fr和json2csharp之類的工具來確保JSON的格式正確,反序列化的類也是正確的。 在這兩種情況下,工具都可以,並且沒有顯示錯誤。 但是我仍然例外。
任何幫助,將不勝感激。
{
"ID":123,
"Content":[ "{\"NewName\":\"asd\",\"Type\":\"2\"}]}" ],
"Notes":[""],
"Type":2,
"Subjects":"asd",
"Classes":"1",
"Name":"fdsgfd",
"Assign_Content":[
"[{\"Type\":\"text\",\"Text\":\"dfgfgs\"]"
],
"Creator":"example@example.com",
"isActive":"False",
"Editor":"example@example.com",
"CreatedDate":"2017-08-22T00:00:00",
"LastModifiedDate":"2017-08-22T00:00:00"
}
var task = JsonConvert.DeserializeObject<RootObject>(JSON);
public class RootObject
{
public int ID { get; set; }
public List<string> Content { get; set; }
public List<string> Notes { get; set; }
public int Type { get; set; }
public string Subjects { get; set; }
public string Classes { get; set; }
public string Name { get; set; }
public List<string> Assign_Content { get; set; }
public string Creator { get; set; }
public string isActive { get; set; }
public string Editor { get; set; }
public string CreatedDate { get; set; }
public string LastModifiedDate { get; set; }
}
Newtonsoft.Json.dll中發生了類型為'Newtonsoft.Json.JsonReaderException'的異常,但未在用戶代碼中處理
附加信息:解析值后,遇到意外字符:N.路徑“ Content [0]”,第1行,位置23。
感謝kblok我做到了:
JSON = "{\"ID\":123,\"Content\":[\"{\"NewName\":\"fdsgfd\",\"Type\":\"2\"}\"],\"Notes\":[\"\"],\"Type\":2,\"Subjects\":\"Tysk\",\"Classes\":\"3\",\"Name\":\"fdsgfd\",\"Assign_Content\":[\"[{\"Type\":\"text\",\"Text\":\"dfgfgs\"}]\"],\"Creator\":\"example@example.com\",\"isActive\":\"False\",\"Editor\":\"example@example.com\",\"CreatedDate\":\"2017-08-22T00:00:00\",\"LastModifiedDate\":\"2017-08-22T00:00:00\"}";
to
JSON= "{\"ID\":123,\"Content\":[\"{\\\"NewName\\\":\\\"fdsgfd\\\",\\\"Type\\\":\\\"2\\\"}\"],\"Notes\":[\"\"],\"Type\":2,\"Subjects\":\"Tysk\",\"Classes\":\"3\",\"Name\":\"fdsgfd\",\"Assign_Content\":[\"[{\\\"Type\\\":\\\"text\\\",\\\"Text\\\":\\\"dfgfgs\\\"}]\"],\"Creator\":\"example@example.com\",\"isActive\":\"False\",\"Editor\":\"example@example.com\",\"CreatedDate\":\"2017-08-22T00:00:00\",\"LastModifiedDate\":\"2017-08-22T00:00:00\"}";
現在可以使用了!
該字符串的結果:
\"Content\":[\"{\"NewName\":\"fdsgfd\",\"Type\":\"2\"}\"]
是:
"Content":["{"NewName":"fdsgfd","Type":"2"}"]
如您所見,那是無效的JSON。 因此,您需要轉義反斜杠以獲取有效的JSON。 代替這個:
\"{\"NewName\":\"fdsgfd\",\"Type\":\"2\"}\"
它應該是:
\"{\\\"NewName\\\":\\\"fdsgfd\\\",\\\"Type\\\":\\\"2\\\"}\"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.